11cb0ef41Sopenharmony_ci// META: script=resources/fetch-tests.js
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_cifunction fetch_should_succeed(test, request) {
41cb0ef41Sopenharmony_ci  return fetch(request).then(response => response.text());
51cb0ef41Sopenharmony_ci}
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cifunction fetch_should_fail(test, url, method = 'GET') {
81cb0ef41Sopenharmony_ci  return promise_rejects_js(test, TypeError, fetch(url, {method: method}));
91cb0ef41Sopenharmony_ci}
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cifetch_tests('fetch', fetch_should_succeed, fetch_should_fail);
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cipromise_test(t => {
141cb0ef41Sopenharmony_ci  const blob_contents = 'test blob contents';
151cb0ef41Sopenharmony_ci  const blob_type = 'image/png';
161cb0ef41Sopenharmony_ci  const blob = new Blob([blob_contents], {type: blob_type});
171cb0ef41Sopenharmony_ci  const url = URL.createObjectURL(blob);
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  return fetch(url).then(response => {
201cb0ef41Sopenharmony_ci    assert_equals(response.headers.get('Content-Type'), blob_type);
211cb0ef41Sopenharmony_ci  });
221cb0ef41Sopenharmony_ci}, 'fetch should return Content-Type from Blob');
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_cipromise_test(t => {
251cb0ef41Sopenharmony_ci  const blob_contents = 'test blob contents';
261cb0ef41Sopenharmony_ci  const blob = new Blob([blob_contents]);
271cb0ef41Sopenharmony_ci  const url = URL.createObjectURL(blob);
281cb0ef41Sopenharmony_ci  const request = new Request(url);
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  // Revoke the object URL.  Request should take a reference to the blob as
311cb0ef41Sopenharmony_ci  // soon as it receives it in open(), so the request succeeds even though we
321cb0ef41Sopenharmony_ci  // revoke the URL before calling fetch().
331cb0ef41Sopenharmony_ci  URL.revokeObjectURL(url);
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  return fetch_should_succeed(t, request).then(text => {
361cb0ef41Sopenharmony_ci    assert_equals(text, blob_contents);
371cb0ef41Sopenharmony_ci  });
381cb0ef41Sopenharmony_ci}, 'Revoke blob URL after creating Request, will fetch');
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_cipromise_test(function(t) {
411cb0ef41Sopenharmony_ci  const blob_contents = 'test blob contents';
421cb0ef41Sopenharmony_ci  const blob = new Blob([blob_contents]);
431cb0ef41Sopenharmony_ci  const url = URL.createObjectURL(blob);
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  const result = fetch_should_succeed(t, url).then(text => {
461cb0ef41Sopenharmony_ci    assert_equals(text, blob_contents);
471cb0ef41Sopenharmony_ci  });
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  // Revoke the object URL. fetch should have already resolved the blob URL.
501cb0ef41Sopenharmony_ci  URL.revokeObjectURL(url);
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  return result;
531cb0ef41Sopenharmony_ci}, 'Revoke blob URL after calling fetch, fetch should succeed');
54