diff options
| author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2026-08-30 15:34:15 +0200 |
|---|---|---|
| committer | Thijs Schreijer <thijs@thijsschreijer.nl> | 2026-08-30 15:34:15 +0200 |
| commit | 6fe110be47c0fc34bc9dce6d377ce9b48ab27c2e (patch) | |
| tree | 15eabb11745033d916e61c565ea4205bae84d0fe /test/urltest.lua | |
| parent | 84c2bb905bda5f1fc58195171c75723b5fec6e87 (diff) | |
| download | luasocket-6fe110be47c0fc34bc9dce6d377ce9b48ab27c2e.tar.gz luasocket-6fe110be47c0fc34bc9dce6d377ce9b48ab27c2e.tar.bz2 luasocket-6fe110be47c0fc34bc9dce6d377ce9b48ab27c2e.zip | |
feat(url): classify host as name/ipv4/ipv6 in parse and build
parse() now sets hosttype ("name"/"ipv4"/"ipv6") plus a matching
hostname/ipv4/ipv6 field alongside host. build() accepts those same
fields when host is absent, erroring if more than one is set.
classify_host is exported for standalone use; it classifies by shape
(colon present -> ipv6, dotted-quad shape -> ipv4) rather than
validating the address.
Diffstat (limited to 'test/urltest.lua')
| -rw-r--r-- | test/urltest.lua | 196 |
1 files changed, 196 insertions, 0 deletions
diff --git a/test/urltest.lua b/test/urltest.lua index 9a3c470..8c50f50 100644 --- a/test/urltest.lua +++ b/test/urltest.lua | |||
| @@ -95,6 +95,8 @@ check_parse_url{ | |||
| 95 | scheme = "scheme", | 95 | scheme = "scheme", |
| 96 | authority = "user:pass$%?#wd@host:port", | 96 | authority = "user:pass$%?#wd@host:port", |
| 97 | host = "host", | 97 | host = "host", |
| 98 | hosttype = "name", | ||
| 99 | hostname = "host", | ||
| 98 | port = "port", | 100 | port = "port", |
| 99 | userinfo = "user:pass$%?#wd", | 101 | userinfo = "user:pass$%?#wd", |
| 100 | password = "pass$%?#wd", | 102 | password = "pass$%?#wd", |
| @@ -109,6 +111,8 @@ check_parse_url{ | |||
| 109 | scheme = "scheme", | 111 | scheme = "scheme", |
| 110 | authority = "user:pass?#wd@host:port", | 112 | authority = "user:pass?#wd@host:port", |
| 111 | host = "host", | 113 | host = "host", |
| 114 | hosttype = "name", | ||
| 115 | hostname = "host", | ||
| 112 | port = "port", | 116 | port = "port", |
| 113 | userinfo = "user:pass?#wd", | 117 | userinfo = "user:pass?#wd", |
| 114 | password = "pass?#wd", | 118 | password = "pass?#wd", |
| @@ -123,6 +127,8 @@ check_parse_url{ | |||
| 123 | scheme = "scheme", | 127 | scheme = "scheme", |
| 124 | authority = "user:pass-wd@host:port", | 128 | authority = "user:pass-wd@host:port", |
| 125 | host = "host", | 129 | host = "host", |
| 130 | hosttype = "name", | ||
| 131 | hostname = "host", | ||
| 126 | port = "port", | 132 | port = "port", |
| 127 | userinfo = "user:pass-wd", | 133 | userinfo = "user:pass-wd", |
| 128 | password = "pass-wd", | 134 | password = "pass-wd", |
| @@ -137,6 +143,8 @@ check_parse_url{ | |||
| 137 | scheme = "scheme", | 143 | scheme = "scheme", |
| 138 | authority = "user:pass#wd@host:port", | 144 | authority = "user:pass#wd@host:port", |
| 139 | host = "host", | 145 | host = "host", |
| 146 | hosttype = "name", | ||
| 147 | hostname = "host", | ||
| 140 | port = "port", | 148 | port = "port", |
| 141 | userinfo = "user:pass#wd", | 149 | userinfo = "user:pass#wd", |
| 142 | password = "pass#wd", | 150 | password = "pass#wd", |
| @@ -151,6 +159,8 @@ check_parse_url{ | |||
| 151 | scheme = "scheme", | 159 | scheme = "scheme", |
| 152 | authority = "user:pass#wd@host:port", | 160 | authority = "user:pass#wd@host:port", |
| 153 | host = "host", | 161 | host = "host", |
| 162 | hosttype = "name", | ||
| 163 | hostname = "host", | ||
| 154 | port = "port", | 164 | port = "port", |
| 155 | userinfo = "user:pass#wd", | 165 | userinfo = "user:pass#wd", |
| 156 | password = "pass#wd", | 166 | password = "pass#wd", |
| @@ -164,6 +174,8 @@ check_parse_url{ | |||
| 164 | scheme = "scheme", | 174 | scheme = "scheme", |
| 165 | authority = "userinfo@host:port", | 175 | authority = "userinfo@host:port", |
| 166 | host = "host", | 176 | host = "host", |
| 177 | hosttype = "name", | ||
| 178 | hostname = "host", | ||
| 167 | port = "port", | 179 | port = "port", |
| 168 | userinfo = "userinfo", | 180 | userinfo = "userinfo", |
| 169 | user = "userinfo", | 181 | user = "userinfo", |
| @@ -178,6 +190,8 @@ check_parse_url{ | |||
| 178 | scheme = "scheme", | 190 | scheme = "scheme", |
| 179 | authority = "user:password@host:port", | 191 | authority = "user:password@host:port", |
| 180 | host = "host", | 192 | host = "host", |
| 193 | hosttype = "name", | ||
| 194 | hostname = "host", | ||
| 181 | port = "port", | 195 | port = "port", |
| 182 | userinfo = "user:password", | 196 | userinfo = "user:password", |
| 183 | user = "user", | 197 | user = "user", |
| @@ -193,6 +207,8 @@ check_parse_url{ | |||
| 193 | scheme = "scheme", | 207 | scheme = "scheme", |
| 194 | authority = "userinfo@host:port", | 208 | authority = "userinfo@host:port", |
| 195 | host = "host", | 209 | host = "host", |
| 210 | hosttype = "name", | ||
| 211 | hostname = "host", | ||
| 196 | port = "port", | 212 | port = "port", |
| 197 | userinfo = "userinfo", | 213 | userinfo = "userinfo", |
| 198 | user = "userinfo", | 214 | user = "userinfo", |
| @@ -207,6 +223,8 @@ check_parse_url{ | |||
| 207 | scheme = "scheme", | 223 | scheme = "scheme", |
| 208 | authority = "userinfo@host:port", | 224 | authority = "userinfo@host:port", |
| 209 | host = "host", | 225 | host = "host", |
| 226 | hosttype = "name", | ||
| 227 | hostname = "host", | ||
| 210 | port = "port", | 228 | port = "port", |
| 211 | userinfo = "userinfo", | 229 | userinfo = "userinfo", |
| 212 | user = "userinfo", | 230 | user = "userinfo", |
| @@ -221,6 +239,8 @@ check_parse_url{ | |||
| 221 | scheme = "scheme", | 239 | scheme = "scheme", |
| 222 | authority = "userinfo@host:port", | 240 | authority = "userinfo@host:port", |
| 223 | host = "host", | 241 | host = "host", |
| 242 | hosttype = "name", | ||
| 243 | hostname = "host", | ||
| 224 | port = "port", | 244 | port = "port", |
| 225 | userinfo = "userinfo", | 245 | userinfo = "userinfo", |
| 226 | user = "userinfo", | 246 | user = "userinfo", |
| @@ -234,6 +254,8 @@ check_parse_url{ | |||
| 234 | scheme = "scheme", | 254 | scheme = "scheme", |
| 235 | authority = "userinfo@host:port", | 255 | authority = "userinfo@host:port", |
| 236 | host = "host", | 256 | host = "host", |
| 257 | hosttype = "name", | ||
| 258 | hostname = "host", | ||
| 237 | port = "port", | 259 | port = "port", |
| 238 | userinfo = "userinfo", | 260 | userinfo = "userinfo", |
| 239 | user = "userinfo", | 261 | user = "userinfo", |
| @@ -248,6 +270,8 @@ check_parse_url{ | |||
| 248 | scheme = "scheme", | 270 | scheme = "scheme", |
| 249 | authority = "userinfo@host:port", | 271 | authority = "userinfo@host:port", |
| 250 | host = "host", | 272 | host = "host", |
| 273 | hosttype = "name", | ||
| 274 | hostname = "host", | ||
| 251 | port = "port", | 275 | port = "port", |
| 252 | userinfo = "userinfo", | 276 | userinfo = "userinfo", |
| 253 | user = "userinfo", | 277 | user = "userinfo", |
| @@ -261,6 +285,8 @@ check_parse_url{ | |||
| 261 | scheme = "scheme", | 285 | scheme = "scheme", |
| 262 | authority = "userinfo@host:port", | 286 | authority = "userinfo@host:port", |
| 263 | host = "host", | 287 | host = "host", |
| 288 | hosttype = "name", | ||
| 289 | hostname = "host", | ||
| 264 | port = "port", | 290 | port = "port", |
| 265 | userinfo = "userinfo", | 291 | userinfo = "userinfo", |
| 266 | user = "userinfo", | 292 | user = "userinfo", |
| @@ -275,6 +301,8 @@ check_parse_url{ | |||
| 275 | scheme = "scheme", | 301 | scheme = "scheme", |
| 276 | authority = "userinfo@host:port", | 302 | authority = "userinfo@host:port", |
| 277 | host = "host", | 303 | host = "host", |
| 304 | hosttype = "name", | ||
| 305 | hostname = "host", | ||
| 278 | port = "port", | 306 | port = "port", |
| 279 | userinfo = "userinfo", | 307 | userinfo = "userinfo", |
| 280 | user = "userinfo", | 308 | user = "userinfo", |
| @@ -284,6 +312,8 @@ check_parse_url{ | |||
| 284 | url = "//userinfo@host:port/path;params?query#fragment", | 312 | url = "//userinfo@host:port/path;params?query#fragment", |
| 285 | authority = "userinfo@host:port", | 313 | authority = "userinfo@host:port", |
| 286 | host = "host", | 314 | host = "host", |
| 315 | hosttype = "name", | ||
| 316 | hostname = "host", | ||
| 287 | port = "port", | 317 | port = "port", |
| 288 | userinfo = "userinfo", | 318 | userinfo = "userinfo", |
| 289 | user = "userinfo", | 319 | user = "userinfo", |
| @@ -297,6 +327,8 @@ check_parse_url{ | |||
| 297 | url = "//userinfo@host:port/path", | 327 | url = "//userinfo@host:port/path", |
| 298 | authority = "userinfo@host:port", | 328 | authority = "userinfo@host:port", |
| 299 | host = "host", | 329 | host = "host", |
| 330 | hosttype = "name", | ||
| 331 | hostname = "host", | ||
| 300 | port = "port", | 332 | port = "port", |
| 301 | userinfo = "userinfo", | 333 | userinfo = "userinfo", |
| 302 | user = "userinfo", | 334 | user = "userinfo", |
| @@ -307,6 +339,8 @@ check_parse_url{ | |||
| 307 | url = "//userinfo@host/path", | 339 | url = "//userinfo@host/path", |
| 308 | authority = "userinfo@host", | 340 | authority = "userinfo@host", |
| 309 | host = "host", | 341 | host = "host", |
| 342 | hosttype = "name", | ||
| 343 | hostname = "host", | ||
| 310 | userinfo = "userinfo", | 344 | userinfo = "userinfo", |
| 311 | user = "userinfo", | 345 | user = "userinfo", |
| 312 | path = "/path", | 346 | path = "/path", |
| @@ -316,6 +350,8 @@ check_parse_url{ | |||
| 316 | url = "//user:password@host/path", | 350 | url = "//user:password@host/path", |
| 317 | authority = "user:password@host", | 351 | authority = "user:password@host", |
| 318 | host = "host", | 352 | host = "host", |
| 353 | hosttype = "name", | ||
| 354 | hostname = "host", | ||
| 319 | userinfo = "user:password", | 355 | userinfo = "user:password", |
| 320 | password = "password", | 356 | password = "password", |
| 321 | user = "user", | 357 | user = "user", |
| @@ -326,6 +362,8 @@ check_parse_url{ | |||
| 326 | url = "//user:@host/path", | 362 | url = "//user:@host/path", |
| 327 | authority = "user:@host", | 363 | authority = "user:@host", |
| 328 | host = "host", | 364 | host = "host", |
| 365 | hosttype = "name", | ||
| 366 | hostname = "host", | ||
| 329 | userinfo = "user:", | 367 | userinfo = "user:", |
| 330 | password = "", | 368 | password = "", |
| 331 | user = "user", | 369 | user = "user", |
| @@ -336,6 +374,8 @@ check_parse_url{ | |||
| 336 | url = "//user@host:port/path", | 374 | url = "//user@host:port/path", |
| 337 | authority = "user@host:port", | 375 | authority = "user@host:port", |
| 338 | host = "host", | 376 | host = "host", |
| 377 | hosttype = "name", | ||
| 378 | hostname = "host", | ||
| 339 | userinfo = "user", | 379 | userinfo = "user", |
| 340 | user = "user", | 380 | user = "user", |
| 341 | port = "port", | 381 | port = "port", |
| @@ -347,6 +387,8 @@ check_parse_url{ | |||
| 347 | authority = "host:port", | 387 | authority = "host:port", |
| 348 | port = "port", | 388 | port = "port", |
| 349 | host = "host", | 389 | host = "host", |
| 390 | hosttype = "name", | ||
| 391 | hostname = "host", | ||
| 350 | path = "/path", | 392 | path = "/path", |
| 351 | } | 393 | } |
| 352 | 394 | ||
| @@ -354,6 +396,8 @@ check_parse_url{ | |||
| 354 | url = "//host/path", | 396 | url = "//host/path", |
| 355 | authority = "host", | 397 | authority = "host", |
| 356 | host = "host", | 398 | host = "host", |
| 399 | hosttype = "name", | ||
| 400 | hostname = "host", | ||
| 357 | path = "/path", | 401 | path = "/path", |
| 358 | } | 402 | } |
| 359 | 403 | ||
| @@ -361,6 +405,8 @@ check_parse_url{ | |||
| 361 | url = "//host", | 405 | url = "//host", |
| 362 | authority = "host", | 406 | authority = "host", |
| 363 | host = "host", | 407 | host = "host", |
| 408 | hosttype = "name", | ||
| 409 | hostname = "host", | ||
| 364 | } | 410 | } |
| 365 | 411 | ||
| 366 | check_parse_url{ | 412 | check_parse_url{ |
| @@ -379,6 +425,8 @@ check_parse_url{ | |||
| 379 | url = "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html", | 425 | url = "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html", |
| 380 | scheme = "http", | 426 | scheme = "http", |
| 381 | host = "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210", | 427 | host = "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210", |
| 428 | hosttype = "ipv6", | ||
| 429 | ipv6 = "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210", | ||
| 382 | authority = "[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80", | 430 | authority = "[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80", |
| 383 | port = "80", | 431 | port = "80", |
| 384 | path = "/index.html" | 432 | path = "/index.html" |
| @@ -388,6 +436,8 @@ check_parse_url{ | |||
| 388 | url = "http://[1080:0:0:0:8:800:200C:417A]/index.html", | 436 | url = "http://[1080:0:0:0:8:800:200C:417A]/index.html", |
| 389 | scheme = "http", | 437 | scheme = "http", |
| 390 | host = "1080:0:0:0:8:800:200C:417A", | 438 | host = "1080:0:0:0:8:800:200C:417A", |
| 439 | hosttype = "ipv6", | ||
| 440 | ipv6 = "1080:0:0:0:8:800:200C:417A", | ||
| 391 | authority = "[1080:0:0:0:8:800:200C:417A]", | 441 | authority = "[1080:0:0:0:8:800:200C:417A]", |
| 392 | path = "/index.html" | 442 | path = "/index.html" |
| 393 | } | 443 | } |
| @@ -396,6 +446,8 @@ check_parse_url{ | |||
| 396 | url = "http://[3ffe:2a00:100:7031::1]", | 446 | url = "http://[3ffe:2a00:100:7031::1]", |
| 397 | scheme = "http", | 447 | scheme = "http", |
| 398 | host = "3ffe:2a00:100:7031::1", | 448 | host = "3ffe:2a00:100:7031::1", |
| 449 | hosttype = "ipv6", | ||
| 450 | ipv6 = "3ffe:2a00:100:7031::1", | ||
| 399 | authority = "[3ffe:2a00:100:7031::1]", | 451 | authority = "[3ffe:2a00:100:7031::1]", |
| 400 | } | 452 | } |
| 401 | 453 | ||
| @@ -403,6 +455,8 @@ check_parse_url{ | |||
| 403 | url = "http://[1080::8:800:200C:417A]/foo", | 455 | url = "http://[1080::8:800:200C:417A]/foo", |
| 404 | scheme = "http", | 456 | scheme = "http", |
| 405 | host = "1080::8:800:200C:417A", | 457 | host = "1080::8:800:200C:417A", |
| 458 | hosttype = "ipv6", | ||
| 459 | ipv6 = "1080::8:800:200C:417A", | ||
| 406 | authority = "[1080::8:800:200C:417A]", | 460 | authority = "[1080::8:800:200C:417A]", |
| 407 | path = "/foo" | 461 | path = "/foo" |
| 408 | } | 462 | } |
| @@ -411,6 +465,8 @@ check_parse_url{ | |||
| 411 | url = "http://[::192.9.5.5]/ipng", | 465 | url = "http://[::192.9.5.5]/ipng", |
| 412 | scheme = "http", | 466 | scheme = "http", |
| 413 | host = "::192.9.5.5", | 467 | host = "::192.9.5.5", |
| 468 | hosttype = "ipv6", | ||
| 469 | ipv6 = "::192.9.5.5", | ||
| 414 | authority = "[::192.9.5.5]", | 470 | authority = "[::192.9.5.5]", |
| 415 | path = "/ipng" | 471 | path = "/ipng" |
| 416 | } | 472 | } |
| @@ -419,6 +475,8 @@ check_parse_url{ | |||
| 419 | url = "http://[::FFFF:129.144.52.38]:80/index.html", | 475 | url = "http://[::FFFF:129.144.52.38]:80/index.html", |
| 420 | scheme = "http", | 476 | scheme = "http", |
| 421 | host = "::FFFF:129.144.52.38", | 477 | host = "::FFFF:129.144.52.38", |
| 478 | hosttype = "ipv6", | ||
| 479 | ipv6 = "::FFFF:129.144.52.38", | ||
| 422 | port = "80", | 480 | port = "80", |
| 423 | authority = "[::FFFF:129.144.52.38]:80", | 481 | authority = "[::FFFF:129.144.52.38]:80", |
| 424 | path = "/index.html" | 482 | path = "/index.html" |
| @@ -428,6 +486,8 @@ check_parse_url{ | |||
| 428 | url = "http://[2010:836B:4179::836B:4179]", | 486 | url = "http://[2010:836B:4179::836B:4179]", |
| 429 | scheme = "http", | 487 | scheme = "http", |
| 430 | host = "2010:836B:4179::836B:4179", | 488 | host = "2010:836B:4179::836B:4179", |
| 489 | hosttype = "ipv6", | ||
| 490 | ipv6 = "2010:836B:4179::836B:4179", | ||
| 431 | authority = "[2010:836B:4179::836B:4179]", | 491 | authority = "[2010:836B:4179::836B:4179]", |
| 432 | } | 492 | } |
| 433 | 493 | ||
| @@ -435,6 +495,8 @@ check_parse_url{ | |||
| 435 | url = "//userinfo@[::FFFF:129.144.52.38]:port/path;params?query#fragment", | 495 | url = "//userinfo@[::FFFF:129.144.52.38]:port/path;params?query#fragment", |
| 436 | authority = "userinfo@[::FFFF:129.144.52.38]:port", | 496 | authority = "userinfo@[::FFFF:129.144.52.38]:port", |
| 437 | host = "::FFFF:129.144.52.38", | 497 | host = "::FFFF:129.144.52.38", |
| 498 | hosttype = "ipv6", | ||
| 499 | ipv6 = "::FFFF:129.144.52.38", | ||
| 438 | port = "port", | 500 | port = "port", |
| 439 | userinfo = "userinfo", | 501 | userinfo = "userinfo", |
| 440 | user = "userinfo", | 502 | user = "userinfo", |
| @@ -449,6 +511,8 @@ check_parse_url{ | |||
| 449 | scheme = "scheme", | 511 | scheme = "scheme", |
| 450 | authority = "user:password@[::192.9.5.5]:port", | 512 | authority = "user:password@[::192.9.5.5]:port", |
| 451 | host = "::192.9.5.5", | 513 | host = "::192.9.5.5", |
| 514 | hosttype = "ipv6", | ||
| 515 | ipv6 = "::192.9.5.5", | ||
| 452 | port = "port", | 516 | port = "port", |
| 453 | userinfo = "user:password", | 517 | userinfo = "user:password", |
| 454 | user = "user", | 518 | user = "user", |
| @@ -459,6 +523,138 @@ check_parse_url{ | |||
| 459 | fragment = "fragment" | 523 | fragment = "fragment" |
| 460 | } | 524 | } |
| 461 | 525 | ||
| 526 | print("testing host classification (hosttype/hostname/ipv4/ipv6)") | ||
| 527 | check_parse_url{ | ||
| 528 | url = "http://example.com/path", | ||
| 529 | scheme = "http", | ||
| 530 | authority = "example.com", | ||
| 531 | host = "example.com", | ||
| 532 | hosttype = "name", | ||
| 533 | hostname = "example.com", | ||
| 534 | path = "/path", | ||
| 535 | } | ||
| 536 | |||
| 537 | check_parse_url{ | ||
| 538 | url = "http://192.168.1.1:8080/path", | ||
| 539 | scheme = "http", | ||
| 540 | authority = "192.168.1.1:8080", | ||
| 541 | host = "192.168.1.1", | ||
| 542 | hosttype = "ipv4", | ||
| 543 | ipv4 = "192.168.1.1", | ||
| 544 | port = "8080", | ||
| 545 | path = "/path", | ||
| 546 | } | ||
| 547 | |||
| 548 | -- octet ranges aren't validated: any dotted-quad shape is classified ipv4, | ||
| 549 | -- even with an out-of-range octet like this one | ||
| 550 | check_parse_url{ | ||
| 551 | url = "http://999.1.1.1/path", | ||
| 552 | scheme = "http", | ||
| 553 | authority = "999.1.1.1", | ||
| 554 | host = "999.1.1.1", | ||
| 555 | hosttype = "ipv4", | ||
| 556 | ipv4 = "999.1.1.1", | ||
| 557 | path = "/path", | ||
| 558 | } | ||
| 559 | |||
| 560 | check_parse_url{ | ||
| 561 | url = "http://[::1]:8080/path", | ||
| 562 | scheme = "http", | ||
| 563 | authority = "[::1]:8080", | ||
| 564 | host = "::1", | ||
| 565 | hosttype = "ipv6", | ||
| 566 | ipv6 = "::1", | ||
| 567 | port = "8080", | ||
| 568 | path = "/path", | ||
| 569 | } | ||
| 570 | |||
| 571 | check_parse_url{ | ||
| 572 | url = "http://[2010:836B:4179::836B:4179]/", | ||
| 573 | scheme = "http", | ||
| 574 | authority = "[2010:836B:4179::836B:4179]", | ||
| 575 | host = "2010:836B:4179::836B:4179", | ||
| 576 | hosttype = "ipv6", | ||
| 577 | ipv6 = "2010:836B:4179::836B:4179", | ||
| 578 | path = "/", | ||
| 579 | } | ||
| 580 | |||
| 581 | -- no authority at all: no host, no hosttype | ||
| 582 | check_parse_url{ | ||
| 583 | url = "relative/path", | ||
| 584 | path = "relative/path", | ||
| 585 | } | ||
| 586 | |||
| 587 | check_build_url{ | ||
| 588 | url = "http://example.com/path", | ||
| 589 | scheme = "http", | ||
| 590 | hostname = "example.com", | ||
| 591 | path = "/path", | ||
| 592 | } | ||
| 593 | |||
| 594 | check_build_url{ | ||
| 595 | url = "http://1.2.3.4/path", | ||
| 596 | scheme = "http", | ||
| 597 | ipv4 = "1.2.3.4", | ||
| 598 | path = "/path", | ||
| 599 | } | ||
| 600 | |||
| 601 | check_build_url{ | ||
| 602 | url = "http://[::1]/path", | ||
| 603 | scheme = "http", | ||
| 604 | ipv6 = "::1", | ||
| 605 | path = "/path", | ||
| 606 | } | ||
| 607 | |||
| 608 | -- an already-bracketed ipv6/host value must not be bracketed again | ||
| 609 | check_build_url{ | ||
| 610 | url = "http://[::1]/path", | ||
| 611 | scheme = "http", | ||
| 612 | ipv6 = "[::1]", | ||
| 613 | path = "/path", | ||
| 614 | } | ||
| 615 | |||
| 616 | check_build_url{ | ||
| 617 | url = "http://[::1]/path", | ||
| 618 | scheme = "http", | ||
| 619 | host = "[::1]", | ||
| 620 | path = "/path", | ||
| 621 | } | ||
| 622 | |||
| 623 | local check_classify_host = function(raw, expect_hosttype, expect_host) | ||
| 624 | local hosttype, host = socket.url.classify_host(raw) | ||
| 625 | if hosttype ~= expect_hosttype or host ~= expect_host then | ||
| 626 | io.write("classify_host: for '", raw, "' expected ", expect_hosttype, | ||
| 627 | " '", expect_host, "' but got ", tostring(hosttype), " '", | ||
| 628 | tostring(host), "'\n") | ||
| 629 | os.exit() | ||
| 630 | end | ||
| 631 | end | ||
| 632 | |||
| 633 | check_classify_host("example.com", "name", "example.com") | ||
| 634 | check_classify_host("999.1.1.1", "ipv4", "999.1.1.1") | ||
| 635 | check_classify_host("192.168.1.1", "ipv4", "192.168.1.1") | ||
| 636 | check_classify_host("[::1]", "ipv6", "::1") | ||
| 637 | check_classify_host("2010:836B:4179::836B:4179", "ipv6", "2010:836B:4179::836B:4179") | ||
| 638 | |||
| 639 | -- legacy 'host' field takes precedence over hostname/ipv4/ipv6 if both given | ||
| 640 | check_build_url{ | ||
| 641 | url = "http://legacy.example/path", | ||
| 642 | scheme = "http", | ||
| 643 | host = "legacy.example", | ||
| 644 | hostname = "ignored.example", | ||
| 645 | path = "/path", | ||
| 646 | } | ||
| 647 | |||
| 648 | -- ambiguous: more than one of hostname/ipv4/ipv6 set, no 'host' to disambiguate | ||
| 649 | do | ||
| 650 | local ok = pcall(socket.url.build, | ||
| 651 | {scheme = "http", ipv4 = "1.2.3.4", hostname = "example.com", path = "/path"}) | ||
| 652 | if ok then | ||
| 653 | print("build: expected error for ambiguous host, got none") | ||
| 654 | os.exit() | ||
| 655 | end | ||
| 656 | end | ||
| 657 | |||
| 462 | print("testing URL building") | 658 | print("testing URL building") |
| 463 | check_build_url { | 659 | check_build_url { |
| 464 | url = "scheme://user:password@host:port/path;params?query#fragment", | 660 | url = "scheme://user:password@host:port/path;params?query#fragment", |
