summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/fetch-later/quota.tentative.https.window.js
blob: 9d0ae4287dfb7990aa8d54973edfbeebf355e058 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// META: script=/common/get-host-info.sub.js
// META: script=/common/utils.js
// META: script=/fetch/fetch-later/resources/fetch-later-helper.js

'use strict';

const kQuotaPerOrigin = 64 * 1024;  // 64 kilobytes per spec.
const {ORIGIN, HTTPS_NOTSAMESITE_ORIGIN} = get_host_info();

// Runs a test case that cover a single fetchLater() call with `body` in its
// request payload. The call is not expected to throw any errors.
function fetchLaterPostTest(body, description) {
  test(() => {
    const controller = new AbortController();
    const result = fetchLater(
        '/fetch-later',
        {method: 'POST', signal: controller.signal, body: body});
    assert_false(result.activated);
    // Release quota taken by the pending request for subsequent tests.
    controller.abort();
  }, description);
}

// Test small payload for each supported data types.
for (const [dataType, skipCharset] of Object.entries(
         BeaconDataTypeToSkipCharset)) {
  fetchLaterPostTest(
      makeBeaconData(generateSequentialData(0, 1024, skipCharset), dataType),
      `A fetchLater() call accept small data in POST request of ${dataType}.`);
}

// Test various size of payloads for the same origin.
for (const dataType in BeaconDataType) {
  if (dataType !== BeaconDataType.FormData &&
      dataType !== BeaconDataType.URLSearchParams) {
    // Skips FormData & URLSearchParams, as browser adds extra bytes to them
    // in addition to the user-provided content. It is difficult to test a
    // request right at the quota limit.
    fetchLaterPostTest(
        // Generates data that is exactly 64 kilobytes.
        makeBeaconData(generatePayload(kQuotaPerOrigin), dataType),
        `A single fetchLater() call takes up the per-origin quota for its ` +
            `body of ${dataType}.`);
  }
}

// Test empty payload.
for (const dataType in BeaconDataType) {
  test(
      () => {
        assert_throws_js(
            TypeError, () => fetchLater('/', {method: 'POST', body: ''}));
      },
      `A single fetchLater() call does not accept empty data in POST request ` +
          `of ${dataType}.`);
}

// Test oversized payload.
for (const dataType in BeaconDataType) {
  test(
      () => {
        assert_throws_dom(
            'QuotaExceededError',
            () => fetchLater('/fetch-later', {
              method: 'POST',
              // Generates data that exceeds 64 kilobytes.
              body:
                  makeBeaconData(generatePayload(kQuotaPerOrigin + 1), dataType)
            }));
      },
      `A single fetchLater() call is not allowed to exceed per-origin quota ` +
          `for its body of ${dataType}.`);
}

// Test accumulated oversized request.
for (const dataType in BeaconDataType) {
  test(
      () => {
        const controller = new AbortController();
        // Makes the 1st call that sends only half of allowed quota.
        fetchLater('/fetch-later', {
          method: 'POST',
          signal: controller.signal,
          body: makeBeaconData(generatePayload(kQuotaPerOrigin / 2), dataType)
        });

        // Makes the 2nd call that sends half+1 of allowed quota.
        assert_throws_dom('QuotaExceededError', () => {
          fetchLater('/fetch-later', {
            method: 'POST',
            signal: controller.signal,
            body: makeBeaconData(
                generatePayload(kQuotaPerOrigin / 2 + 1), dataType)
          });
        });
        // Release quota taken by the pending requests for subsequent tests.
        controller.abort();
      },
      `The 2nd fetchLater() call is not allowed to exceed per-origin quota ` +
          `for its body of ${dataType}.`);
}

// Test various size of payloads across different origins.
for (const dataType in BeaconDataType) {
  test(
      () => {
        const controller = new AbortController();
        // Makes the 1st call that sends only half of allowed quota.
        fetchLater('/fetch-later', {
          method: 'POST',
          signal: controller.signal,
          body: makeBeaconData(generatePayload(kQuotaPerOrigin / 2), dataType)
        });

        // Makes the 2nd call that sends half+1 of allowed quota, but to a
        // different origin.
        fetchLater(`${HTTPS_NOTSAMESITE_ORIGIN}/fetch-later`, {
          method: 'POST',
          signal: controller.signal,
          body:
              makeBeaconData(generatePayload(kQuotaPerOrigin / 2 + 1), dataType)
        });
        // Release quota taken by the pending requests for subsequent tests.
        controller.abort();
      },
      `The 2nd fetchLater() call to another origin does not exceed per-origin` +
          ` quota for its body of ${dataType}.`);
}