|
| 1 | +use Test::Nginx::Socket 'no_plan'; |
| 2 | +use Cwd qw(cwd); |
| 3 | + |
| 4 | +my $pwd = cwd(); |
| 5 | + |
| 6 | +$ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; |
| 7 | +$ENV{TEST_COVERAGE} ||= 0; |
| 8 | + |
| 9 | +our $HttpConfig = qq{ |
| 10 | + lua_package_path "$pwd/lib/?.lua;/usr/local/share/lua/5.1/?.lua;;"; |
| 11 | + error_log logs/error.log debug; |
| 12 | +
|
| 13 | + init_by_lua_block { |
| 14 | + if $ENV{TEST_COVERAGE} == 1 then |
| 15 | + jit.off() |
| 16 | + require("luacov.runner").init() |
| 17 | + end |
| 18 | + } |
| 19 | +}; |
| 20 | + |
| 21 | +no_long_string(); |
| 22 | +#no_diff(); |
| 23 | + |
| 24 | +run_tests(); |
| 25 | + |
| 26 | +__DATA__ |
| 27 | +=== TEST 1: Parse URI errors if malformed |
| 28 | +--- http_config eval: $::HttpConfig |
| 29 | +--- config |
| 30 | + location = /a { |
| 31 | + content_by_lua ' |
| 32 | + local http = require("resty.http").new() |
| 33 | + local parts, err = http:parse_uri("http:///example.com") |
| 34 | + if not parts then ngx.say(err) end |
| 35 | + '; |
| 36 | + } |
| 37 | +--- request |
| 38 | +GET /a |
| 39 | +--- response_body |
| 40 | +bad uri: http:///example.com |
| 41 | +--- no_error_log |
| 42 | +[error] |
| 43 | +[warn] |
| 44 | +
|
| 45 | +
|
| 46 | +=== TEST 2: Parse URI fills in defaults correctly |
| 47 | +--- http_config eval: $::HttpConfig |
| 48 | +--- config |
| 49 | + location = /a { |
| 50 | + content_by_lua ' |
| 51 | + local http = require("resty.http").new() |
| 52 | +
|
| 53 | + local function test_uri(uri) |
| 54 | + local scheme, host, port, path, query = unpack(http:parse_uri(uri, false)) |
| 55 | + ngx.say("scheme: ", scheme, ", host: ", host, ", port: ", port, ", path: ", path, ", query: ", query) |
| 56 | + end |
| 57 | +
|
| 58 | + test_uri("http://example.com") |
| 59 | + test_uri("http://example.com/") |
| 60 | + test_uri("https://example.com/foo/bar") |
| 61 | + test_uri("https://example.com/foo/bar?a=1&b=2") |
| 62 | + test_uri("http://example.com?a=1&b=2") |
| 63 | + test_uri("//example.com") |
| 64 | + test_uri("//example.com?a=1&b=2") |
| 65 | + test_uri("//example.com/foo/bar?a=1&b=2") |
| 66 | + '; |
| 67 | + } |
| 68 | +--- request |
| 69 | +GET /a |
| 70 | +--- response_body |
| 71 | +scheme: http, host: example.com, port: 80, path: /, query: |
| 72 | +scheme: http, host: example.com, port: 80, path: /, query: |
| 73 | +scheme: https, host: example.com, port: 443, path: /foo/bar, query: |
| 74 | +scheme: https, host: example.com, port: 443, path: /foo/bar, query: a=1&b=2 |
| 75 | +scheme: http, host: example.com, port: 80, path: /, query: a=1&b=2 |
| 76 | +scheme: http, host: example.com, port: 80, path: /, query: |
| 77 | +scheme: http, host: example.com, port: 80, path: /, query: a=1&b=2 |
| 78 | +scheme: http, host: example.com, port: 80, path: /foo/bar, query: a=1&b=2 |
| 79 | +--- no_error_log |
| 80 | +[error] |
| 81 | +[warn] |
| 82 | +
|
| 83 | +
|
| 84 | +=== TEST 3: Parse URI fills in defaults correctly, using backwards compatible mode |
| 85 | +--- http_config eval: $::HttpConfig |
| 86 | +--- config |
| 87 | + location = /a { |
| 88 | + content_by_lua ' |
| 89 | + local http = require("resty.http").new() |
| 90 | +
|
| 91 | + local function test_uri(uri) |
| 92 | + local scheme, host, port, path, query = unpack(http:parse_uri(uri)) |
| 93 | + ngx.say("scheme: ", scheme, ", host: ", host, ", port: ", port, ", path: ", path) |
| 94 | + end |
| 95 | +
|
| 96 | + test_uri("http://example.com") |
| 97 | + test_uri("http://example.com/") |
| 98 | + test_uri("https://example.com/foo/bar") |
| 99 | + test_uri("https://example.com/foo/bar?a=1&b=2") |
| 100 | + test_uri("http://example.com?a=1&b=2") |
| 101 | + test_uri("//example.com") |
| 102 | + test_uri("//example.com?a=1&b=2") |
| 103 | + test_uri("//example.com/foo/bar?a=1&b=2") |
| 104 | + '; |
| 105 | + } |
| 106 | +--- request |
| 107 | +GET /a |
| 108 | +--- response_body |
| 109 | +scheme: http, host: example.com, port: 80, path: / |
| 110 | +scheme: http, host: example.com, port: 80, path: / |
| 111 | +scheme: https, host: example.com, port: 443, path: /foo/bar |
| 112 | +scheme: https, host: example.com, port: 443, path: /foo/bar?a=1&b=2 |
| 113 | +scheme: http, host: example.com, port: 80, path: /?a=1&b=2 |
| 114 | +scheme: http, host: example.com, port: 80, path: / |
| 115 | +scheme: http, host: example.com, port: 80, path: /?a=1&b=2 |
| 116 | +scheme: http, host: example.com, port: 80, path: /foo/bar?a=1&b=2 |
| 117 | +--- no_error_log |
| 118 | +[error] |
| 119 | +[warn] |
| 120 | +
|
| 121 | +
|
| 122 | +=== TEST 4: IPv6 notation |
| 123 | +--- http_config eval: $::HttpConfig |
| 124 | +--- config |
| 125 | + location = /a { |
| 126 | + content_by_lua ' |
| 127 | + local http = require("resty.http").new() |
| 128 | +
|
| 129 | + local function test_uri(uri) |
| 130 | + local scheme, host, port, path, query = unpack(http:parse_uri(uri, false)) |
| 131 | + ngx.say("scheme: ", scheme, ", host: ", host, ", port: ", port, ", path: ", path, ", query: ", query) |
| 132 | + end |
| 133 | +
|
| 134 | + test_uri("http://[::1]") |
| 135 | + test_uri("http://[::1]/") |
| 136 | + test_uri("https://[::1]/foo/bar") |
| 137 | + test_uri("https://[::1]/foo/bar?a=1&b=2") |
| 138 | + test_uri("http://[::1]?a=1&b=2") |
| 139 | + test_uri("//[0:0:0:0:0:0:0:0]") |
| 140 | + test_uri("//[0:0:0:0:0:0:0:0]?a=1&b=2") |
| 141 | + test_uri("//[0:0:0:0:0:0:0:0]/foo/bar?a=1&b=2") |
| 142 | + '; |
| 143 | + } |
| 144 | +--- request |
| 145 | +GET /a |
| 146 | +--- response_body |
| 147 | +scheme: http, host: [::1], port: 80, path: /, query: |
| 148 | +scheme: http, host: [::1], port: 80, path: /, query: |
| 149 | +scheme: https, host: [::1], port: 443, path: /foo/bar, query: |
| 150 | +scheme: https, host: [::1], port: 443, path: /foo/bar, query: a=1&b=2 |
| 151 | +scheme: http, host: [::1], port: 80, path: /, query: a=1&b=2 |
| 152 | +scheme: http, host: [0:0:0:0:0:0:0:0], port: 80, path: /, query: |
| 153 | +scheme: http, host: [0:0:0:0:0:0:0:0], port: 80, path: /, query: a=1&b=2 |
| 154 | +scheme: http, host: [0:0:0:0:0:0:0:0], port: 80, path: /foo/bar, query: a=1&b=2 |
| 155 | +--- no_error_log |
| 156 | +[error] |
| 157 | +[warn] |
0 commit comments