11cb0ef41Sopenharmony_ci// META: script=resources/fetch-tests.js
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_cifunction xhr_should_succeed(test, url) {
41cb0ef41Sopenharmony_ci  return new Promise((resolve, reject) => {
51cb0ef41Sopenharmony_ci    const xhr = new XMLHttpRequest();
61cb0ef41Sopenharmony_ci    xhr.open('GET', url);
71cb0ef41Sopenharmony_ci    xhr.onload = test.step_func(() => {
81cb0ef41Sopenharmony_ci      assert_equals(xhr.status, 200);
91cb0ef41Sopenharmony_ci      assert_equals(xhr.statusText, 'OK');
101cb0ef41Sopenharmony_ci      resolve(xhr.response);
111cb0ef41Sopenharmony_ci    });
121cb0ef41Sopenharmony_ci    xhr.onerror = () => reject('Got unexpected error event');
131cb0ef41Sopenharmony_ci    xhr.send();
141cb0ef41Sopenharmony_ci  });
151cb0ef41Sopenharmony_ci}
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cifunction xhr_should_fail(test, url, method = 'GET') {
181cb0ef41Sopenharmony_ci  const xhr = new XMLHttpRequest();
191cb0ef41Sopenharmony_ci  xhr.open(method, url);
201cb0ef41Sopenharmony_ci  const result1 = new Promise((resolve, reject) => {
211cb0ef41Sopenharmony_ci    xhr.onload = () => reject('Got unexpected load event');
221cb0ef41Sopenharmony_ci    xhr.onerror = resolve;
231cb0ef41Sopenharmony_ci  });
241cb0ef41Sopenharmony_ci  const result2 = new Promise(resolve => {
251cb0ef41Sopenharmony_ci    xhr.onreadystatechange = test.step_func(() => {
261cb0ef41Sopenharmony_ci      if (xhr.readyState !== xhr.DONE) return;
271cb0ef41Sopenharmony_ci      assert_equals(xhr.status, 0);
281cb0ef41Sopenharmony_ci      resolve();
291cb0ef41Sopenharmony_ci    });
301cb0ef41Sopenharmony_ci  });
311cb0ef41Sopenharmony_ci  xhr.send();
321cb0ef41Sopenharmony_ci  return Promise.all([result1, result2]);
331cb0ef41Sopenharmony_ci}
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_cifetch_tests('XHR', xhr_should_succeed, xhr_should_fail);
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ciasync_test(t => {
381cb0ef41Sopenharmony_ci  const blob_contents = 'test blob contents';
391cb0ef41Sopenharmony_ci  const blob_type = 'image/png';
401cb0ef41Sopenharmony_ci  const blob = new Blob([blob_contents], {type: blob_type});
411cb0ef41Sopenharmony_ci  const url = URL.createObjectURL(blob);
421cb0ef41Sopenharmony_ci  const xhr = new XMLHttpRequest();
431cb0ef41Sopenharmony_ci  xhr.open('GET', url);
441cb0ef41Sopenharmony_ci  xhr.onloadend = t.step_func_done(() => {
451cb0ef41Sopenharmony_ci    assert_equals(xhr.getResponseHeader('Content-Type'), blob_type);
461cb0ef41Sopenharmony_ci  });
471cb0ef41Sopenharmony_ci  xhr.send();
481cb0ef41Sopenharmony_ci}, 'XHR should return Content-Type from Blob');
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ciasync_test(t => {
511cb0ef41Sopenharmony_ci  const blob_contents = 'test blob contents';
521cb0ef41Sopenharmony_ci  const blob = new Blob([blob_contents]);
531cb0ef41Sopenharmony_ci  const url = URL.createObjectURL(blob);
541cb0ef41Sopenharmony_ci  const xhr = new XMLHttpRequest();
551cb0ef41Sopenharmony_ci  xhr.open('GET', url);
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  // Revoke the object URL.  XHR should take a reference to the blob as soon as
581cb0ef41Sopenharmony_ci  // it receives it in open(), so the request succeeds even though we revoke the
591cb0ef41Sopenharmony_ci  // URL before calling send().
601cb0ef41Sopenharmony_ci  URL.revokeObjectURL(url);
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  xhr.onload = t.step_func_done(() => {
631cb0ef41Sopenharmony_ci    assert_equals(xhr.response, blob_contents);
641cb0ef41Sopenharmony_ci  });
651cb0ef41Sopenharmony_ci  xhr.onerror = t.unreached_func('Got unexpected error event');
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci  xhr.send();
681cb0ef41Sopenharmony_ci}, 'Revoke blob URL after open(), will fetch');
69