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

Side by Side Diff: third_party/WebKit/LayoutTests/external/wpt/fetch/api/request/request-consume.html

Issue 2778753002: Import //fetch from Web Platform Tests. (Closed)
Patch Set: Baselines. Created 3 years, 8 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
(Empty)
1 <!doctype html>
2 <html>
3 <head>
4 <meta charset="utf-8">
5 <title>Request consume</title>
6 <meta name="help" href="https://fetch.spec.whatwg.org/#request">
7 <meta name="help" href="https://fetch.spec.whatwg.org/#body-mixin">
8 <meta name="author" title="Canon Research France" href="https://www.crf.cano n.fr">
9 <script src="/resources/testharness.js"></script>
10 <script src="/resources/testharnessreport.js"></script>
11 <script src="../resources/utils.js"></script>
12 </head>
13 <body>
14 <script>
15 function checkBodyText(request, expectedBody) {
16 return request.text().then(function(bodyAsText) {
17 assert_equals(bodyAsText, expectedBody, "Retrieve and verify request's b ody");
18 assert_true(request.bodyUsed, "body as text: bodyUsed turned true");
19 });
20 }
21
22 function checkBodyBlob(request, expectedBody, checkContentType) {
23 return request.blob().then(function(bodyAsBlob) {
24 if (checkContentType)
25 assert_equals(bodyAsBlob.type, "text/plain", "Blob body type should be computed from the request Content-Type");
26
27 var promise = new Promise(function (resolve, reject) {
28 var reader = new FileReader();
29 reader.onload = function(evt) {
30 resolve(reader.result)
31 };
32 reader.onerror = function() {
33 reject("Blob's reader failed");
34 };
35 reader.readAsText(bodyAsBlob);
36 });
37 return promise.then(function(body) {
38 assert_equals(body, expectedBody, "Retrieve and verify request's body" );
39 assert_true(request.bodyUsed, "body as blob: bodyUsed turned true");
40 });
41 });
42 }
43
44 function checkBodyArrayBuffer(request, expectedBody) {
45 return request.arrayBuffer().then(function(bodyAsArrayBuffer) {
46 validateBufferFromString(bodyAsArrayBuffer, expectedBody, "Retrieve and verify request's body");
47 assert_true(request.bodyUsed, "body as arrayBuffer: bodyUsed turned true ");
48 });
49 }
50
51 function checkBodyJSON(request, expectedBody) {
52 return request.json().then(function(bodyAsJSON) {
53 var strBody = JSON.stringify(bodyAsJSON)
54 assert_equals(strBody, expectedBody, "Retrieve and verify request's body ");
55 assert_true(request.bodyUsed, "body as json: bodyUsed turned true");
56 });
57 }
58
59 function checkBodyFormData(request, expectedBody) {
60 return request.formData().then(function(bodyAsFormData) {
61 assert_true(bodyAsFormData instanceof FormData, "Should receive a FormDa ta");
62 assert_true(request.bodyUsed, "body as formData: bodyUsed turned true");
63 });
64 }
65
66 function checkRequestBody(body, expected, bodyType) {
67 promise_test(function(test) {
68 var request = new Request("", {"method": "POST", "body": body, "headers" : [["Content-Type", "text/PLAIN"]] });
69 assert_false(request.bodyUsed, "bodyUsed is false at init");
70 return checkBodyText(request, expected);
71 }, "Consume " + bodyType + " request's body as text");
72 promise_test(function(test) {
73 var request = new Request("", {"method": "POST", "body": body });
74 assert_false(request.bodyUsed, "bodyUsed is false at init");
75 return checkBodyBlob(request, expected);
76 }, "Consume " + bodyType + " request's body as blob");
77 promise_test(function(test) {
78 var request = new Request("", {"method": "POST", "body": body });
79 assert_false(request.bodyUsed, "bodyUsed is false at init");
80 return checkBodyArrayBuffer(request, expected);
81 }, "Consume " + bodyType + " request's body as arrayBuffer");
82 promise_test(function(test) {
83 var request = new Request("", {"method": "POST", "body": body });
84 assert_false(request.bodyUsed, "bodyUsed is false at init");
85 return checkBodyJSON(request, expected);
86 }, "Consume " + bodyType + " request's body as JSON");
87 }
88
89 var textData = JSON.stringify("This is response's body");
90 var blob = new Blob([textData], { "type" : "text/plain" });
91
92 checkRequestBody(textData, textData, "String");
93
94 var string = "\"123456\"";
95 function getArrayBuffer() {
96 var arrayBuffer = new ArrayBuffer(8);
97 var int8Array = new Int8Array(arrayBuffer);
98 for (var cptr = 0; cptr < 8; cptr++)
99 int8Array[cptr] = string.charCodeAt(cptr);
100 return arrayBuffer;
101 }
102
103 function getArrayBufferWithZeros() {
104 var arrayBuffer = new ArrayBuffer(10);
105 var int8Array = new Int8Array(arrayBuffer);
106 for (var cptr = 0; cptr < 8; cptr++)
107 int8Array[cptr + 1] = string.charCodeAt(cptr);
108 return arrayBuffer;
109 }
110
111 checkRequestBody(getArrayBuffer(), string, "ArrayBuffer");
112 checkRequestBody(new Uint8Array(getArrayBuffer()), string, "Uint8Array");
113 checkRequestBody(new Int8Array(getArrayBufferWithZeros(), 1, 8), string, "In t8Array");
114 checkRequestBody(new Float32Array(getArrayBuffer()), string, "Float32Array") ;
115 checkRequestBody(new DataView(getArrayBufferWithZeros(), 1, 8), string, "Dat aView");
116
117 promise_test(function(test) {
118 var formData = new FormData();
119 formData.append("name", "value")
120 var request = new Request("", {"method": "POST", "body": formData });
121 assert_false(request.bodyUsed, "bodyUsed is false at init");
122 return checkBodyFormData(request, formData);
123 }, "Consume FormData request's body as FormData");
124
125 function checkBlobResponseBody(blobBody, blobData, bodyType, checkFunction) {
126 promise_test(function(test) {
127 var response = new Response(blobBody);
128 assert_false(response.bodyUsed, "bodyUsed is false at init");
129 return checkFunction(response, blobData);
130 }, "Consume blob response's body as " + bodyType);
131 }
132
133 checkBlobResponseBody(blob, textData, "blob", checkBodyBlob);
134 checkBlobResponseBody(blob, textData, "text", checkBodyText);
135 checkBlobResponseBody(blob, textData, "json", checkBodyJSON);
136 checkBlobResponseBody(blob, textData, "arrayBuffer", checkBodyArrayBuffer);
137 checkBlobResponseBody(new Blob([""]), "", "blob (empty blob as input)", chec kBodyBlob);
138
139 var goodJSONValues = ["null", "1", "true", "\"string\""];
140 goodJSONValues.forEach(function(value) {
141 promise_test(function(test) {
142 var request = new Request("", {"method": "POST", "body": value});
143 return request.json().then(function(v) {
144 assert_equals(v, JSON.parse(value));
145 });
146 }, "Consume JSON from text: '" + JSON.stringify(value) + "'");
147 });
148
149 var badJSONValues = ["undefined", "{", "a", "["];
150 badJSONValues.forEach(function(value) {
151 promise_test(function(test) {
152 var request = new Request("", {"method": "POST", "body": value});
153 return promise_rejects(test, new SyntaxError(), request.json());
154 }, "Trying to consume bad JSON text as JSON: '" + value + "'");
155 });
156 </script>
157 </body>
158 </html>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698