Debian Codegraph @

caddyconfig/httpcaddyfile/addresses_test.go

raw symbols SCIP
1 package httpcaddyfile
2
3 import (
4 "testing"
5 )
6
7 func TestParseAddress(t *testing.T) {
8 for i, test := range []struct {
9 input string
10 scheme, host, port, path string
11 shouldErr bool
12 }{
13 {``, "", "", "", "", false},
14 {`localhost`, "", "localhost", "", "", false},
15 {`localhost:1234`, "", "localhost", "1234", "", false},
16 {`localhost:`, "", "localhost", "", "", false},
17 {`0.0.0.0`, "", "0.0.0.0", "", "", false},
18 {`127.0.0.1:1234`, "", "127.0.0.1", "1234", "", false},
19 {`:1234`, "", "", "1234", "", false},
20 {`[::1]`, "", "::1", "", "", false},
21 {`[::1]:1234`, "", "::1", "1234", "", false},
22 {`:`, "", "", "", "", false},
23 {`:http`, "", "", "", "", true},
24 {`:https`, "", "", "", "", true},
25 {`localhost:http`, "", "", "", "", true}, // using service name in port is verboten, as of Go 1.12.8
26 {`localhost:https`, "", "", "", "", true},
27 {`http://localhost:https`, "", "", "", "", true}, // conflict
28 {`http://localhost:http`, "", "", "", "", true}, // repeated scheme
29 {`host:https/path`, "", "", "", "", true},
30 {`http://localhost:443`, "http", "localhost", "443", "", false}, // NOTE: not conventional
31 {`https://localhost:80`, "https", "localhost", "80", "", false}, // NOTE: not conventional
32 {`http://localhost`, "http", "localhost", "", "", false},
33 {`https://localhost`, "https", "localhost", "", "", false},
34 {`http://{env.APP_DOMAIN}`, "http", "{env.APP_DOMAIN}", "", "", false},
35 {`{env.APP_DOMAIN}:80`, "", "{env.APP_DOMAIN}", "80", "", false},
36 {`{env.APP_DOMAIN}/path`, "", "{env.APP_DOMAIN}", "", "/path", false},
37 {`example.com/{env.APP_PATH}`, "", "example.com", "", "/{env.APP_PATH}", false},
38 {`http://127.0.0.1`, "http", "127.0.0.1", "", "", false},
39 {`https://127.0.0.1`, "https", "127.0.0.1", "", "", false},
40 {`http://[::1]`, "http", "::1", "", "", false},
41 {`http://localhost:1234`, "http", "localhost", "1234", "", false},
42 {`https://127.0.0.1:1234`, "https", "127.0.0.1", "1234", "", false},
43 {`http://[::1]:1234`, "http", "::1", "1234", "", false},
44 {``, "", "", "", "", false},
45 {`::1`, "", "::1", "", "", false},
46 {`localhost::`, "", "localhost::", "", "", false},
47 {`#$%@`, "", "#$%@", "", "", false}, // don't want to presume what the hostname could be
48 {`host/path`, "", "host", "", "/path", false},
49 {`http://host/`, "http", "host", "", "/", false},
50 {`//asdf`, "", "", "", "//asdf", false},
51 {`:1234/asdf`, "", "", "1234", "/asdf", false},
52 {`http://host/path`, "http", "host", "", "/path", false},
53 {`https://host:443/path/foo`, "https", "host", "443", "/path/foo", false},
54 {`host:80/path`, "", "host", "80", "/path", false},
55 {`/path`, "", "", "", "/path", false},
56 } {
57 actual, err := ParseAddress(test.input)
58
59 if err != nil && !test.shouldErr {
60 t.Errorf("Test %d (%s): Expected no error, but had error: %v", i, test.input, err)
61 }
62 if err == nil && test.shouldErr {
63 t.Errorf("Test %d (%s): Expected error, but had none (%#v)", i, test.input, actual)
64 }
65
66 if !test.shouldErr && actual.Original != test.input {
67 t.Errorf("Test %d (%s): Expected original '%s', got '%s'", i, test.input, test.input, actual.Original)
68 }
69 if actual.Scheme != test.scheme {
70 t.Errorf("Test %d (%s): Expected scheme '%s', got '%s'", i, test.input, test.scheme, actual.Scheme)
71 }
72 if actual.Host != test.host {
73 t.Errorf("Test %d (%s): Expected host '%s', got '%s'", i, test.input, test.host, actual.Host)
74 }
75 if actual.Port != test.port {
76 t.Errorf("Test %d (%s): Expected port '%s', got '%s'", i, test.input, test.port, actual.Port)
77 }
78 if actual.Path != test.path {
79 t.Errorf("Test %d (%s): Expected path '%s', got '%s'", i, test.input, test.path, actual.Path)
80 }
81 }
82 }
83
84 func TestAddressString(t *testing.T) {
85 for i, test := range []struct {
86 addr Address
87 expected string
88 }{
89 {Address{Scheme: "http", Host: "host", Port: "1234", Path: "/path"}, "http://host:1234/path"},
90 {Address{Scheme: "", Host: "host", Port: "", Path: ""}, "http://host"},
91 {Address{Scheme: "", Host: "host", Port: "80", Path: ""}, "http://host"},
92 {Address{Scheme: "", Host: "host", Port: "443", Path: ""}, "https://host"},
93 {Address{Scheme: "https", Host: "host", Port: "443", Path: ""}, "https://host"},
94 {Address{Scheme: "https", Host: "host", Port: "", Path: ""}, "https://host"},
95 {Address{Scheme: "", Host: "host", Port: "80", Path: "/path"}, "http://host/path"},
96 {Address{Scheme: "http", Host: "", Port: "1234", Path: ""}, "http://:1234"},
97 {Address{Scheme: "", Host: "", Port: "", Path: ""}, ""},
98 } {
99 actual := test.addr.String()
100 if actual != test.expected {
101 t.Errorf("Test %d: expected '%s' but got '%s'", i, test.expected, actual)
102 }
103 }
104 }
105
106 func TestKeyNormalization(t *testing.T) {
107 testCases := []struct {
108 input string
109 expect Address
110 }{
111 {
112 input: "example.com",
113 expect: Address{
114 Host: "example.com",
115 },
116 },
117 {
118 input: "http://host:1234/path",
119 expect: Address{
120 Scheme: "http",
121 Host: "host",
122 Port: "1234",
123 Path: "/path",
124 },
125 },
126 {
127 input: "HTTP://A/ABCDEF",
128 expect: Address{
129 Scheme: "http",
130 Host: "a",
131 Path: "/ABCDEF",
132 },
133 },
134 {
135 input: "A/ABCDEF",
136 expect: Address{
137 Host: "a",
138 Path: "/ABCDEF",
139 },
140 },
141 {
142 input: "A:2015/Path",
143 expect: Address{
144 Host: "a",
145 Port: "2015",
146 Path: "/Path",
147 },
148 },
149 {
150 input: "sub.{env.MY_DOMAIN}",
151 expect: Address{
152 Host: "sub.{env.MY_DOMAIN}",
153 },
154 },
155 {
156 input: "sub.ExAmPle",
157 expect: Address{
158 Host: "sub.example",
159 },
160 },
161 {
162 input: "sub.\\{env.MY_DOMAIN\\}",
163 expect: Address{
164 Host: "sub.\\{env.my_domain\\}",
165 },
166 },
167 {
168 input: "sub.{env.MY_DOMAIN}.com",
169 expect: Address{
170 Host: "sub.{env.MY_DOMAIN}.com",
171 },
172 },
173 {
174 input: ":80",
175 expect: Address{
176 Port: "80",
177 },
178 },
179 {
180 input: ":443",
181 expect: Address{
182 Port: "443",
183 },
184 },
185 {
186 input: ":1234",
187 expect: Address{
188 Port: "1234",
189 },
190 },
191 {
192 input: "",
193 expect: Address{},
194 },
195 {
196 input: ":",
197 expect: Address{},
198 },
199 {
200 input: "[::]",
201 expect: Address{
202 Host: "::",
203 },
204 },
205 {
206 input: "127.0.0.1",
207 expect: Address{
208 Host: "127.0.0.1",
209 },
210 },
211 {
212 input: "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:1234",
213 expect: Address{
214 Host: "2001:db8:85a3:8d3:1319:8a2e:370:7348",
215 Port: "1234",
216 },
217 },
218 {
219 // IPv4 address in IPv6 form (#4381)
220 input: "[::ffff:cff4:e77d]:1234",
221 expect: Address{
222 Host: "::ffff:cff4:e77d",
223 Port: "1234",
224 },
225 },
226 {
227 input: "::ffff:cff4:e77d",
228 expect: Address{
229 Host: "::ffff:cff4:e77d",
230 },
231 },
232 }
233 for i, tc := range testCases {
234 addr, err := ParseAddress(tc.input)
235 if err != nil {
236 t.Errorf("Test %d: Parsing address '%s': %v", i, tc.input, err)
237 continue
238 }
239 actual := addr.Normalize()
240 if actual.Scheme != tc.expect.Scheme {
241 t.Errorf("Test %d: Input '%s': Expected Scheme='%s' but got Scheme='%s'", i, tc.input, tc.expect.Scheme, actual.Scheme)
242 }
243 if actual.Host != tc.expect.Host {
244 t.Errorf("Test %d: Input '%s': Expected Host='%s' but got Host='%s'", i, tc.input, tc.expect.Host, actual.Host)
245 }
246 if actual.Port != tc.expect.Port {
247 t.Errorf("Test %d: Input '%s': Expected Port='%s' but got Port='%s'", i, tc.input, tc.expect.Port, actual.Port)
248 }
249 if actual.Path != tc.expect.Path {
250 t.Errorf("Test %d: Input '%s': Expected Path='%s' but got Path='%s'", i, tc.input, tc.expect.Path, actual.Path)
251 }
252 }
253 }