Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 01:00 UTC
| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package reverseproxy |
| 16 | |
| 17 | import "testing" |
| 18 | |
| 19 | func TestParseUpstreamDialAddress(t *testing.T) { |
| 20 | for i, tc := range []struct { |
| 21 | input string |
| 22 | expectHostPort string |
| 23 | expectScheme string |
| 24 | expectErr bool |
| 25 | }{ |
| 26 | { |
| 27 | input: "foo", |
| 28 | expectHostPort: "foo:80", |
| 29 | }, |
| 30 | { |
| 31 | input: "foo:1234", |
| 32 | expectHostPort: "foo:1234", |
| 33 | }, |
| 34 | { |
| 35 | input: "127.0.0.1", |
| 36 | expectHostPort: "127.0.0.1:80", |
| 37 | }, |
| 38 | { |
| 39 | input: "127.0.0.1:1234", |
| 40 | expectHostPort: "127.0.0.1:1234", |
| 41 | }, |
| 42 | { |
| 43 | input: "[::1]", |
| 44 | expectHostPort: "[::1]:80", |
| 45 | }, |
| 46 | { |
| 47 | input: "[::1]:1234", |
| 48 | expectHostPort: "[::1]:1234", |
| 49 | }, |
| 50 | { |
| 51 | input: "{foo}", |
| 52 | expectHostPort: "{foo}", |
| 53 | }, |
| 54 | { |
| 55 | input: "{foo}:80", |
| 56 | expectHostPort: "{foo}:80", |
| 57 | }, |
| 58 | { |
| 59 | input: "{foo}:{bar}", |
| 60 | expectHostPort: "{foo}:{bar}", |
| 61 | }, |
| 62 | { |
| 63 | input: "http://foo", |
| 64 | expectHostPort: "foo:80", |
| 65 | expectScheme: "http", |
| 66 | }, |
| 67 | { |
| 68 | input: "http://foo:1234", |
| 69 | expectHostPort: "foo:1234", |
| 70 | expectScheme: "http", |
| 71 | }, |
| 72 | { |
| 73 | input: "http://127.0.0.1", |
| 74 | expectHostPort: "127.0.0.1:80", |
| 75 | expectScheme: "http", |
| 76 | }, |
| 77 | { |
| 78 | input: "http://127.0.0.1:1234", |
| 79 | expectHostPort: "127.0.0.1:1234", |
| 80 | expectScheme: "http", |
| 81 | }, |
| 82 | { |
| 83 | input: "http://[::1]", |
| 84 | expectHostPort: "[::1]:80", |
| 85 | expectScheme: "http", |
| 86 | }, |
| 87 | { |
| 88 | input: "http://[::1]:80", |
| 89 | expectHostPort: "[::1]:80", |
| 90 | expectScheme: "http", |
| 91 | }, |
| 92 | { |
| 93 | input: "https://foo", |
| 94 | expectHostPort: "foo:443", |
| 95 | expectScheme: "https", |
| 96 | }, |
| 97 | { |
| 98 | input: "https://foo:1234", |
| 99 | expectHostPort: "foo:1234", |
| 100 | expectScheme: "https", |
| 101 | }, |
| 102 | { |
| 103 | input: "https://127.0.0.1", |
| 104 | expectHostPort: "127.0.0.1:443", |
| 105 | expectScheme: "https", |
| 106 | }, |
| 107 | { |
| 108 | input: "https://127.0.0.1:1234", |
| 109 | expectHostPort: "127.0.0.1:1234", |
| 110 | expectScheme: "https", |
| 111 | }, |
| 112 | { |
| 113 | input: "https://[::1]", |
| 114 | expectHostPort: "[::1]:443", |
| 115 | expectScheme: "https", |
| 116 | }, |
| 117 | { |
| 118 | input: "https://[::1]:1234", |
| 119 | expectHostPort: "[::1]:1234", |
| 120 | expectScheme: "https", |
| 121 | }, |
| 122 | { |
| 123 | input: "h2c://foo", |
| 124 | expectHostPort: "foo:80", |
| 125 | expectScheme: "h2c", |
| 126 | }, |
| 127 | { |
| 128 | input: "h2c://foo:1234", |
| 129 | expectHostPort: "foo:1234", |
| 130 | expectScheme: "h2c", |
| 131 | }, |
| 132 | { |
| 133 | input: "h2c://127.0.0.1", |
| 134 | expectHostPort: "127.0.0.1:80", |
| 135 | expectScheme: "h2c", |
| 136 | }, |
| 137 | { |
| 138 | input: "h2c://127.0.0.1:1234", |
| 139 | expectHostPort: "127.0.0.1:1234", |
| 140 | expectScheme: "h2c", |
| 141 | }, |
| 142 | { |
| 143 | input: "h2c://[::1]", |
| 144 | expectHostPort: "[::1]:80", |
| 145 | expectScheme: "h2c", |
| 146 | }, |
| 147 | { |
| 148 | input: "h2c://[::1]:1234", |
| 149 | expectHostPort: "[::1]:1234", |
| 150 | expectScheme: "h2c", |
| 151 | }, |
| 152 | { |
| 153 | input: "localhost:1001-1009", |
| 154 | expectHostPort: "localhost:1001-1009", |
| 155 | }, |
| 156 | { |
| 157 | input: "{host}:1001-1009", |
| 158 | expectHostPort: "{host}:1001-1009", |
| 159 | }, |
| 160 | { |
| 161 | input: "http://localhost:1001-1009", |
| 162 | expectHostPort: "localhost:1001-1009", |
| 163 | expectScheme: "http", |
| 164 | }, |
| 165 | { |
| 166 | input: "https://localhost:1001-1009", |
| 167 | expectHostPort: "localhost:1001-1009", |
| 168 | expectScheme: "https", |
| 169 | }, |
| 170 | { |
| 171 | input: "unix//var/php.sock", |
| 172 | expectHostPort: "unix//var/php.sock", |
| 173 | }, |
| 174 | { |
| 175 | input: "unix+h2c//var/grpc.sock", |
| 176 | expectHostPort: "unix//var/grpc.sock", |
| 177 | expectScheme: "h2c", |
| 178 | }, |
| 179 | { |
| 180 | input: "unix/{foo}", |
| 181 | expectHostPort: "unix/{foo}", |
| 182 | }, |
| 183 | { |
| 184 | input: "unix+h2c/{foo}", |
| 185 | expectHostPort: "unix/{foo}", |
| 186 | expectScheme: "h2c", |
| 187 | }, |
| 188 | { |
| 189 | input: "unix//foo/{foo}/bar", |
| 190 | expectHostPort: "unix//foo/{foo}/bar", |
| 191 | }, |
| 192 | { |
| 193 | input: "unix+h2c//foo/{foo}/bar", |
| 194 | expectHostPort: "unix//foo/{foo}/bar", |
| 195 | expectScheme: "h2c", |
| 196 | }, |
| 197 | { |
| 198 | input: "http://{foo}", |
| 199 | expectErr: true, |
| 200 | }, |
| 201 | { |
| 202 | input: "http:// :80", |
| 203 | expectErr: true, |
| 204 | }, |
| 205 | { |
| 206 | input: "http://localhost/path", |
| 207 | expectErr: true, |
| 208 | }, |
| 209 | { |
| 210 | input: "http://localhost?key=value", |
| 211 | expectErr: true, |
| 212 | }, |
| 213 | { |
| 214 | input: "http://localhost#fragment", |
| 215 | expectErr: true, |
| 216 | }, |
| 217 | { |
| 218 | input: "http://localhost:8001-8002-8003", |
| 219 | expectErr: true, |
| 220 | }, |
| 221 | { |
| 222 | input: "http://localhost:8001-8002/foo:bar", |
| 223 | expectErr: true, |
| 224 | }, |
| 225 | { |
| 226 | input: "http://localhost:8001-8002/foo:1", |
| 227 | expectErr: true, |
| 228 | }, |
| 229 | { |
| 230 | input: "http://localhost:8001-8002/foo:1-2", |
| 231 | expectErr: true, |
| 232 | }, |
| 233 | { |
| 234 | input: "http://localhost:8001-8002#foo:1", |
| 235 | expectErr: true, |
| 236 | }, |
| 237 | { |
| 238 | input: "http://foo:443", |
| 239 | expectErr: true, |
| 240 | }, |
| 241 | { |
| 242 | input: "https://foo:80", |
| 243 | expectErr: true, |
| 244 | }, |
| 245 | { |
| 246 | input: "h2c://foo:443", |
| 247 | expectErr: true, |
| 248 | }, |
| 249 | { |
| 250 | input: `unix/c:\absolute\path`, |
| 251 | expectHostPort: `unix/c:\absolute\path`, |
| 252 | }, |
| 253 | { |
| 254 | input: `unix+h2c/c:\absolute\path`, |
| 255 | expectHostPort: `unix/c:\absolute\path`, |
| 256 | expectScheme: "h2c", |
| 257 | }, |
| 258 | { |
| 259 | input: "unix/c:/absolute/path", |
| 260 | expectHostPort: "unix/c:/absolute/path", |
| 261 | }, |
| 262 | { |
| 263 | input: "unix+h2c/c:/absolute/path", |
| 264 | expectHostPort: "unix/c:/absolute/path", |
| 265 | expectScheme: "h2c", |
| 266 | }, |
| 267 | } { |
| 268 | actualAddr, err := parseUpstreamDialAddress(tc.input) |
| 269 | if tc.expectErr && err == nil { |
| 270 | t.Errorf("Test %d: Expected error but got %v", i, err) |
| 271 | } |
| 272 | if !tc.expectErr && err != nil { |
| 273 | t.Errorf("Test %d: Expected no error but got %v", i, err) |
| 274 | } |
| 275 | if actualAddr.dialAddr() != tc.expectHostPort { |
| 276 | t.Errorf("Test %d: input %s: Expected host and port '%s' but got '%s'", i, tc.input, tc.expectHostPort, actualAddr.dialAddr()) |
| 277 | } |
| 278 | if actualAddr.scheme != tc.expectScheme { |
| 279 | t.Errorf("Test %d: Expected scheme '%s' but got '%s'", i, tc.expectScheme, actualAddr.scheme) |
| 280 | } |
| 281 | } |
| 282 | } |