OLD | NEW |
1 importScripts('worker-test-harness.js'); | 1 importScripts('worker-test-harness.js'); |
2 importScripts('test-helpers.js'); | 2 importScripts('test-helpers.js'); |
3 | 3 |
4 var URL = 'https://www.example.com/test.html'; | 4 var URL = 'https://www.example.com/test.html'; |
5 | 5 |
6 test(function() { | 6 test(function() { |
7 var headers = new Headers; | 7 var headers = new Headers; |
8 headers.set('User-Agent', 'Mozilla/5.0'); | 8 headers.set('User-Agent', 'Mozilla/5.0'); |
9 headers.set('Accept', 'text/html'); | 9 headers.set('Accept', 'text/html'); |
10 headers.set('X-ServiceWorker-Test', 'request test field'); | 10 headers.set('X-ServiceWorker-Test', 'request test field'); |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 }; | 296 }; |
297 var request = | 297 var request = |
298 new Request(URL, | 298 new Request(URL, |
299 { | 299 { |
300 method: 'POST', | 300 method: 'POST', |
301 body: new Blob(['Test Blob'], {type: 'test/type'}) | 301 body: new Blob(['Test Blob'], {type: 'test/type'}) |
302 }); | 302 }); |
303 assert_equals( | 303 assert_equals( |
304 getContentType(request.headers), 'test/type', | 304 getContentType(request.headers), 'test/type', |
305 'ContentType header of Request created with Blob body must be set.'); | 305 'ContentType header of Request created with Blob body must be set.'); |
306 request.body.asText() | 306 assert_false(request.bodyUsed, |
| 307 'bodyUsed must be true before calling text()'); |
| 308 request.text() |
307 .then(function(result) { | 309 .then(function(result) { |
308 assert_equals(result, 'Test Blob', | 310 assert_equals(result, 'Test Blob', |
309 'Creating a Request with Blob body should success.'); | 311 'Creating a Request with Blob body should success.'); |
310 | 312 |
311 request = new Request(URL, {method: 'POST', body: 'Test String'}); | 313 request = new Request(URL, {method: 'POST', body: 'Test String'}); |
312 assert_equals( | 314 assert_equals( |
313 getContentType(request.headers), 'text/plain;charset=UTF-8', | 315 getContentType(request.headers), 'text/plain;charset=UTF-8', |
314 'ContentType header of Request created with string must be set.'); | 316 'ContentType header of Request created with string must be set.'); |
315 return request.body.asText(); | 317 return request.text(); |
316 }) | 318 }) |
317 .then(function(result) { | 319 .then(function(result) { |
318 assert_equals(result, 'Test String', | 320 assert_equals(result, 'Test String', |
319 'Creating a Request with string body should success.'); | 321 'Creating a Request with string body should success.'); |
320 | 322 |
321 var text = "Test ArrayBuffer"; | 323 var text = "Test ArrayBuffer"; |
322 var array = new Uint8Array(text.length); | 324 var array = new Uint8Array(text.length); |
323 for (var i = 0; i < text.length; ++i) | 325 for (var i = 0; i < text.length; ++i) |
324 array[i] = text.charCodeAt(i); | 326 array[i] = text.charCodeAt(i); |
325 request = new Request(URL, {method: 'POST', body: array.buffer}); | 327 request = new Request(URL, {method: 'POST', body: array.buffer}); |
326 return request.body.asText(); | 328 return request.text(); |
327 }) | 329 }) |
328 .then(function(result) { | 330 .then(function(result) { |
329 assert_equals( | 331 assert_equals( |
330 result, 'Test ArrayBuffer', | 332 result, 'Test ArrayBuffer', |
331 'Creating a Request with ArrayBuffer body should success.'); | 333 'Creating a Request with ArrayBuffer body should success.'); |
332 | 334 |
333 var text = "Test ArrayBufferView"; | 335 var text = "Test ArrayBufferView"; |
334 var array = new Uint8Array(text.length); | 336 var array = new Uint8Array(text.length); |
335 for (var i = 0; i < text.length; ++i) | 337 for (var i = 0; i < text.length; ++i) |
336 array[i] = text.charCodeAt(i); | 338 array[i] = text.charCodeAt(i); |
337 request = new Request(URL, {method: 'POST', body: array}); | 339 request = new Request(URL, {method: 'POST', body: array}); |
338 return request.body.asText(); | 340 return request.text(); |
339 }) | 341 }) |
340 .then(function(result) { | 342 .then(function(result) { |
341 assert_equals( | 343 assert_equals( |
342 result, 'Test ArrayBufferView', | 344 result, 'Test ArrayBufferView', |
343 'Creating a Request with ArrayBuffer body should success.'); | 345 'Creating a Request with ArrayBuffer body should success.'); |
344 | 346 |
345 var formData = new FormData(); | 347 var formData = new FormData(); |
346 formData.append('sample string', '1234567890'); | 348 formData.append('sample string', '1234567890'); |
347 formData.append('sample blob', new Blob(['blob content'])); | 349 formData.append('sample blob', new Blob(['blob content'])); |
348 formData.append('sample file', | 350 formData.append('sample file', |
349 new File(['file content'], 'file.dat')); | 351 new File(['file content'], 'file.dat')); |
350 request = new Request(URL, {method: 'POST', body: formData}); | 352 request = new Request(URL, {method: 'POST', body: formData}); |
351 return request.body.asText(); | 353 return request.text(); |
352 }) | 354 }) |
353 .then(function(result) { | 355 .then(function(result) { |
354 var reg = new RegExp('multipart\/form-data; boundary=(.*)'); | 356 var reg = new RegExp('multipart\/form-data; boundary=(.*)'); |
355 var regResult = reg.exec(getContentType(request.headers)); | 357 var regResult = reg.exec(getContentType(request.headers)); |
356 var boundary = regResult[1]; | 358 var boundary = regResult[1]; |
357 var expected_body = | 359 var expected_body = |
358 '--' + boundary + '\r\n' + | 360 '--' + boundary + '\r\n' + |
359 'Content-Disposition: form-data; name="sample string"\r\n' + | 361 'Content-Disposition: form-data; name="sample string"\r\n' + |
360 '\r\n' + | 362 '\r\n' + |
361 '1234567890\r\n' + | 363 '1234567890\r\n' + |
(...skipping 11 matching lines...) Expand all Loading... |
373 'file content\r\n' + | 375 'file content\r\n' + |
374 '--' + boundary + '--\r\n'; | 376 '--' + boundary + '--\r\n'; |
375 assert_equals( | 377 assert_equals( |
376 result, expected_body, | 378 result, expected_body, |
377 'Creating a Request with FormData body should success.'); | 379 'Creating a Request with FormData body should success.'); |
378 }) | 380 }) |
379 .then(function() { | 381 .then(function() { |
380 t.done(); | 382 t.done(); |
381 }) | 383 }) |
382 .catch(unreached_rejection(t)); | 384 .catch(unreached_rejection(t)); |
| 385 assert_true(request.bodyUsed, |
| 386 'bodyUsed must be true after calling text()'); |
383 }, 'Request body test in ServiceWorkerGlobalScope'); | 387 }, 'Request body test in ServiceWorkerGlobalScope'); |
OLD | NEW |