github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/util/net/port_split_test.go (about) 1 /* 2 Copyright 2015 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 net 18 19 import ( 20 "testing" 21 ) 22 23 func TestSplitSchemeNamePort(t *testing.T) { 24 table := []struct { 25 in string 26 name, port, scheme string 27 valid bool 28 normalized bool 29 }{ 30 { 31 in: "aoeu:asdf", 32 name: "aoeu", 33 port: "asdf", 34 valid: true, 35 normalized: true, 36 }, { 37 in: "http:aoeu:asdf", 38 scheme: "http", 39 name: "aoeu", 40 port: "asdf", 41 valid: true, 42 normalized: true, 43 }, { 44 in: "https:aoeu:", 45 scheme: "https", 46 name: "aoeu", 47 port: "", 48 valid: true, 49 normalized: false, 50 }, { 51 in: "https:aoeu:asdf", 52 scheme: "https", 53 name: "aoeu", 54 port: "asdf", 55 valid: true, 56 normalized: true, 57 }, { 58 in: "aoeu:", 59 name: "aoeu", 60 valid: true, 61 normalized: false, 62 }, { 63 in: "aoeu", 64 name: "aoeu", 65 valid: true, 66 normalized: true, 67 }, { 68 in: ":asdf", 69 valid: false, 70 }, { 71 in: "aoeu:asdf:htns", 72 valid: false, 73 }, { 74 in: "http::asdf", 75 valid: false, 76 }, { 77 in: "http::", 78 valid: false, 79 }, { 80 in: "", 81 valid: false, 82 }, 83 } 84 85 for _, item := range table { 86 scheme, name, port, valid := SplitSchemeNamePort(item.in) 87 if e, a := item.scheme, scheme; e != a { 88 t.Errorf("%q: Wanted %q, got %q", item.in, e, a) 89 } 90 if e, a := item.name, name; e != a { 91 t.Errorf("%q: Wanted %q, got %q", item.in, e, a) 92 } 93 if e, a := item.port, port; e != a { 94 t.Errorf("%q: Wanted %q, got %q", item.in, e, a) 95 } 96 if e, a := item.valid, valid; e != a { 97 t.Errorf("%q: Wanted %t, got %t", item.in, e, a) 98 } 99 100 // Make sure valid items round trip through JoinSchemeNamePort 101 if item.valid { 102 out := JoinSchemeNamePort(scheme, name, port) 103 if item.normalized && out != item.in { 104 t.Errorf("%q: Wanted %s, got %s", item.in, item.in, out) 105 } 106 scheme, name, port, valid := SplitSchemeNamePort(out) 107 if e, a := item.scheme, scheme; e != a { 108 t.Errorf("%q: Wanted %q, got %q", item.in, e, a) 109 } 110 if e, a := item.name, name; e != a { 111 t.Errorf("%q: Wanted %q, got %q", item.in, e, a) 112 } 113 if e, a := item.port, port; e != a { 114 t.Errorf("%q: Wanted %q, got %q", item.in, e, a) 115 } 116 if e, a := item.valid, valid; e != a { 117 t.Errorf("%q: Wanted %t, got %t", item.in, e, a) 118 } 119 } 120 } 121 }