OLD | NEW |
---|---|
1 importScripts('worker-test-helpers.js'); | 1 importScripts('worker-test-helpers.js'); |
2 | 2 |
3 test(function() { | 3 var url = 'https://www.example.com/test.html'; |
falken
2014/07/04 05:43:39
name this to be a constant like var URL?
horo
2014/07/04 08:00:38
Done.
| |
4 var headers = new HeaderMap; | 4 test(function() { |
5 var headers = new Headers; | |
5 headers.set('User-Agent', 'Mozilla/5.0'); | 6 headers.set('User-Agent', 'Mozilla/5.0'); |
6 headers.set('Accept', 'text/html'); | 7 headers.set('Accept', 'text/html'); |
7 headers.set('X-ServiceWorker-Test', 'request test field'); | 8 headers.set('X-ServiceWorker-Test', 'request test field'); |
8 | 9 |
9 var request = new Request({ | 10 var request = new Request(url, {method: 'GET', headers: headers}); |
10 url: 'https://www.example.com/test.html', | 11 |
11 method: 'GET', | 12 assert_equals(request.url, url, 'Request.url should match'); |
12 headers: headers | |
13 }); | |
14 | |
15 assert_equals(request.url, 'https://www.example.com/test.html', 'Request.url should match'); | |
16 assert_equals(request.method, 'GET', 'Request.method should match'); | 13 assert_equals(request.method, 'GET', 'Request.method should match'); |
17 assert_equals(request.origin, 'https://www.example.com', 'Request.origin sho uld match'); | 14 assert_equals(request.referrer, location.href, 'Request.referrer should matc h'); |
18 assert_true(request.headers instanceof HeaderMap, 'Request.headers should be HeaderMap'); | 15 assert_true(request.headers instanceof Headers, 'Request.headers should be H eaders'); |
19 assert_equals(request.headers.size, 3, 'Request.headers.size should match'); | 16 |
20 // Note: detailed behavioral tests for HeaderMap are in another test, | 17 // 'User-Agent' is a forbidden header. |
21 // http/tests/serviceworker/headermap.html. | 18 assert_equals(request.headers.size, 2, 'Request.headers.size should match'); |
19 // Note: detailed behavioral tests for Headers are in another test, | |
20 // http/tests/serviceworker/headers.html. | |
22 | 21 |
23 request.url = 'http://localhost/'; | 22 request.url = 'http://localhost/'; |
24 assert_equals(request.url, 'http://localhost/', 'Request.url should be writa ble'); | 23 assert_equals(request.url, 'https://www.example.com/test.html', 'Request.url should be readonly'); |
25 request.url = 'http://localhost/\uD800'; // Unmatched lead surrogate. | 24 request = new Request('http://localhost/\uD800'); // Unmatched lead surrogat e. |
26 assert_equals(request.url, | 25 assert_equals(request.url, |
27 'http://localhost/' + encodeURIComponent('\uFFFD'), | 26 'http://localhost/' + encodeURIComponent('\uFFFD'), |
28 'Request.url should have unmatched surrogates replaced.'); | 27 'Request.url should have unmatched surrogates replaced.'); |
29 request.method = 'POST'; | 28 request.method = 'POST'; |
30 assert_equals(request.method, 'POST', 'Request.method should be writable'); | 29 assert_equals(request.method, 'GET', 'Request.method should be readonly'); |
31 assert_throws({name: 'TypeError'}, function() { request.method = 'invalid \u 0100'; }, | 30 }, |
falken
2014/07/04 05:43:39
I think we typically don't line break here
horo
2014/07/04 08:00:37
Done.
| |
32 'Request.method should throw on invalid ByteString'); | 31 'Request basic test in ServiceWorkerGlobalScope'); |
33 | 32 |
34 }, 'Request in ServiceWorkerGlobalScope'); | 33 test(function() { |
34 [new Request(url), | |
35 new Request(url, {method: ''}), | |
36 new Request(url, {mode: ''}), | |
37 new Request(url, {mode: 'invalid mode'}), | |
38 new Request(url, {credentials: ''}), | |
39 new Request(url, {credentials: 'invalid credentials'})].forEach(function(re quest) { | |
40 assert_equals(request.url, url, 'Request.url should match'); | |
41 assert_equals(request.method, 'GET', 'Default Request.method should be G ET'); | |
42 assert_equals(request.mode, 'cors', 'Default Request.mode should be cors '); | |
43 assert_equals(request.credentials, 'omit', 'Default Request.credentials should be omit'); | |
44 }); | |
45 }, | |
46 'Request default value test in ServiceWorkerGlobalScope'); | |
47 | |
48 test(function() { | |
49 var request = new Request(url); | |
50 request.headers.append('X-ServiceWorker-Foo', 'foo1'); | |
51 request.headers.append('X-ServiceWorker-Foo', 'foo2'); | |
52 request.headers.append('X-ServiceWorker-Bar', 'bar'); | |
53 var request2 = new Request(request); | |
54 assert_equals(request2.url, url, 'Request.url should match'); | |
55 assert_equals(request2.method, 'GET', 'Request.method should match'); | |
56 assert_equals(request2.mode, 'cors', 'Request.mode should match'); | |
57 assert_equals(request2.credentials, 'omit', 'Request.credentials should matc h'); | |
58 assert_equals(request2.headers.getAll('X-ServiceWorker-Foo')[0], 'foo1', | |
59 'Request.headers should match'); | |
60 assert_equals(request2.headers.getAll('X-ServiceWorker-Foo')[1], 'foo2', | |
61 'Request.headers should match'); | |
62 assert_equals(request2.headers.getAll('X-ServiceWorker-Bar')[0], 'bar', | |
63 'Request.headers should match'); | |
64 }, | |
65 'Request header test in ServiceWorkerGlobalScope'); | |
66 | |
67 test(function() { | |
68 var request1 = {}; | |
69 var request2 = {}; | |
70 var kMethods = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'OPTIONS', '', undef ined]; | |
71 var kSimpleMethod = ['GET', 'HEAD', 'POST', '', undefined]; | |
72 var kModes = ['same-origin', 'no-cors', 'cors', '', undefined]; | |
falken
2014/07/04 05:43:39
It looks uncommon to use kVariable to denote a con
horo
2014/07/04 08:00:38
Done.
| |
73 var isSimpleMethod = function (method) { | |
falken
2014/07/04 05:43:39
This seems more readable as simply function isSimp
falken
2014/07/04 05:43:40
Be consistent with "function (" vs "function(". I
horo
2014/07/04 08:00:38
Done.
horo
2014/07/04 08:00:38
Done.
| |
74 return kSimpleMethod.indexOf(method) != -1; | |
75 }; | |
76 var effectiveMethod = function (method1, method2) { | |
77 return method2 ? method2 : (method1 ? method1 : 'GET'); | |
78 }; | |
79 var effectiveMode = function (mode1, mode2) { | |
80 return mode2 ? mode2 : (mode1 ? mode1 : 'cors'); | |
81 }; | |
82 kMethods.forEach(function(method1) { | |
83 kModes.forEach(function(mode1) { | |
84 var init1 = {}; | |
85 if (method1 != undefined) { init1['method'] = method1; } | |
86 if (mode1 != undefined) { init1['mode'] = mode1; } | |
87 if (!isSimpleMethod(method1) && mode1 == 'no-cors') { | |
88 assert_throws( | |
89 {name:'TypeError'}, | |
90 function() { request1 = new Request(url, init1); }, | |
91 'new no-cors Request with non simple method (' + method1 +') should throw'); | |
92 return; | |
93 } | |
94 request1 = new Request(url, init1); | |
95 assert_equals(request1.method, method1 ? method1 : 'GET', 'Request.m ethod should match'); | |
96 assert_equals(request1.mode, mode1 ? mode1 : 'cors', 'Request.mode s hould match'); | |
97 request1 = new Request(request1); | |
98 assert_equals(request1.method, method1 ? method1 : 'GET', 'Request.m ethod should match'); | |
99 assert_equals(request1.mode, mode1 ? mode1 : 'cors', 'Request.mode s hould match'); | |
100 kMethods.forEach(function(method2) { | |
101 kModes.forEach(function(mode2) { | |
102 var init2 = {}; | |
103 if (method2 != undefined) { init2['method'] = method2; } | |
104 if (mode2 != undefined) { init2['mode'] = mode2; } | |
105 if (!isSimpleMethod(effectiveMethod(method1, method2)) && | |
106 effectiveMode(mode1,mode2) == 'no-cors') { | |
falken
2014/07/04 05:43:39
If you want to wrap, this should be indented an ad
falken
2014/07/04 05:43:39
spacing: mode1, mode2
horo
2014/07/04 08:00:38
Done.
horo
2014/07/04 08:00:38
Done.
| |
107 assert_throws( | |
108 {name:'TypeError'}, | |
109 function() { request2 = new Request(request1, init2) ; }, | |
110 'new no-cors Request with non simple method should t hrow'); | |
111 return; | |
112 } | |
113 request2 = new Request(request1, init2); | |
114 assert_equals(request2.method, | |
115 method2 ? method2 : request1.method, | |
116 'Request.method should be overridden'); | |
117 assert_equals(request2.mode, | |
118 mode2 ? mode2 : request1.mode, | |
119 'Request.mode should be overridden'); | |
120 }); | |
121 }); | |
122 }); | |
123 }); | |
124 }, | |
125 'Request header test in ServiceWorkerGlobalScope'); | |
126 | |
127 test(function() { | |
128 var request1 = {}; | |
129 var request2 = {}; | |
130 var kCredentials = ['omit', 'same-origin', 'include', '', undefined]; | |
131 kCredentials.forEach(function(credentials1) { | |
132 var init1 = {}; | |
133 if (credentials1 != undefined) { init1['credentials'] = credentials1; } | |
134 request1 = new Request(url, init1); | |
135 assert_equals(request1.credentials, credentials1 ? credentials1 : 'omit' , 'Request.credentials should match'); | |
136 request1 = new Request(request1); | |
137 assert_equals(request1.credentials, credentials1 ? credentials1 : 'omit' , 'Request.credentials should match'); | |
138 kCredentials.forEach(function(credentials2) { | |
139 var init2 = {}; | |
140 if (credentials2 != undefined) { init2['credentials'] = credentials2 ; } | |
141 request2 = new Request(request1, init2); | |
142 assert_equals(request2.credentials, | |
143 credentials2 ? credentials2 : request1.credentials, | |
144 'Request.credentials should be overridden'); | |
145 }); | |
146 }); | |
147 }, | |
148 'Request credentials test in ServiceWorkerGlobalScope'); | |
149 | |
150 test(function() { | |
151 ['same-origin', 'cors', 'no-cors'].forEach(function(mode) { | |
152 var forbiddenMethods = ['TRACE', 'TRACK', 'CONNECT']; | |
153 forbiddenMethods.forEach(function(method) { | |
154 assert_throws( | |
155 {name:'TypeError'}, | |
156 function() { var request = new Request(url, {mode: mode, method: method}); }, | |
157 'new Request with a forbidden method (' + method +') should thro w'); | |
158 }); | |
159 var invalidNames = ['(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', | |
160 '/', '[', ']', '?', '=', '{', '}', '\u3042', 'a(b', | |
161 'invalid name']; | |
162 invalidNames.forEach(function(name) { | |
163 assert_throws( | |
164 {name:'TypeError'}, | |
165 function() { var request = new Request(url, {mode: mode, method: name}); }, | |
166 'new Request with an invalid method (' + name +') should throw') ; | |
167 }); | |
168 }); | |
169 }, | |
170 'Request method name test in ServiceWorkerGlobalScope'); | |
171 | |
172 test(function() { | |
173 var kForbiddenHeaders = | |
174 ['Accept-Charset', 'Accept-Encoding', 'Access-Control-Request-Headers', | |
175 'Access-Control-Request-Method', 'Connection', 'Content-Length', 'Cooki e', | |
176 'Cookie2', 'Date', 'DNT', 'Expect', 'Host', 'Keep-Alive', 'Origin', | |
177 'Referer', 'TE', 'Trailer', 'Transfer-Encoding', 'Upgrade', 'User-Agent ', | |
178 'Via', 'Proxy-', 'Sec-', 'Proxy-FooBar', 'Sec-FooBar']; | |
179 var kSimpleHeaders = | |
180 [['Accept', '*'], ['Accept-Language', 'ru'], ['Content-Language', 'ru'], | |
181 ['Content-Type', 'application/x-www-form-urlencoded'], | |
182 ['Content-Type', 'multipart/form-data'], | |
183 ['Content-Type', 'text/plain']]; | |
184 var kNonSimpleHeaders = | |
185 [['X-ServiceWorker-Test', 'test'], | |
186 ['X-ServiceWorker-Test2', 'test2'], | |
187 ['Content-Type', 'foo/bar']]; | |
188 | |
189 ['same-origin', 'cors'].forEach(function(mode) { | |
190 var request = new Request(url, {mode: mode}); | |
191 kForbiddenHeaders.forEach(function(header) { | |
192 request.headers.append(header, 'test'); | |
193 assert_equals(request.headers.size, 0, | |
194 'Request.headers.append should ignore the forbidden he aders'); | |
195 request.headers.set(header, 'test'); | |
196 assert_equals(request.headers.size, 0, | |
197 'Request.headers.set should ignore the forbidden heade rs'); | |
198 }); | |
199 var request = new Request(url, {mode: mode}); | |
200 assert_equals(request.headers.size, 0); | |
201 kNonSimpleHeaders.forEach(function(header) { | |
202 request.headers.append(header[0], header[1]); | |
203 }); | |
204 assert_equals(request.headers.size, kNonSimpleHeaders.length); | |
205 kNonSimpleHeaders.forEach(function(header) { | |
206 assert_equals(request.headers.get(header[0]), header[1]); | |
207 }); | |
208 request = new Request(url, {mode: mode}); | |
209 assert_equals(request.headers.size, 0); | |
210 kNonSimpleHeaders.forEach(function(header) { | |
211 request.headers.set(header[0], header[1]); | |
212 }); | |
213 assert_equals(request.headers.size, kNonSimpleHeaders.length); | |
214 kNonSimpleHeaders.forEach(function(header) { | |
215 assert_equals(request.headers.get(header[0]), header[1]); | |
216 }); | |
217 }); | |
218 request = new Request(url, {mode: 'no-cors'}); | |
219 kForbiddenHeaders.forEach(function(header) { | |
220 request.headers.set(header, 'test'); | |
221 request.headers.append(header, 'test'); | |
222 }); | |
223 kNonSimpleHeaders.forEach(function(header) { | |
224 request.headers.set(header[0], header[1]); | |
225 request.headers.append(header[0], header[1]); | |
226 }); | |
227 assert_equals(request.headers.size, 0, | |
228 'no-cors request should only accept simple headers'); | |
229 | |
230 kSimpleHeaders.forEach(function(header) { | |
231 request = new Request(url, {mode: 'no-cors'}); | |
232 request.headers.append(header[0], header[1]); | |
233 assert_equals(request.headers.size, 1, | |
234 'no-cors request should accept simple headers'); | |
235 request = new Request(url, {mode: 'no-cors'}); | |
236 request.headers.set(header[0], header[1]); | |
237 assert_equals(request.headers.size, 1, | |
238 'no-cors request should accept simple headers'); | |
239 request.headers.delete(header[0]); | |
240 if (header[0] == 'Content-Type') { | |
241 assert_equals( | |
242 request.headers.size, 1, | |
243 'Content-Type header of no-cors request shouldn\'t be deleted'); | |
244 } else { | |
245 assert_equals(request.headers.size, 0); | |
246 } | |
247 }); | |
248 | |
249 kSimpleHeaders.forEach(function(header) { | |
250 var headers = {}; | |
251 kNonSimpleHeaders.forEach(function(header2) { | |
252 headers[header2[0]] = header2[1]; | |
253 }); | |
254 kForbiddenHeaders.forEach(function(header) { headers[header] = 'foo';}); | |
falken
2014/07/04 05:43:40
space between ; and } for consistency with rest of
horo
2014/07/04 08:00:38
Done.
| |
255 headers[header[0]] = header[1]; | |
256 var expectedSize = kNonSimpleHeaders.length; | |
257 if (header[0] != 'Content-Type') { | |
258 ++expectedSize; | |
259 } | |
260 ['same-origin', 'cors'].forEach(function(mode) { | |
261 request = new Request(url, {mode: mode, headers: headers}); | |
262 assert_equals(request.headers.size, expectedSize, | |
263 'Request should not support the forbidden headers'); | |
264 }); | |
265 request = new Request(url, {mode: 'no-cors', headers: headers}); | |
266 assert_equals(request.headers.size, 1, | |
267 'No-CORS Request.headers should only support simple header s'); | |
268 }); | |
269 }, | |
270 'Request headers test in ServiceWorkerGlobalScope'); | |
OLD | NEW |