• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// This method generates a number of tests verifying fetching of blob URLs,
2// allowing the same tests to be used both with fetch() and XMLHttpRequest.
3//
4// |fetch_method| is only used in test names, and should describe the
5// (javascript) method being used by the other two arguments (i.e. 'fetch' or 'XHR').
6//
7// |fetch_should_succeed| is a callback that is called with the Test and a URL.
8// Fetching the URL is expected to succeed. The callback should return a promise
9// resolved with whatever contents were fetched.
10//
11// |fetch_should_fail| similarly is a callback that is called with the Test, a URL
12// to fetch, and optionally a method to use to do the fetch. If no method is
13// specified the callback should use the 'GET' method. Fetching of these URLs is
14// expected to fail, and the callback should return a promise that resolves iff
15// fetching did indeed fail.
16function fetch_tests(fetch_method, fetch_should_succeed, fetch_should_fail) {
17  const blob_contents = 'test blob contents';
18  const blob = new Blob([blob_contents]);
19
20  promise_test(t => {
21    const url = URL.createObjectURL(blob);
22
23    return fetch_should_succeed(t, url).then(text => {
24      assert_equals(text, blob_contents);
25    });
26  }, 'Blob URLs can be used in ' + fetch_method);
27
28  promise_test(t => {
29    const url = URL.createObjectURL(blob);
30
31    return fetch_should_succeed(t, url + '#fragment').then(text => {
32      assert_equals(text, blob_contents);
33    });
34  }, fetch_method + ' with a fragment should succeed');
35
36  promise_test(t => {
37    const url = URL.createObjectURL(blob);
38    URL.revokeObjectURL(url);
39
40    return fetch_should_fail(t, url);
41  }, fetch_method + ' of a revoked URL should fail');
42
43  promise_test(t => {
44    const url = URL.createObjectURL(blob);
45    URL.revokeObjectURL(url + '#fragment');
46
47    return fetch_should_succeed(t, url).then(text => {
48      assert_equals(text, blob_contents);
49    });
50  }, 'Only exact matches should revoke URLs, using ' + fetch_method);
51
52  promise_test(t => {
53    const url = URL.createObjectURL(blob);
54
55    return fetch_should_fail(t, url + '?querystring');
56  }, 'Appending a query string should cause ' + fetch_method + ' to fail');
57
58  promise_test(t => {
59    const url = URL.createObjectURL(blob);
60
61    return fetch_should_fail(t, url + '/path');
62  }, 'Appending a path should cause ' + fetch_method + ' to fail');
63
64  for (const method of ['HEAD', 'POST', 'DELETE', 'OPTIONS', 'PUT', 'CUSTOM']) {
65    const url = URL.createObjectURL(blob);
66
67    promise_test(t => {
68      return fetch_should_fail(t, url, method);
69    }, fetch_method + ' with method "' + method + '" should fail');
70  }
71}