| OLD | NEW |
| 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 function consume(reader) { | 5 function consume(reader) { |
| 6 var chunks = []; | 6 var chunks = []; |
| 7 function rec(reader) { | 7 function rec(reader) { |
| 8 return reader.read().then(function(r) { | 8 return reader.read().then(function(r) { |
| 9 if (r.done) { | 9 if (r.done) { |
| 10 return chunks; | 10 return chunks; |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 function() { | 227 function() { |
| 228 new Response(new Blob(), {statusText: text}); | 228 new Response(new Blob(), {statusText: text}); |
| 229 }, | 229 }, |
| 230 'new Response with invalid statusText (' + text + | 230 'new Response with invalid statusText (' + text + |
| 231 ') must throw'); | 231 ') must throw'); |
| 232 }); | 232 }); |
| 233 }, 'Response throw error test'); | 233 }, 'Response throw error test'); |
| 234 | 234 |
| 235 promise_test(function(t) { | 235 promise_test(function(t) { |
| 236 var res = new Response('hello'); | 236 var res = new Response('hello'); |
| 237 return consume(res.body.getReader()).then(function(chunks) { | |
| 238 return decode(chunks); | |
| 239 }).then(function(text) { | |
| 240 assert_equals(text, 'hello'); | |
| 241 return res.body.getReader().read(); | |
| 242 }).then(function(r) { | |
| 243 assert_true(r.done); | |
| 244 return res.text(); | |
| 245 }).then(function(r) { | |
| 246 assert_equals(r, ''); | |
| 247 }); | |
| 248 }, 'Read Response body via stream'); | |
| 249 | |
| 250 promise_test(function(t) { | |
| 251 var res = new Response('hello'); | |
| 252 res.body.cancel(); | 237 res.body.cancel(); |
| 253 return res.body.getReader().read().then(function(r) { | 238 return res.body.getReader().read().then(function(r) { |
| 254 assert_true(r.done); | 239 assert_true(r.done); |
| 255 return res.text(); | |
| 256 }).then(function(r) { | |
| 257 assert_equals(r, ''); | |
| 258 }); | 240 }); |
| 259 }, 'Cancel body stream on Response'); | 241 }, 'Cancel body stream on Response'); |
| 260 | 242 |
| 261 promise_test(function(t) { | 243 promise_test(function(t) { |
| 262 return new Response().text().then(text => { | 244 return new Response().text().then(text => { |
| 263 assert_equals(text.constructor, String); | 245 assert_equals(text.constructor, String); |
| 264 assert_equals(text, ''); | 246 assert_equals(text, ''); |
| 265 }); | 247 }); |
| 266 }, 'call text() on null body response'); | 248 }, 'call text() on null body response'); |
| 267 | 249 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 280 }); | 262 }); |
| 281 }, 'call blob() on null body response'); | 263 }, 'call blob() on null body response'); |
| 282 | 264 |
| 283 promise_test(function(t) { | 265 promise_test(function(t) { |
| 284 return new Response().json().then(unreached_rejection(t), e => { | 266 return new Response().json().then(unreached_rejection(t), e => { |
| 285 assert_equals(e.constructor, SyntaxError); | 267 assert_equals(e.constructor, SyntaxError); |
| 286 }); | 268 }); |
| 287 }, 'call json() on null body response'); | 269 }, 'call json() on null body response'); |
| 288 | 270 |
| 289 promise_test(function(t) { | 271 promise_test(function(t) { |
| 290 // TODO(yhirano): In the current implementation, The body stream always | |
| 291 // consists of one chunk and hence, we cannot create a meaning test case | |
| 292 // here. Fix this test case when reading into a size-specified buffer gets | |
| 293 // possible. | |
| 294 var size = 8 * 1024 * 1024; | |
| 295 var buffer = new ArrayBuffer(size); | |
| 296 var blob = new Blob([buffer]); | |
| 297 | |
| 298 var res = new Response(blob); | |
| 299 var reader = res.body.getReader(); | |
| 300 var head; | |
| 301 return reader.read().then(function(r) { | |
| 302 assert_false(r.done); | |
| 303 head = r.value; | |
| 304 assert_not_equals(head.byteLength, 0); | |
| 305 // TODO(yhirano): See above. | |
| 306 // assert_not_equals(head.byteLength, size); | |
| 307 reader.releaseLock(); | |
| 308 return res.arrayBuffer(); | |
| 309 }).then(function(buffer) { | |
| 310 assert_equals(buffer.byteLength + head.byteLength, size); | |
| 311 return res.body.getReader().read(); | |
| 312 }).then(function(r) { | |
| 313 assert_true(r.done); | |
| 314 }); | |
| 315 }, 'Partial read on Response'); | |
| 316 | |
| 317 promise_test(function(t) { | |
| 318 var res = new Response('hello'); | 272 var res = new Response('hello'); |
| 319 var body = res.body; | 273 var body = res.body; |
| 320 var clone = res.clone(); | 274 var clone = res.clone(); |
| 275 assert_false(res.bodyUsed); |
| 276 assert_false(clone.bodyUsed); |
| 321 assert_not_equals(res.body, body); | 277 assert_not_equals(res.body, body); |
| 322 assert_not_equals(res.body, clone.body); | 278 assert_not_equals(res.body, clone.body); |
| 323 assert_not_equals(body, clone.body); | 279 assert_not_equals(body, clone.body); |
| 324 assert_throws({name: 'TypeError'}, function() { body.getReader(); }); | 280 assert_throws({name: 'TypeError'}, function() { body.getReader(); }); |
| 325 return Promise.all([res.text(), clone.text()]).then(function(r) { | 281 return Promise.all([res.text(), clone.text()]).then(function(r) { |
| 326 assert_equals(r[0], 'hello'); | 282 assert_equals(r[0], 'hello'); |
| 327 assert_equals(r[1], 'hello'); | 283 assert_equals(r[1], 'hello'); |
| 328 return Promise.all([res.text(), clone.text(), res.clone().text()]); | |
| 329 }).then(function(r) { | |
| 330 assert_equals(r[0], ''); | |
| 331 assert_equals(r[1], ''); | |
| 332 assert_equals(r[2], ''); | |
| 333 }); | 284 }); |
| 334 }, 'Clone on Response (text)'); | 285 }, 'Clone on Response (text)'); |
| 335 | 286 |
| 336 promise_test(function(t) { | 287 promise_test(function(t) { |
| 337 var res = new Response('hello'); | 288 var res = new Response('hello'); |
| 338 var body = res.body; | 289 var body = res.body; |
| 339 var clone = res.clone(); | 290 var clone = res.clone(); |
| 291 assert_false(res.bodyUsed); |
| 292 assert_false(clone.bodyUsed); |
| 340 assert_not_equals(res.body, body); | 293 assert_not_equals(res.body, body); |
| 341 assert_not_equals(res.body, clone.body); | 294 assert_not_equals(res.body, clone.body); |
| 342 assert_not_equals(body, clone.body); | 295 assert_not_equals(body, clone.body); |
| 343 assert_throws({name: 'TypeError'}, function() { body.getReader(); }); | 296 assert_throws({name: 'TypeError'}, function() { body.getReader(); }); |
| 344 var reader1 = res.body.getReader(); | 297 var reader1 = res.body.getReader(); |
| 345 var reader2 = clone.body.getReader(); | 298 var reader2 = clone.body.getReader(); |
| 346 return Promise.all([consume(reader1), consume(reader2)]).then(function(r) { | 299 return Promise.all([consume(reader1), consume(reader2)]).then(function(r) { |
| 347 assert_equals(decode(r[0]), 'hello'); | 300 assert_equals(decode(r[0]), 'hello'); |
| 348 assert_equals(decode(r[1]), 'hello'); | 301 assert_equals(decode(r[1]), 'hello'); |
| 349 return Promise.all([res.text(), clone.text(), res.clone().text()]); | |
| 350 }).then(function(r) { | |
| 351 assert_equals(r[0], ''); | |
| 352 assert_equals(r[1], ''); | |
| 353 assert_equals(r[2], ''); | |
| 354 }); | 302 }); |
| 355 }, 'Clone on Response (manual read)'); | 303 }, 'Clone on Response (manual read)'); |
| 356 | 304 |
| 357 promise_test(function(t) { | |
| 358 var res = new Response('hello'); | |
| 359 var clone = res.clone(); | |
| 360 res.body.cancel(); | |
| 361 return Promise.all([res.text(), clone.text()]).then(function(r) { | |
| 362 assert_equals(r[0], ''); | |
| 363 assert_equals(r[1], 'hello'); | |
| 364 }); | |
| 365 }, 'Clone and Cancel on Response'); | |
| 366 | |
| 367 promise_test(function(t) { | |
| 368 var res = new Response('hello'); | |
| 369 res.body.cancel(); | |
| 370 var clone = res.clone(); | |
| 371 return Promise.all([res.blob(), clone.blob()]).then(function(r) { | |
| 372 assert_equals(r[0].type, 'text/plain;charset=utf-8', 'cloned type'); | |
| 373 assert_equals(r[1].type, 'text/plain;charset=utf-8', 'original type'); | |
| 374 assert_equals(r[0].size, 0, 'original size'); | |
| 375 assert_equals(r[1].size, 0, 'cloned size'); | |
| 376 }); | |
| 377 }, 'Cancel and Clone on Response'); | |
| 378 | |
| 379 // Tests for MIME types. | 305 // Tests for MIME types. |
| 380 promise_test(function(t) { | 306 promise_test(function(t) { |
| 381 var res = new Response(new Blob([''])); | 307 var res = new Response(new Blob([''])); |
| 382 return res.blob() | 308 return res.blob() |
| 383 .then(function(blob) { | 309 .then(function(blob) { |
| 384 assert_equals(blob.type, ''); | 310 assert_equals(blob.type, ''); |
| 385 assert_equals(res.headers.get('Content-Type'), null); | 311 assert_equals(res.headers.get('Content-Type'), null); |
| 386 }); | 312 }); |
| 387 }, 'MIME type for Blob'); | 313 }, 'MIME type for Blob'); |
| 388 | 314 |
| 389 promise_test(function(t) { | 315 promise_test(function(t) { |
| 390 var res = new Response(new Blob(['hello'], {type: 'Text/Plain'})); | 316 var res = new Response(new Blob(['hello'], {type: 'Text/Plain'})); |
| 391 return res.blob() | 317 return res.blob() |
| 392 .then(function(blob) { | 318 .then(function(blob) { |
| 393 assert_equals(blob.type, 'text/plain'); | 319 assert_equals(blob.type, 'text/plain'); |
| 394 assert_equals(blob.size, 5); | 320 assert_equals(blob.size, 5); |
| 395 assert_equals(res.headers.get('Content-Type'), 'text/plain'); | 321 assert_equals(res.headers.get('Content-Type'), 'text/plain'); |
| 396 return res.blob(); | |
| 397 }).then(function(blob) { | |
| 398 // When we read from a response twice, it returns an empty contents. | |
| 399 // But the type should remain. | |
| 400 assert_equals(blob.type, 'text/plain'); | |
| 401 assert_equals(blob.size, 0); | |
| 402 assert_equals(res.headers.get('Content-Type'), 'text/plain'); | |
| 403 }); | 322 }); |
| 404 }, 'MIME type for Blob with non-empty type'); | 323 }, 'MIME type for Blob with non-empty type'); |
| 405 | 324 |
| 406 promise_test(function(t) { | 325 promise_test(function(t) { |
| 407 var res = new Response(new FormData()); | 326 var res = new Response(new FormData()); |
| 408 return res.blob() | 327 return res.blob() |
| 409 .then(function(blob) { | 328 .then(function(blob) { |
| 410 assert_equals(blob.type.indexOf('multipart/form-data; boundary='), | 329 assert_equals(blob.type.indexOf('multipart/form-data; boundary='), |
| 411 0); | 330 0); |
| 412 assert_equals(res.headers.get('Content-Type') | 331 assert_equals(res.headers.get('Content-Type') |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 451 promise_test(function(t) { | 370 promise_test(function(t) { |
| 452 var res = new Response(new Blob([''], {type: 'Text/Plain'}), | 371 var res = new Response(new Blob([''], {type: 'Text/Plain'}), |
| 453 {headers: [['Content-Type', 'Text/Html']]}); | 372 {headers: [['Content-Type', 'Text/Html']]}); |
| 454 return res.blob() | 373 return res.blob() |
| 455 .then(function(blob) { | 374 .then(function(blob) { |
| 456 assert_equals(blob.type, 'text/html'); | 375 assert_equals(blob.type, 'text/html'); |
| 457 assert_equals(res.headers.get('Content-Type'), 'Text/Html'); | 376 assert_equals(res.headers.get('Content-Type'), 'Text/Html'); |
| 458 }); | 377 }); |
| 459 }, 'Extract a MIME type (1)'); | 378 }, 'Extract a MIME type (1)'); |
| 460 | 379 |
| 461 promise_test(function(t) { | |
| 462 var res = new Response(new Blob([''], {type: 'Text/Plain'}), | |
| 463 {headers: [['Content-Type', 'Text/Html']]}); | |
| 464 res.body.cancel(); | |
| 465 return res.blob() | |
| 466 .then(function(blob) { | |
| 467 assert_equals(blob.type, 'text/html'); | |
| 468 assert_equals(res.headers.get('Content-Type'), 'Text/Html'); | |
| 469 }); | |
| 470 }, 'Extract a MIME type (2)'); | |
| 471 | |
| 472 promise_test(function(t) { | |
| 473 var res = new Response(new Blob([''], {type: 'Text/Plain'}), | |
| 474 {headers: [['Content-Type', 'Text/Html']]}); | |
| 475 res.body.cancel(); | |
| 476 res = res.clone(); | |
| 477 return res.blob() | |
| 478 .then(function(blob) { | |
| 479 assert_equals(blob.type, 'text/html'); | |
| 480 assert_equals(res.headers.get('Content-Type'), 'Text/Html'); | |
| 481 }); | |
| 482 }, 'Extract a MIME type (3)'); | |
| 483 | |
| 484 promise_test(function() { | 380 promise_test(function() { |
| 485 var response = Response.error(); | 381 var response = Response.error(); |
| 486 return response.text().then(function(text) { | 382 return response.text().then(function(text) { |
| 487 assert_equals(response.type, 'error'); | 383 assert_equals(response.type, 'error'); |
| 488 assert_equals(response.url, '', 'url must be the empty string'); | 384 assert_equals(response.url, '', 'url must be the empty string'); |
| 489 assert_equals(response.status, 0, 'status is always 0'); | 385 assert_equals(response.status, 0, 'status is always 0'); |
| 490 assert_false(response.ok); | 386 assert_false(response.ok); |
| 491 assert_equals(response.statusText, '', | 387 assert_equals(response.statusText, '', |
| 492 'status message is always the empty byte sequence'); | 388 'status message is always the empty byte sequence'); |
| 493 assert_equals(size(response.headers), 0, | 389 assert_equals(size(response.headers), 0, |
| 494 'header list is always empty.'); | 390 'header list is always empty.'); |
| 495 assert_equals(text, '', | 391 assert_equals(text, '', |
| 496 'body is always null'); | 392 'body is always null'); |
| 497 }); | 393 }); |
| 498 }, 'Response.error()'); | 394 }, 'Response.error()'); |
| 499 | 395 |
| 500 promise_test(function(t) { | |
| 501 var res = new Response('hello'); | |
| 502 return res.text().then(function(text) { | |
| 503 assert_equals(text, 'hello'); | |
| 504 return Promise.all([res.text(), res.text()]); | |
| 505 }).then(function(texts) { | |
| 506 assert_equals(texts[0], ''); | |
| 507 assert_equals(texts[1], ''); | |
| 508 return res.body.getReader().read(); | |
| 509 }).then(function(r) { | |
| 510 assert_true(r.done); | |
| 511 assert_equals(r.value, undefined); | |
| 512 }); | |
| 513 }, 'Read after text()'); | |
| 514 | |
| 515 promise_test(function() { | 396 promise_test(function() { |
| 516 var response = Response.redirect('https://www.example.com/test.html'); | 397 var response = Response.redirect('https://www.example.com/test.html'); |
| 517 return response.text().then(function(text) { | 398 return response.text().then(function(text) { |
| 518 assert_equals(response.status, 302, | 399 assert_equals(response.status, 302, |
| 519 'default value of status is always 302'); | 400 'default value of status is always 302'); |
| 520 assert_equals(response.headers.get('location'), | 401 assert_equals(response.headers.get('location'), |
| 521 'https://www.example.com/test.html', | 402 'https://www.example.com/test.html', |
| 522 'Location header should be correct absoulte URL'); | 403 'Location header should be correct absoulte URL'); |
| 523 assert_throws({name: 'TypeError'}, | 404 assert_throws({name: 'TypeError'}, |
| 524 function() { | 405 function() { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 546 test(function() { | 427 test(function() { |
| 547 ['http://ex\x0aample.com', | 428 ['http://ex\x0aample.com', |
| 548 'http://ex\x0dample.com'].forEach(function(url) { | 429 'http://ex\x0dample.com'].forEach(function(url) { |
| 549 assert_equals(Response.redirect(url).headers.get('Location'), | 430 assert_equals(Response.redirect(url).headers.get('Location'), |
| 550 'http://example.com/', | 431 'http://example.com/', |
| 551 'Location header value must not contain CR or LF'); | 432 'Location header value must not contain CR or LF'); |
| 552 }); | 433 }); |
| 553 }, 'Response.redirect() with URLs with CR or LF'); | 434 }, 'Response.redirect() with URLs with CR or LF'); |
| 554 | 435 |
| 555 done(); | 436 done(); |
| OLD | NEW |