Debian Codegraph @

extensions/Scribunto/tests/phpunit/Engines/LuaCommon/UriLibraryTests.lua

raw symbols SCIP
1 local testframework = require 'Module:TestFramework'
2
3 local function test_new( arg )
4 -- Skip the functions
5 local ret = {}
6 for k, v in pairs( mw.uri.new( arg ) ) do
7 if type( v ) ~= 'function' then
8 ret[k] = v
9 end
10 end
11 return ret
12 end
13
14 -- Tests
15 local tests = {
16 { name = 'uri.encode', func = mw.uri.encode,
17 args = { '__foo b\195\161r + baz__' },
18 expect = { '__foo+b%C3%A1r+%2B+baz__' }
19 },
20 { name = 'uri.encode QUERY', func = mw.uri.encode,
21 args = { '__foo b\195\161r + /baz/__', 'QUERY' },
22 expect = { '__foo+b%C3%A1r+%2B+%2Fbaz%2F__' }
23 },
24 { name = 'uri.encode PATH', func = mw.uri.encode,
25 args = { '__foo b\195\161r + /baz/__', 'PATH' },
26 expect = { '__foo%20b%C3%A1r%20%2B%20%2Fbaz%2F__' }
27 },
28 { name = 'uri.encode WIKI', func = mw.uri.encode,
29 args = { '__foo b\195\161r + /baz/__', 'WIKI' },
30 expect = { '__foo_b%C3%A1r_%2B_/baz/__' }
31 },
32
33 { name = 'uri.decode', func = mw.uri.decode,
34 args = { '__foo+b%C3%A1r+%2B+baz__' },
35 expect = { '__foo b\195\161r + baz__' }
36 },
37 { name = 'uri.decode QUERY', func = mw.uri.decode,
38 args = { '__foo+b%C3%A1r+%2B+baz__', 'QUERY' },
39 expect = { '__foo b\195\161r + baz__' }
40 },
41 { name = 'uri.decode PATH', func = mw.uri.decode,
42 args = { '__foo+b%C3%A1r+%2B+baz__', 'PATH' },
43 expect = { '__foo+b\195\161r+++baz__' }
44 },
45 { name = 'uri.decode WIKI', func = mw.uri.decode,
46 args = { '__foo+b%C3%A1r+%2B+baz__', 'WIKI' },
47 expect = { ' foo+b\195\161r+++baz ' }
48 },
49
50 { name = 'uri.anchorEncode', func = mw.uri.anchorEncode,
51 args = { '__foo b\195\161r__' },
52 expect = { 'foo_b.C3.A1r' }
53 },
54
55 { name = 'uri.new', func = test_new,
56 args = { 'http://www.example.com/test?foo=1&bar&baz=1&baz=2#fragment' },
57 expect = {
58 {
59 protocol = 'http',
60 host = 'www.example.com',
61 hostPort = 'www.example.com',
62 authority = 'www.example.com',
63 path = '/test',
64 query = {
65 foo = '1',
66 bar = false,
67 baz = { '1', '2' },
68 },
69 queryString = 'foo=1&bar&baz=1&baz=2',
70 fragment = 'fragment',
71 relativePath = '/test?foo=1&bar&baz=1&baz=2#fragment',
72 },
73 },
74 },
75
76 { name = 'uri.new', func = mw.uri.new, type = 'ToString',
77 args = { 'http://www.example.com/test?foo=1&bar&baz=1&baz=2#fragment' },
78 expect = { 'http://www.example.com/test?foo=1&bar&baz=1&baz=2#fragment' },
79 },
80
81 { name = 'uri.localUrl( Example )', func = mw.uri.localUrl, type = 'ToString',
82 args = { 'Example' },
83 expect = { '/wiki/Example' },
84 },
85 { name = 'uri.localUrl( Example, string )', func = mw.uri.localUrl, type = 'ToString',
86 args = { 'Example', 'action=edit' },
87 expect = { '/w/index.php?title=Example&action=edit' },
88 },
89 { name = 'uri.localUrl( Example, table )', func = mw.uri.localUrl, type = 'ToString',
90 args = { 'Example', { action = 'edit' } },
91 expect = { '/w/index.php?title=Example&action=edit' },
92 },
93
94 { name = 'uri.fullUrl( Example )', func = mw.uri.fullUrl, type = 'ToString',
95 args = { 'Example' },
96 expect = { '//wiki.local/wiki/Example' },
97 },
98 { name = 'uri.fullUrl( Example, string )', func = mw.uri.fullUrl, type = 'ToString',
99 args = { 'Example', 'action=edit' },
100 expect = { '//wiki.local/w/index.php?title=Example&action=edit' },
101 },
102 { name = 'uri.fullUrl( Example, table )', func = mw.uri.fullUrl, type = 'ToString',
103 args = { 'Example', { action = 'edit' } },
104 expect = { '//wiki.local/w/index.php?title=Example&action=edit' },
105 },
106
107 { name = 'uri.canonicalUrl( Example )', func = mw.uri.canonicalUrl, type = 'ToString',
108 args = { 'Example' },
109 expect = { 'http://wiki.local/wiki/Example' },
110 },
111 { name = 'uri.canonicalUrl( Example, string )', func = mw.uri.canonicalUrl, type = 'ToString',
112 args = { 'Example', 'action=edit' },
113 expect = { 'http://wiki.local/w/index.php?title=Example&action=edit' },
114 },
115 { name = 'uri.canonicalUrl( Example, table )', func = mw.uri.canonicalUrl, type = 'ToString',
116 args = { 'Example', { action = 'edit' } },
117 expect = { 'http://wiki.local/w/index.php?title=Example&action=edit' },
118 },
119
120 { name = 'uri.new with empty query string', func = mw.uri.new, type = 'ToString',
121 args = { 'http://wiki.local/w/index.php?' },
122 expect = { 'http://wiki.local/w/index.php?' },
123 },
124
125 { name = 'uri.new with empty fragment', func = mw.uri.new, type = 'ToString',
126 args = { 'http://wiki.local/w/index.php#' },
127 expect = { 'http://wiki.local/w/index.php#' },
128 },
129
130 { name = 'uri.new with IPv6', func = mw.uri.new, type = 'ToString',
131 args = { 'http://[2001:db8::]' },
132 expect = { 'http://[2001:db8::]' },
133 },
134 { name = 'uri.new with IPv6 and port', func = mw.uri.new, type = 'ToString',
135 args = { 'http://[2001:db8::]:80' },
136 expect = { 'http://[2001:db8::]:80' },
137 },
138 }
139
140 -- Add tests to test round-tripping for every combination of parameters
141 local bits = { [0] = false, false, false, false, false, false, false, false, false }
142 local ct = 0
143 while not bits[8] do
144 local url = {}
145 if bits[0] then
146 url[#url+1] = 'http:'
147 end
148 if bits[1] or bits[2] or bits[3] or bits[4] then
149 url[#url+1] = '//'
150 end
151 if bits[1] then
152 url[#url+1] = 'user'
153 end
154 if bits[2] then
155 url[#url+1] = ':password'
156 end
157 if bits[1] or bits[2] then
158 url[#url+1] = '@'
159 end
160 if bits[3] then
161 url[#url+1] = 'host.example.com'
162 end
163 if bits[4] then
164 url[#url+1] = ':123'
165 end
166 if bits[5] then
167 url[#url+1] = '/path'
168 end
169 if bits[6] then
170 url[#url+1] = '?query=1'
171 end
172 if bits[7] then
173 url[#url+1] = '#fragment'
174 end
175
176 url = table.concat( url, '' )
177 tests[#tests+1] = { name = 'uri.new (' .. ct .. ')', func = mw.uri.new, type = 'ToString',
178 args = { url },
179 expect = { url },
180 }
181 ct = ct + 1
182
183 for i = 0, 8 do
184 bits[i] = not bits[i]
185 if bits[i] then
186 break
187 end
188 end
189 end
190
191 return testframework.getTestProvider( tests )