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

Unified Diff: third_party/WebKit/LayoutTests/http/tests/fetch/script-tests/body-mixin.js

Issue 1418813004: [Fetch API] Reflect spec changes of bodyUsed property (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/LayoutTests/http/tests/fetch/script-tests/body-mixin.js
diff --git a/third_party/WebKit/LayoutTests/http/tests/fetch/script-tests/body-mixin.js b/third_party/WebKit/LayoutTests/http/tests/fetch/script-tests/body-mixin.js
index ea9e07b8ccbcab8f3c15289ca0fb92c836ac2656..491f4b9555194e95baf3e7d1432071dc37e9ded4 100644
--- a/third_party/WebKit/LayoutTests/http/tests/fetch/script-tests/body-mixin.js
+++ b/third_party/WebKit/LayoutTests/http/tests/fetch/script-tests/body-mixin.js
@@ -12,6 +12,16 @@ function readStream(reader, values) {
return reader.closed;
}
+function isLocked(stream) {
+ try {
+ var reader = stream.getReader();
+ reader.releaseLock();
+ return false;
+ } catch(e) {
+ return true;
+ }
+}
+
promise_test(function(test) {
return fetch('/fetch/resources/doctype.html')
.then(function(response) {
@@ -63,55 +73,90 @@ promise_test(function(test) {
}, 'FetchTwiceTest');
promise_test(function(test) {
+ var response;
return fetch('/fetch/resources/doctype.html')
- .then(function(response) {
- return response.arrayBuffer();
+ .then(function(res) {
+ response = res;
+ assert_false(response.bodyUsed);
+ var p = response.arrayBuffer();
+ assert_true(response.bodyUsed);
+ assert_true(isLocked(response.body));
+ return p;
})
.then(function(b) {
+ assert_true(isLocked(response.body));
assert_equals(b.byteLength, 16);
})
}, 'ArrayBufferTest');
promise_test(function(test) {
+ var response;
return fetch('/fetch/resources/doctype.html')
- .then(function(response) {
- return response.blob();
+ .then(function(res) {
+ response = res;
+ assert_false(response.bodyUsed);
+ var p = response.blob();
+ assert_true(response.bodyUsed);
+ assert_true(isLocked(response.body));
+ return p;
})
.then(function(blob) {
+ assert_true(isLocked(response.body));
assert_equals(blob.size, 16);
assert_equals(blob.type, 'text/html');
})
}, 'BlobTest');
promise_test(function(test) {
+ var response;
return fetch('/fetch/resources/doctype.html')
- .then(function(response) {
- return response.json();
+ .then(function(res) {
+ response = res;
+ assert_false(response.bodyUsed);
+ var p = response.json();
+ assert_true(response.bodyUsed);
+ assert_true(isLocked(response.body));
+ return p;
})
.then(
test.unreached_func('json() must fail'),
function(e) {
+ assert_true(isLocked(response.body));
assert_equals(e.name, 'SyntaxError', 'expected JSON error');
})
}, 'JSONFailedTest');
promise_test(function(test) {
+ var response;
return fetch('/fetch/resources/simple.json')
- .then(function(response) {
- return response.json();
+ .then(function(res) {
+ response = res;
+ assert_false(response.bodyUsed);
+ var p = response.json();
+ assert_true(response.bodyUsed);
+ assert_true(isLocked(response.body));
+ return p;
})
.then(function(json) {
+ assert_true(isLocked(response.body));
assert_equals(json['a'], 1);
assert_equals(json['b'], 2);
})
}, 'JSONTest');
promise_test(function(test) {
+ var response;
return fetch('/fetch/resources/doctype.html')
- .then(function(response) {
- return response.text();
+ .then(function(res) {
+ response = res;
+ assert_false(response.bodyUsed);
+ var p = response.text();
+ assert_true(response.bodyUsed);
+ assert_true(isLocked(response.body));
+ return p;
})
.then(function(text) {
+ assert_true(isLocked(response.body));
assert_equals(text, '<!DOCTYPE html>\n');
})
}, 'TextTest');
@@ -119,45 +164,110 @@ promise_test(function(test) {
promise_test(function(test) {
return fetch('/fetch/resources/non-ascii.txt')
.then(function(response) {
- return response.text();
+ assert_false(response.bodyUsed);
+ var p = response.text();
+ assert_true(response.bodyUsed);
+ return p;
})
.then(function(text) {
assert_equals(text, '\u4e2d\u6587 Gem\u00fcse\n');
})
}, 'NonAsciiTextTest');
-promise_test(function(test) {
- var expected = '';
- for (var i = 0; i < 100; ++i)
- expected += i;
+test(t => {
+ var req = new Request('/');
+ assert_false(req.bodyUsed);
+ req.text();
+ assert_false(req.bodyUsed);
+ }, 'BodyUsedShouldNotBeSetForNullBody');
- var decoder = new TextDecoder();
- var actual = '';
- var response;
- var reader;
- return fetch('/fetch/resources/progressive.php')
- .then(function(res) {
- response = res;
- reader = response.body.getReader();
- return reader.read();
- })
- .then(function(r) {
- assert_false(r.done);
- actual += decoder.decode(r.value, {stream: true});
- })
- .then(function() {
- return response.text().then(unreached_fulfillment(test), function() {
- // response.text() should fail because we have a reader.
- });
- })
- .then(function() {
- reader.releaseLock();
- return response.arrayBuffer();
- })
- .then(function(buffer) {
- actual += decoder.decode(buffer);
- assert_equals(actual, expected);
- })
- }, 'PartiallyReadFromStreamAndReadArrayBufferTest');
+test(t => {
+ var req = new Request('/', {method: 'POST', body: ''});
+ assert_false(req.bodyUsed);
+ req.text();
+ assert_true(req.bodyUsed);
+ }, 'BodyUsedShouldBeSetForEmptyBody');
+
+test(t => {
+ var res = new Response('');
+ assert_false(res.bodyUsed);
+ var reader = res.body.getReader();
+ assert_false(res.bodyUsed);
+ reader.read();
+ assert_true(res.bodyUsed);
+ }, 'BodyUsedShouldBeSetWhenRead');
+
+test(t => {
+ var res = new Response('');
+ assert_false(res.bodyUsed);
+ var reader = res.body.getReader();
+ assert_false(res.bodyUsed);
+ reader.cancel();
+ assert_true(res.bodyUsed);
+ }, 'BodyUsedShouldBeSetWhenCancelled');
+
+promise_test(t => {
+ var res = new Response('');
+ res.body.cancel();
+ return res.arrayBuffer().then(unreached_fulfillment(t), e => {
+ assert_equals(e.name, 'TypeError');
+ });
+ }, 'Used => arrayBuffer');
+
+promise_test(t => {
+ var res = new Response('');
+ res.body.cancel();
+ return res.blob().then(unreached_fulfillment(t), e => {
+ assert_equals(e.name, 'TypeError');
+ });
+ }, 'Used => blob');
+
+promise_test(t => {
+ var res = new Response('');
+ res.body.cancel();
+ return res.json().then(unreached_fulfillment(t), e => {
+ assert_equals(e.name, 'TypeError');
+ });
+ }, 'Used => json');
+
+promise_test(t => {
+ var res = new Response('');
+ res.body.cancel();
+ return res.text().then(unreached_fulfillment(t), e => {
+ assert_equals(e.name, 'TypeError');
+ });
+ }, 'Used => text');
+
+promise_test(t => {
+ var res = new Response('');
+ res.body.getReader();
+ return res.arrayBuffer().then(unreached_fulfillment(t), e => {
+ assert_equals(e.name, 'TypeError');
+ });
+ }, 'Locked => arrayBuffer');
+
+promise_test(t => {
+ var res = new Response('');
+ res.body.getReader();
+ return res.blob().then(unreached_fulfillment(t), e => {
+ assert_equals(e.name, 'TypeError');
+ });
+ }, 'Locked => blob');
+
+promise_test(t => {
+ var res = new Response('');
+ res.body.getReader();
+ return res.json().then(unreached_fulfillment(t), e => {
+ assert_equals(e.name, 'TypeError');
+ });
+ }, 'Locked => json');
+
+promise_test(t => {
+ var res = new Response('');
+ res.body.getReader();
+ return res.text().then(unreached_fulfillment(t), e => {
+ assert_equals(e.name, 'TypeError');
+ });
+ }, 'Locked => text');
done();

Powered by Google App Engine
This is Rietveld 408576698