Skip to content

Commit f986d8e

Browse files
committed
Move parse_uri tests to separate file and add ipv6 tests
1 parent 654ea15 commit f986d8e

File tree

2 files changed

+157
-95
lines changed

2 files changed

+157
-95
lines changed

t/01-basic.t

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -300,101 +300,6 @@ not initialized
300300
[warn]
301301

302302

303-
=== TEST 9: Parse URI errors if malformed
304-
--- http_config eval: $::HttpConfig
305-
--- config
306-
location = /a {
307-
content_by_lua '
308-
local http = require("resty.http").new()
309-
local parts, err = http:parse_uri("http:///example.com")
310-
if not parts then ngx.say(err) end
311-
';
312-
}
313-
--- request
314-
GET /a
315-
--- response_body
316-
bad uri: http:///example.com
317-
--- no_error_log
318-
[error]
319-
[warn]
320-
321-
322-
=== TEST 10: Parse URI fills in defaults correctly
323-
--- http_config eval: $::HttpConfig
324-
--- config
325-
location = /a {
326-
content_by_lua '
327-
local http = require("resty.http").new()
328-
329-
local function test_uri(uri)
330-
local scheme, host, port, path, query = unpack(http:parse_uri(uri, false))
331-
ngx.say("scheme: ", scheme, ", host: ", host, ", port: ", port, ", path: ", path, ", query: ", query)
332-
end
333-
334-
test_uri("http://example.com")
335-
test_uri("http://example.com/")
336-
test_uri("https://example.com/foo/bar")
337-
test_uri("https://example.com/foo/bar?a=1&b=2")
338-
test_uri("http://example.com?a=1&b=2")
339-
test_uri("//example.com")
340-
test_uri("//example.com?a=1&b=2")
341-
test_uri("//example.com/foo/bar?a=1&b=2")
342-
';
343-
}
344-
--- request
345-
GET /a
346-
--- response_body
347-
scheme: http, host: example.com, port: 80, path: /, query:
348-
scheme: http, host: example.com, port: 80, path: /, query:
349-
scheme: https, host: example.com, port: 443, path: /foo/bar, query:
350-
scheme: https, host: example.com, port: 443, path: /foo/bar, query: a=1&b=2
351-
scheme: http, host: example.com, port: 80, path: /, query: a=1&b=2
352-
scheme: http, host: example.com, port: 80, path: /, query:
353-
scheme: http, host: example.com, port: 80, path: /, query: a=1&b=2
354-
scheme: http, host: example.com, port: 80, path: /foo/bar, query: a=1&b=2
355-
--- no_error_log
356-
[error]
357-
[warn]
358-
359-
360-
=== TEST 11: Parse URI fills in defaults correctly, using backwards compatible mode
361-
--- http_config eval: $::HttpConfig
362-
--- config
363-
location = /a {
364-
content_by_lua '
365-
local http = require("resty.http").new()
366-
367-
local function test_uri(uri)
368-
local scheme, host, port, path, query = unpack(http:parse_uri(uri))
369-
ngx.say("scheme: ", scheme, ", host: ", host, ", port: ", port, ", path: ", path)
370-
end
371-
372-
test_uri("http://example.com")
373-
test_uri("http://example.com/")
374-
test_uri("https://example.com/foo/bar")
375-
test_uri("https://example.com/foo/bar?a=1&b=2")
376-
test_uri("http://example.com?a=1&b=2")
377-
test_uri("//example.com")
378-
test_uri("//example.com?a=1&b=2")
379-
test_uri("//example.com/foo/bar?a=1&b=2")
380-
';
381-
}
382-
--- request
383-
GET /a
384-
--- response_body
385-
scheme: http, host: example.com, port: 80, path: /
386-
scheme: http, host: example.com, port: 80, path: /
387-
scheme: https, host: example.com, port: 443, path: /foo/bar
388-
scheme: https, host: example.com, port: 443, path: /foo/bar?a=1&b=2
389-
scheme: http, host: example.com, port: 80, path: /?a=1&b=2
390-
scheme: http, host: example.com, port: 80, path: /
391-
scheme: http, host: example.com, port: 80, path: /?a=1&b=2
392-
scheme: http, host: example.com, port: 80, path: /foo/bar?a=1&b=2
393-
--- no_error_log
394-
[error]
395-
[warn]
396-
397-
398303
=== TEST 12: Allow empty HTTP header values (RFC7230)
399304
--- http_config eval: $::HttpConfig
400305
--- config

t/18-parse_uri.t

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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

Comments
 (0)