Bug 1691099 [wpt PR 27512] - Reorganize and augment beacon wpt, a=testonly
[gecko.git] / testing / web-platform / tests / beacon / beacon-common.sub.js
blob46994954608410c56c56edb2becce8f388ef7d2a
1 'use strict';
3 const EMPTY = 'empty';
4 const SMALL = 'small';
5 const LARGE = 'large';
6 const MAX = 'max';
7 const TOOLARGE = 'toolarge';
9 const STRING = 'string';
10 const ARRAYBUFFER = 'arraybuffer';
11 const FORM = 'form';
12 const BLOB = 'blob';
14 function getContentType(type) {
15   switch (type) {
16     case STRING:
17       return 'text/plain;charset=UTF-8';
18     case ARRAYBUFFER:
19       return null;
20     case FORM:
21       return 'multipart/form-data';
22     case BLOB:
23       return null;
24     default:
25       throw Error(`invalid type: ${type}`);
26   }
29 // Create a payload with the given size and type.
30 // `sizeString` must be one of EMPTY, SMALL, LARGE, MAX, TOOLARGE.
31 // `type` must be one of STRING, ARRAYBUFFER, FORM, BLOB.
32 // `contentType` is effective only if `type` is BLOB.
33 function makePayload(sizeString, type, contentType) {
34   let size = 0;
35   switch (sizeString) {
36     case EMPTY:
37       size = 0;
38       break;
39     case SMALL:
40       size = 10;
41       break;
42     case LARGE:
43       size = 10 * 1000;
44       break;
45     case MAX:
46       if (type === FORM) {
47         throw Error('Not supported');
48       }
49       size = 65536;
50       break;
51     case TOOLARGE:
52       size = 65537;
53       break;
54     default:
55       throw Error('invalid size');
56   }
58   let data = '';
59   if (size > 0) {
60     const prefix = String(size) + ':';
61     data = prefix + Array(size - prefix.length).fill('*').join('');
62   }
64   switch (type) {
65     case STRING:
66       return data;
67     case ARRAYBUFFER:
68       return new TextEncoder().encode(data).buffer;
69     case FORM:
70       const formData = new FormData();
71       if (size > 0) {
72         formData.append('payload', data);
73       }
74       return formData;
75     case BLOB:
76       const options = contentType ? {type: contentType} : undefined;
77       const blob = new Blob([data], options);
78       return blob;
79     default:
80       throw Error('invalid type');
81   }
84 function parallelPromiseTest(func, description) {
85   async_test((t) => {
86     Promise.resolve(func(t)).then(() => t.done()).catch(t.step_func((e) => {
87       throw e;
88     }));
89   }, description);
92 // Poll the server for the test result.
93 async function waitForResult(id, expectedError = null) {
94   const url = `/beacon/resources/beacon.py?cmd=stat&id=${id}`;
95   for (let i = 0; i < 30; ++i) {
96     const response = await fetch(url);
97     const text = await response.text();
98     const results = JSON.parse(text);
100     if (results.length === 0) {
101       await new Promise(resolve => step_timeout(resolve, 100));
102       continue;
103     }
104     assert_equals(results.length, 1, `bad response: '${text}'`);
105     const result = results[0];
106     // null JSON values parse as null, not undefined
107     assert_equals(result.error, expectedError, 'error recorded in stash');
108     return result;
109   }
110   assert_true(false, 'timeout');