| 1 | /* |
| 2 | Copyright 2020 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package util |
| 18 | |
| 19 | import ( |
| 20 | "testing" |
| 21 | ) |
| 22 | |
| 23 | const ( |
| 24 | failed = "\u2717" |
| 25 | succeed = "\u2713" |
| 26 | ) |
| 27 | |
| 28 | func TestRemovePortFromHost(t *testing.T) { |
| 29 | tests := []struct { |
| 30 | name string |
| 31 | origHost string |
| 32 | expect string |
| 33 | }{ |
| 34 | {"Domain&Port", "localhost:8080", "localhost"}, |
| 35 | {"Domain", "localhost", "localhost"}, |
| 36 | {"IPv4&Port", "192.168.0.1:8080", "192.168.0.1"}, |
| 37 | {"ShortestIPv6", "::", "::"}, |
| 38 | {"IPv6", "9878::7675:1292:9183:7562", "9878::7675:1292:9183:7562"}, |
| 39 | {"IPv6&Port", "[alsk:1204:1020::1292]:8080", "alsk:1204:1020::1292"}, |
| 40 | {"FQDN", " www.kubernetes.test:8080", " www.kubernetes.test"}, |
| 41 | } |
| 42 | |
| 43 | for _, tt := range tests { |
| 44 | st := tt |
| 45 | tf := func(t *testing.T) { |
| 46 | t.Parallel() |
| 47 | t.Logf("\tTestCase: %s", st.name) |
| 48 | { |
| 49 | get := RemovePortFromHost(st.origHost) |
| 50 | if get != st.expect { |
| 51 | t.Fatalf("\t%s\texpect %v, but get %v", failed, st.expect, get) |
| 52 | } |
| 53 | t.Logf("\t%s\texpect %v, get %v", succeed, st.expect, get) |
| 54 | |
| 55 | } |
| 56 | } |
| 57 | t.Run(st.name, tf) |
| 58 | } |
| 59 | } |