Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(507)

Side by Side Diff: third_party/WebKit/LayoutTests/http/tests/fetch/script-tests/request.js

Issue 2679563002: [WIP] Expose Request.body property
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 if (self.importScripts) { 1 if (self.importScripts) {
2 importScripts('../resources/fetch-test-helpers.js'); 2 importScripts('../resources/fetch-test-helpers.js');
3 } 3 }
4 4
5 var URL = 'https://www.example.com/test.html'; 5 var URL = 'https://www.example.com/test.html';
6 6
7 test(function() { 7 test(function() {
8 var headers = new Headers; 8 var headers = new Headers;
9 headers.set('User-Agent', 'Mozilla/5.0'); 9 headers.set('User-Agent', 'Mozilla/5.0');
10 headers.set('Accept', 'text/html'); 10 headers.set('Accept', 'text/html');
(...skipping 19 matching lines...) Expand all
30 request.url = 'http://localhost/'; 30 request.url = 'http://localhost/';
31 assert_equals(request.url, 'https://www.example.com/test.html', 31 assert_equals(request.url, 'https://www.example.com/test.html',
32 'Request.url should be readonly'); 32 'Request.url should be readonly');
33 33
34 // Unmatched lead surrogate. 34 // Unmatched lead surrogate.
35 request = new Request('http://localhost/\uD800'); 35 request = new Request('http://localhost/\uD800');
36 assert_equals(request.url, 36 assert_equals(request.url,
37 'http://localhost/' + encodeURIComponent('\uFFFD'), 37 'http://localhost/' + encodeURIComponent('\uFFFD'),
38 'Request.url should have unmatched surrogates replaced.'); 38 'Request.url should have unmatched surrogates replaced.');
39 39
40 assert_equals(request.body, null, 'null body');
40 request.method = 'POST'; 41 request.method = 'POST';
41 assert_equals(request.method, 'GET', 'Request.method should be readonly'); 42 assert_equals(request.method, 'GET', 'Request.method should be readonly');
42 }, 'Request basic test'); 43 }, 'Request basic test');
43 44
44 test(function() { 45 test(function() {
45 [new Request(URL), 46 [new Request(URL),
46 // All mode/credentials below are invalid and thus ignored. 47 // All mode/credentials below are invalid and thus ignored.
47 new Request(URL, {mode: null}), 48 new Request(URL, {mode: null}),
48 new Request(URL, {mode: undefined}), 49 new Request(URL, {mode: undefined}),
49 new Request(URL, {mode: 'sameorigin'}), 50 new Request(URL, {mode: 'sameorigin'}),
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 'consumed.'); 349 'consumed.');
349 var req2 = new Request(req); 350 var req2 = new Request(req);
350 assert_false(req.bodyUsed, 351 assert_false(req.bodyUsed,
351 'Request should not be flagged as used if it does not ' + 352 'Request should not be flagged as used if it does not ' +
352 'have body.'); 353 'have body.');
353 assert_false(req2.bodyUsed, 354 assert_false(req2.bodyUsed,
354 'Request should not be flagged as used if it has not been ' + 355 'Request should not be flagged as used if it has not been ' +
355 'consumed.'); 356 'consumed.');
356 }, 'Request construction without body behavior regardning "bodyUsed"'); 357 }, 'Request construction without body behavior regardning "bodyUsed"');
357 358
358 test(function() { 359 promise_test(function() {
359 var req = new Request(URL, {method: 'POST', body: 'hello'}); 360 var req = new Request(URL, {method: 'POST', body: 'hello'});
360 assert_false(req.bodyUsed, 361 assert_false(req.bodyUsed,
361 'Request should not be flagged as used if it has not been ' + 362 'Request should not be flagged as used if it has not been ' +
362 'consumed.'); 363 'consumed.');
364 const originalBody = req.body;
365 assert_not_equals(req.body, null, 'Request body');
363 var req2 = new Request(req); 366 var req2 = new Request(req);
364 assert_true(req.bodyUsed, 367 assert_true(req.bodyUsed,
365 'Request should be flagged as used if it has been consumed.'); 368 'Request should be flagged as used if it has been consumed.');
366 assert_false(req2.bodyUsed, 369 assert_false(req2.bodyUsed,
367 'Request should not be flagged as used if it has not been ' + 370 'Request should not be flagged as used if it has not been ' +
368 'consumed.'); 371 'consumed.');
372 assert_equals(req.body, originalBody,
373 'The body property must not change.');
374 assert_not_equals(req.body, req2.body,
375 'A new Request has a new body property');
369 // See https://crbug.com/501195. 376 // See https://crbug.com/501195.
370 assert_throws( 377 assert_throws(
371 {name: 'TypeError'}, 378 {name: 'TypeError'},
372 function() { new Request(req); }, 379 function() { new Request(req); },
373 'Request construction should throw if used.'); 380 'Request construction should throw if used.');
374 }, 'Request construction without body behavior regardning "bodyUsed"'); 381 }, 'Request construction regarding "body" and "bodyUsed"');
375 382
376 test(function() { 383 test(function() {
377 var req = new Request(URL, {method: 'POST', body: 'hello'}); 384 var req = new Request(URL, {method: 'POST', body: 'hello'});
385 const originalBody = req.body;
386 assert_not_equals(req.body, null, 'Request body');
378 assert_false(req.bodyUsed, 387 assert_false(req.bodyUsed,
379 'Request should not be flagged as used if it has not been ' + 388 'Request should not be flagged as used if it has not been ' +
380 'consumed.'); 389 'consumed.');
381 assert_throws( 390 assert_throws(
382 {name: 'TypeError'}, 391 {name: 'TypeError'},
383 function() { new Request(req, {method: 'GET'}); }, 392 function() { new Request(req, {method: 'GET'}); },
384 'A get request may not have body.'); 393 'A get request may not have body.');
385 394
386 assert_false(req.bodyUsed, 'After the GET case'); 395 assert_false(req.bodyUsed, 'After the GET case');
396 assert_equals(req.body, originalBody,
397 'After the GET case: The body property must not change.');
387 398
388 assert_throws( 399 assert_throws(
389 {name: 'TypeError'}, 400 {name: 'TypeError'},
390 function() { new Request(req, {method: 'CONNECT'}); }, 401 function() { new Request(req, {method: 'CONNECT'}); },
391 'Request() with a forbidden method must throw.'); 402 'Request() with a forbidden method must throw.');
392 403
393 assert_false(req.bodyUsed, 'After the forbidden method case'); 404 assert_false(req.bodyUsed, 'After the forbidden method case');
405 assert_equals(req.body, originalBody,
406 'After the forbidden method case: ' +
407 'The body property must not change.');
394 408
395 var req2 = new Request(req); 409 var req2 = new Request(req);
396 assert_true(req.bodyUsed, 410 assert_true(req.bodyUsed,
397 'Request should be flagged as used if it has been consumed.'); 411 'Request should be flagged as used if it has been consumed.');
398 }, 'Request construction failure should not set "bodyUsed"'); 412 }, 'Request construction failure should not set "bodyUsed"');
399 413
400 test(function() { 414 test(function() {
401 assert_equals(new Request(URL).referrer, 'about:client'); 415 assert_equals(new Request(URL).referrer, 'about:client');
402 assert_equals(new Request(URL).referrerPolicy, ''); 416 assert_equals(new Request(URL).referrerPolicy, '');
403 }, 'Request without RequestInit.'); 417 }, 'Request without RequestInit.');
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after
735 promise_test(function(t) { 749 promise_test(function(t) {
736 var req = new Request('http://localhost/', 750 var req = new Request('http://localhost/',
737 {method: 'POST', 751 {method: 'POST',
738 body: new Blob([''], {type: 'Text/Plain'})}); 752 body: new Blob([''], {type: 'Text/Plain'})});
739 return req.blob() 753 return req.blob()
740 .then(function(blob) { 754 .then(function(blob) {
741 assert_equals(blob.type, 'text/plain'); 755 assert_equals(blob.type, 'text/plain');
742 assert_equals(req.headers.get('Content-Type'), 'text/plain'); 756 assert_equals(req.headers.get('Content-Type'), 'text/plain');
743 }); 757 });
744 }, 'MIME type for Blob with non-empty type'); 758 }, 'MIME type for Blob with non-empty type');
745
746 promise_test(function(t) { 759 promise_test(function(t) {
747 var req = new Request('http://localhost/', 760 var req = new Request('http://localhost/',
748 {method: 'POST', body: new FormData()}); 761 {method: 'POST', body: new FormData()});
749 return req.blob() 762 return req.blob()
750 .then(function(blob) { 763 .then(function(blob) {
751 assert_equals(blob.type.indexOf('multipart/form-data; boundary='), 764 assert_equals(blob.type.indexOf('multipart/form-data; boundary='),
752 0); 765 0);
753 assert_equals(req.headers.get('Content-Type') 766 assert_equals(req.headers.get('Content-Type')
754 .indexOf('multipart/form-data; boundary='), 767 .indexOf('multipart/form-data; boundary='),
755 0); 768 0);
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
812 {method: 'POST', 825 {method: 'POST',
813 credentials: 'include', 826 credentials: 'include',
814 body: 'this is a body'}); 827 body: 'this is a body'});
815 828
816 return req.text() 829 return req.text()
817 .then(t => { 830 .then(t => {
818 assert_equals(t, 'this is a body'); 831 assert_equals(t, 'this is a body');
819 }); 832 });
820 }, 'Credentials and body can both be set.'); 833 }, 'Credentials and body can both be set.');
821 834
835 promise_test(() => {
836 var controller;
837 var stream = new ReadableStream({start: c => controller = c});
838 controller.enqueue(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f]));
839 controller.enqueue(new Uint8Array([0x77, 0x6f, 0x72, 0x6c, 0x64]));
840 controller.close();
841 assert_false(stream.locked);
842 var request = new Request(URL, {method: 'POST', body: stream});
843 assert_equals(request.body, stream, 'body property');
844 var p = request.text().then(t => {
845 assert_equals(t, 'helloworld');
846 });
847 assert_true(stream.locked);
848 return p;
849 }, 'Request constructed with a stream');
850
851 promise_test(() => {
852 const request = new Request(URL, {method: 'POST', body: 'hello'});
853 return readableStreamToArray(request.body).then(chunks => {
854 const decoder = new TextDecoder('utf-8');
855 let r = '';
856 for (const chunk of chunks) {
857 r += decoder.decode(chunk, {stream: true});
858 }
859 r += decoder.decode();
860 assert_equals(r, 'hello');
861 });
862 }, 'Request constructed with a String / Read from body stream');
863
864 promise_test(() => {
865 var controller;
866 var stream = new ReadableStream({start: c => controller = c});
867 controller.enqueue(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f]));
868 controller.enqueue(new Uint8Array([0x77, 0x6f, 0x72, 0x6c, 0x64]));
869 controller.close();
870
871 var request = new Request(URL, {method: 'POST', body: stream});
872 return readableStreamToArray(request.body).then(chunks => {
873 var decoder = new TextDecoder('utf-8');
874 var r = '';
875 for (var chunk of chunks) {
876 r += decoder.decode(chunk, {stream: true});
877 }
878 r += decoder.decode();
879 assert_equals(r, 'helloworld');
880 });
881 }, 'Request constructed with a stream / Read from body stream');
882
883 promise_test(t => {
884 var controller;
885 var stream = new ReadableStream({start: c => controller = c});
886 setTimeout(() => {
887 controller.enqueue(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f]));
888 controller.enqueue(new Uint8Array([0x77, 0x6f, 0x72, 0x6c, 0x64]));
889 controller.error();
890 }, 1);
891 var request = new Request(URL, {method: 'POST', body: stream});
892 return promise_rejects(t, TypeError(), request.text());
893 }, 'Response constructed with an errored stream');
894
895 promise_test(t => {
896 var controller;
897 var stream = new ReadableStream({start: c => controller = c});
898 stream.getReader();
899 var request = new Request(URL, {method: 'POST', body: stream});
900 return promise_rejects(t, TypeError(), request.text());
901 }, 'Request constructed with a locked stream');
902
903 promise_test(t => {
904 var controller;
905 var stream = new ReadableStream({start: c => controller = c});
906 setTimeout(() => controller.enqueue(), 1);
907 var request = new Request(URL, {method: 'POST', body: stream});
908 return promise_rejects(t, TypeError(), request.text());
909 }, 'Request constructed stream with an undefined chunk');
910
911 promise_test(t => {
912 var controller;
913 var stream = new ReadableStream({start: c => controller = c});
914 setTimeout(() => controller.enqueue(null), 1);
915 var request = new Request(URL, {method: 'POST', body: stream});
916 return promise_rejects(t, TypeError(), request.text());
917 }, 'Request constructed stream with a null chunk');
918
919 promise_test(t => {
920 var controller;
921 var stream = new ReadableStream({start: c => controller = c});
922 setTimeout(() => controller.enqueue('hello'), 1);
923 var request = new Request(URL, {method: 'POST', body: stream});
924 return promise_rejects(t, TypeError(), request.text());
925 }, 'Request constructed stream with a string chunk');
926
927
822 done(); 928 done();
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/LayoutTests/http/tests/serviceworker/webexposed/global-interface-listing-service-worker-expected.txt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698