1 if (self
.importScripts
) {
2 importScripts('/fetch/resources/fetch-test-helpers.js');
5 function read_until_end(reader
) {
8 return reader
.read().then(function(r
) {
20 promise_test(function(t
) {
21 return fetch('/fetch/resources/doctype.html').then(function(res
) {
22 var stream
= res
.body
;
23 var reader
= stream
.getReader();
24 assert_throws({name
: 'TypeError'}, function() { stream
.getReader() });
26 var another
= stream
.getReader();
27 assert_not_equals(another
, reader
);
29 }, 'ReadableStreamReader acquisition / releasing');
31 promise_test(function(t
) {
32 return fetch('/fetch/resources/doctype.html').then(function(res
) {
33 var reader
= res
.body
.getReader();
34 return read_until_end(reader
);
35 }).then(function(chunks
) {
37 for (var chunk
of chunks
) {
38 assert_equals(chunk
.constructor, Uint8Array
, 'chunk\'s type');
39 size
+= chunk
.byteLength
;
41 var buffer
= new Uint8Array(size
);
43 for (var chunk
of chunks
) {
44 buffer
.set(new Uint8Array(chunk
), offset
);
45 offset
+= chunk
.byteLength
;
47 return new TextDecoder().decode(buffer
);
48 }).then(function(string
) {
49 assert_equals(string
, '<!DOCTYPE html>\n');
51 }, 'read contents with ReadableStreamReader');
53 promise_test(function(t
) {
54 return fetch('/fetch/resources/progressive.php').then(function(res
) {
55 assert_false(res
.bodyUsed
);
56 var reader
= res
.body
.getReader();
57 assert_true(res
.bodyUsed
);
59 }).then(unreached_rejection(t
), function() {
60 // text() should fail because bodyUsed is set.
62 }, 'acquiring a reader should set bodyUsed.');
64 promise_test(function(t
) {
66 return fetch('/fetch/resources/progressive.php').then(function(res
) {
68 assert_false(res
.bodyUsed
);
69 var p
= res
.arrayBuffer();
70 assert_true(res
.bodyUsed
);
71 assert_throws({name
: 'TypeError'}, function() { res
.body
.getReader() });
73 }).then(function(buffer
) {
74 assert_equals(buffer
.byteLength
, 190);
75 // Now we can obtain a (closed) reader.
76 return response
.body
.getReader().closed
;
78 }, 'Setting bodyUsed means the body is locked.');
80 promise_test(function(t
) {
81 return fetch('/fetch/resources/slow-failure.cgi').then(function(res
) {
82 return res
.text().then(function() {
83 assert_unreached('text() should fail');
85 return res
.body
.getReader().closed
.then(function() {
86 assert_unreached('res.body should be errored');
90 }, 'Error in text() should be propagated to the body stream.');
92 promise_test(function(t
) {
96 return fetch('/fetch/resources/progressive.php').then(function(res
) {
98 reader
= res
.body
.getReader();
100 }).then(function(r
) {
101 assert_false(r
.done
);
102 read
+= r
.value
.byteLength
;
103 // Make sure that we received something but we didn't receive all.
104 assert_not_equals(read
, 0);
105 assert_not_equals(read
, 190);
107 reader
.releaseLock();
108 var original_body
= original
.body
;
109 var clone
= original
.clone();
110 assert_not_equals(original
.body
, clone
.body
);
111 assert_not_equals(original
.body
, original_body
);
112 assert_not_equals(clone
.body
, original_body
);
113 assert_throws({name
: 'TypeError'}, function() {
114 original_body
.getReader();
116 var reader1
= original
.body
.getReader();
117 var reader2
= clone
.body
.getReader();
118 return Promise
.all([read_until_end(reader1
), read_until_end(reader2
)]);
119 }).then(function(r
) {
122 for (var chunk
of r
[0]) {
123 read1
+= chunk
.byteLength
;
125 for (var chunk
of r
[1]) {
126 read2
+= chunk
.byteLength
;
128 // Make sure that we received all data in total.
129 assert_equals(read
+ read1
, 190);
130 assert_equals(read
+ read2
, 190);
132 }, 'Clone after reading partially');
134 promise_test(function(t
) {
135 return fetch('/fetch/resources/progressive.php').then(function(res
) {
138 }).then(function(text
) {
139 assert_equals(text
, '');
141 }, 'Cancelling stream stops downloading.');
143 promise_test(function(t
) {
144 return fetch('/fetch/resources/progressive.php').then(function(res
) {
145 var clone
= res
.clone();
147 return Promise
.all([res
.arrayBuffer(), clone
.arrayBuffer()]);
148 }).then(function(r
) {
149 assert_equals(r
[0].byteLength
, 0);
150 assert_equals(r
[1].byteLength
, 190);
152 }, 'Cancelling stream should not affect cloned one.');
154 promise_test(function(t
) {
156 return fetch('/fetch/resources/progressive.php').then(function(res
) {
159 assert_throws({name
: 'TypeError'}, function() { stream
.getReader() });
161 }).then(function(text
) {
162 assert_equals(text
.length
, 190);
163 return stream
.getReader().read();
164 }).then(function(r
) {
166 assert_equals(r
.value
, undefined);
168 }, 'Accessing body when processing text().');