-
Notifications
You must be signed in to change notification settings - Fork 9
/
parse_url.qtest
executable file
·147 lines (117 loc) · 6.02 KB
/
parse_url.qtest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env qore
# -*- mode: qore; indent-tabs-mode: nil -*-
%new-style
%enable-all-warnings
%require-types
%strict-args
%requires ../../../../qlib/QUnit.qm
%exec-class parseUrlTest
public class parseUrlTest inherits QUnit::Test {
constructor() : Test("parse_url test", "1.0") {
addTestCase("decode test", \decodeTest());
addTestCase("slash test", \slashTest());
addTestCase("Test 1", \test());
addTestCase("port test", \portTest());
# Return for compatibility with test harness that checks return value.
set_return_value(main());
}
decodeTest() {
hash<UrlInfo> h = parse_url("https://user-%3cname%3e%40example.com%2f"
"token:pass%3f%3aword@qoretechnologies.zendesk.com/api/v2/users%40str", QURL_DECODE);
assertEq("user-<name>@example.com/token", h.username);
assertEq("pass?:word", h.password);
assertEq("qoretechnologies.zendesk.com", h.host);
assertEq("/api/v2/users%40str", h.path);
h = parse_url("https://user-%3cname%3e%40example.com%2f"
"token:pass%3f%3aword@qoretechnologies.zendesk.com/api/v2/users%40str", QURL_DECODE_PATH);
assertEq("user-<name>@example.com/token", h.username);
assertEq("pass?:word", h.password);
assertEq("qoretechnologies.zendesk.com", h.host);
assertEq("/api/v2/users@str", h.path);
}
slashTest() {
hash<auto> h = parse_url("https://user@example.com/token:api-token@qoretechnologies.zendesk.com/api/v2/users");
assertEq("user@example.com/token", h.username);
assertEq("api-token", h.password);
assertEq("qoretechnologies.zendesk.com", h.host);
assertEq("/api/v2/users", h.path);
h = parse_url("https://user@example.com/token:api-token@qoretechnologies.zendesk.com");
assertEq("user@example.com/token", h.username);
assertEq("api-token", h.password);
assertEq("qoretechnologies.zendesk.com", h.host);
assertNothing(h.path);
h = parse_url("https://user@example.com:token@qoretechnologies.zendesk.com");
assertEq("user@example.com", h.username);
assertEq("token", h.password);
assertEq("qoretechnologies.zendesk.com", h.host);
assertNothing(h.path);
}
test() {
# "standard" test
assertEq(("protocol": "http", "path": "/path", "username": "user", "password": "pass", "host": "host", "port": 80), parse_url("http://user:pass@host:80/path"));
# "standard" path
assertEq(("protocol": "file", "path": "/tmp"), parse_url("file:///tmp"));
# UNIX socket URL
assertEq(("protocol": "http", "host": "/tmp/socket-file.sock"), parse_url("http://socket=%2Ftmp%2Fsocket-file.sock"));
assertEq(("protocol": "http", "host": "/tmp/socket-file.sock", "path": "/path"), parse_url("http://socket=%2Ftmp%2Fsocket-file.sock/path"));
# windows file paths
assertEq(("protocol": "file", "path": "c:\\tmp"), parse_url("file://c:\\tmp"));
assertEq(("protocol": "file", "path": "\\\\share\\dir"), parse_url("file://\\\\share\\dir"));
# IPv6 localhost
assertEq(("host": "::"), parse_url("::"));
# one-character username
assertEq(("protocol": "http", "username": "u", "password": "p", "host": "h"), parse_url("http://u:p@h"));
# check that we can parse a UTF-16-encoded URL
assertEq(("protocol": "http", "username": "u", "password": "p", "host": "h"), parse_url(convert_encoding("http://u:p@h", "UTF-16")));
# empty URL
assertThrows("PARSE-URL-ERROR", sub () { parse_url(""); } );
# invalid characters in port
assertThrows("PARSE-URL-ERROR", sub () { parse_url("scheme://user@host:hi/"); } );
# corner case: a path string should be recognized and parsed into the path key
assertEq(("path": "/tmp/file"), parse_url("/tmp/file"));
# corner case: a UNIX socket path string with no protocol
assertEq(("host": "/tmp/socket-file.sock"), parse_url("socket=%2Ftmp%2Fsocket-file.sock"));
# corner case: a UNIX socket path string with no protocol but with a path
assertEq(("host": "/tmp/socket-file.sock", "path": "/path"), parse_url("socket=%2Ftmp%2Fsocket-file.sock/path"));
# corner case: a relative path must be parsed with just the "path" key
assertEq(("path": "some/path"), parse_url("some/path"));
# corner case: a hostname must be returned just in the "host" key
assertEq(("host": "hostname"), parse_url("hostname"));
# corner case: a single leading backslash is returned as a hostname
assertEq(("host": "\\hostname"), parse_url("\\hostname"));
# corner case: two leading backslashes are returned as a UNC path
assertEq(("path": "\\\\hostname"), parse_url("\\\\hostname"));
# issue #3287: single character hostname
assertEq("a", parse_url("http://a").host);
assertEq("a", parse_url("http://a/").host);
assertEq("a", parse_url("http://a:1").host);
assertEq("a", parse_url("http://a:1/").host);
}
private portTest() {
# non num chars in port
try {
parse_url("http://localhost:abc");
fail("it must throw an exception");
}
catch (hash ex) {
assertEq("PARSE-URL-ERROR", ex.err);
assertEq(True, ex.desc =~ /has an invalid non-numeric character in the port specification/, ex.desc);
}
# out of INT range port
try {
parse_url("http://localhost:999999999999999");
fail("it must throw an exception");
} catch (hash<ExceptionInfo> ex) {
assertEq("PARSE-URL-ERROR", ex.err);
assertRegex("invalid port.*between.*0.*65535", ex.desc);
}
# out of int16 range: 0-65535
try {
parse_url("http://localhost:65536");
fail("it must throw an exception");
} catch (hash<ExceptionInfo> ex) {
assertEq("PARSE-URL-ERROR", ex.err);
assertRegex("invalid port.*between.*0.*65535", ex.desc);
}
}
}