44
55'use strict' ;
66
7- const kQuotaPerOrigin = 64 * 1024 ; // 64 kilobytes per spec.
7+ const QUOTA_PER_ORIGIN = 64 * 1024 ; // 64 kilobytes per spec.
88const { ORIGIN , HTTPS_NOTSAMESITE_ORIGIN } = get_host_info ( ) ;
9+ const TEST_ENDPOINT = '/fetch-later' ;
910
1011// Runs a test case that cover a single fetchLater() call with `body` in its
1112// request payload. The call is not expected to throw any errors.
1213function fetchLaterPostTest ( body , description ) {
1314 test ( ( ) => {
1415 const controller = new AbortController ( ) ;
1516 const result = fetchLater (
16- '/fetch-later' ,
17- { method : 'POST' , signal : controller . signal , body : body } ) ;
17+ TEST_ENDPOINT , { method : 'POST' , signal : controller . signal , body : body } ) ;
1818 assert_false ( result . activated ) ;
1919 // Release quota taken by the pending request for subsequent tests.
2020 controller . abort ( ) ;
@@ -30,19 +30,25 @@ for (const [dataType, skipCharset] of Object.entries(
3030}
3131
3232// Test various size of payloads for the same origin.
33- for ( const dataType in BeaconDataType ) {
34- if ( dataType !== BeaconDataType . FormData &&
35- dataType !== BeaconDataType . URLSearchParams ) {
36- // Skips FormData & URLSearchParams, as browser adds extra bytes to them
37- // in addition to the user-provided content. It is difficult to test a
38- // request right at the quota limit.
39- fetchLaterPostTest (
40- // Generates data that is exactly 64 kilobytes.
41- makeBeaconData ( generatePayload ( kQuotaPerOrigin ) , dataType ) ,
42- `A single fetchLater() call takes up the per-origin quota for its ` +
43- `body of ${ dataType } .` ) ;
44- }
45- }
33+
34+ // Test max possible size of payload.
35+ // Length of absolute URL to the endpoint.
36+ const POST_TEST_REQUEST_URL_SIZE = ( ORIGIN + TEST_ENDPOINT ) . length ;
37+ // Total size of the request header.
38+ const POST_TEST_REQUEST_HEADER_SIZE = 36 ;
39+ // Runs this test only for String type beacon, as browser adds extra bytes to
40+ // body for some other types (FormData & URLSearchParams), and the request
41+ // header sizes varies for every other types. It is difficult to test a request
42+ // right at the quota limit.
43+ fetchLaterPostTest (
44+ // Generates data that is exactly 64 kilobytes.
45+ makeBeaconData (
46+ generatePayload (
47+ QUOTA_PER_ORIGIN - POST_TEST_REQUEST_URL_SIZE -
48+ POST_TEST_REQUEST_HEADER_SIZE ) ,
49+ BeaconDataType . String ) ,
50+ `A single fetchLater() call takes up the per-origin quota for its ` +
51+ `body of String.` ) ;
4652
4753// Test empty payload.
4854for ( const dataType in BeaconDataType ) {
@@ -64,8 +70,8 @@ for (const dataType in BeaconDataType) {
6470 ( ) => fetchLater ( '/fetch-later' , {
6571 method : 'POST' ,
6672 // Generates data that exceeds 64 kilobytes.
67- body :
68- makeBeaconData ( generatePayload ( kQuotaPerOrigin + 1 ) , dataType )
73+ body : makeBeaconData (
74+ generatePayload ( QUOTA_PER_ORIGIN + 1 ) , dataType )
6975 } ) ) ;
7076 } ,
7177 `A single fetchLater() call is not allowed to exceed per-origin quota ` +
@@ -81,7 +87,7 @@ for (const dataType in BeaconDataType) {
8187 fetchLater ( '/fetch-later' , {
8288 method : 'POST' ,
8389 signal : controller . signal ,
84- body : makeBeaconData ( generatePayload ( kQuotaPerOrigin / 2 ) , dataType )
90+ body : makeBeaconData ( generatePayload ( QUOTA_PER_ORIGIN / 2 ) , dataType )
8591 } ) ;
8692
8793 // Makes the 2nd call that sends half+1 of allowed quota.
@@ -90,7 +96,7 @@ for (const dataType in BeaconDataType) {
9096 method : 'POST' ,
9197 signal : controller . signal ,
9298 body : makeBeaconData (
93- generatePayload ( kQuotaPerOrigin / 2 + 1 ) , dataType )
99+ generatePayload ( QUOTA_PER_ORIGIN / 2 + 1 ) , dataType )
94100 } ) ;
95101 } ) ;
96102 // Release quota taken by the pending requests for subsequent tests.
@@ -109,16 +115,16 @@ for (const dataType in BeaconDataType) {
109115 fetchLater ( '/fetch-later' , {
110116 method : 'POST' ,
111117 signal : controller . signal ,
112- body : makeBeaconData ( generatePayload ( kQuotaPerOrigin / 2 ) , dataType )
118+ body : makeBeaconData ( generatePayload ( QUOTA_PER_ORIGIN / 2 ) , dataType )
113119 } ) ;
114120
115121 // Makes the 2nd call that sends half+1 of allowed quota, but to a
116122 // different origin.
117123 fetchLater ( `${ HTTPS_NOTSAMESITE_ORIGIN } /fetch-later` , {
118124 method : 'POST' ,
119125 signal : controller . signal ,
120- body :
121- makeBeaconData ( generatePayload ( kQuotaPerOrigin / 2 + 1 ) , dataType )
126+ body : makeBeaconData (
127+ generatePayload ( QUOTA_PER_ORIGIN / 2 + 1 ) , dataType )
122128 } ) ;
123129 // Release quota taken by the pending requests for subsequent tests.
124130 controller . abort ( ) ;
0 commit comments