Skip to content

Commit 6509f76

Browse files
committed
fix response type restrictions and add tests
1 parent 44ed1ad commit 6509f76

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/scriptlets/prevent-fetch.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
* - `emptyObj` - empty object
4343
* - `emptyArr` - empty array
4444
* - responseType - optional, string for defining response type, defaults to `default`. Possible values:
45+
* - default
4546
* - opaque
4647
*
4748
* > Usage with no arguments will log fetch calls to browser console;
@@ -104,9 +105,11 @@ export function preventFetch(source, propsToMatch, responseBody = 'emptyObj', re
104105
return;
105106
}
106107

107-
let allowedResponseType;
108-
if (responseType === 'default' || responseType === 'opaque') {
109-
allowedResponseType = responseType;
108+
// Skip disallowed response types
109+
if (!(responseType === 'default' || responseType === 'opaque')) {
110+
// eslint-disable-next-line no-console
111+
console.log(`Invalid parameter: ${responseType}`);
112+
return;
110113
}
111114

112115
const handlerWrapper = (target, thisArg, args) => {
@@ -139,7 +142,7 @@ export function preventFetch(source, propsToMatch, responseBody = 'emptyObj', re
139142

140143
if (shouldPrevent) {
141144
hit(source);
142-
return noopPromiseResolve(strResponseBody, fetchData.url, allowedResponseType);
145+
return noopPromiseResolve(strResponseBody, fetchData.url, responseType);
143146
}
144147

145148
return Reflect.apply(target, thisArg, args);

tests/scriptlets/prevent-fetch.test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,48 @@ if (!isSupported) {
287287
assert.strictEqual(window.hit, 'FIRED', 'hit function fired');
288288
done();
289289
});
290+
291+
test('simple fetch - valid response type', async (assert) => {
292+
const OPAQUE_RESPONSE_TYPE = 'opaque';
293+
const INPUT_JSON_PATH = `${FETCH_OBJECTS_PATH}/test01.json`;
294+
const init = {
295+
method: 'GET',
296+
};
297+
298+
runScriptlet(name, ['*', '', OPAQUE_RESPONSE_TYPE]);
299+
const done = assert.async();
300+
301+
const response = await fetch(INPUT_JSON_PATH, init);
302+
303+
assert.strictEqual(response.type, OPAQUE_RESPONSE_TYPE, 'Response type is set');
304+
assert.strictEqual(window.hit, 'FIRED', 'hit function fired');
305+
done();
306+
});
307+
308+
test('simple fetch - invalid response type', async (assert) => {
309+
const INVALID_RESPONSE_TYPE = 'invalid_type';
310+
const BASIC_RESPONSE_TYPE = 'basic';
311+
const INPUT_JSON_PATH = `${FETCH_OBJECTS_PATH}/test01.json`;
312+
const init = {
313+
method: 'GET',
314+
};
315+
316+
const expectedJson = {
317+
a1: 1,
318+
b2: 'test',
319+
c3: 3,
320+
};
321+
322+
runScriptlet(name, ['*', '', INVALID_RESPONSE_TYPE]);
323+
const done = assert.async();
324+
325+
const response = await fetch(INPUT_JSON_PATH, init);
326+
const actualJson = await response.json();
327+
328+
assert.deepEqual(actualJson, expectedJson, 'Request is not modified');
329+
330+
assert.strictEqual(response.type, BASIC_RESPONSE_TYPE, 'Response type is not modified');
331+
assert.strictEqual(window.hit, undefined, 'hit function fired');
332+
done();
333+
});
290334
}

0 commit comments

Comments
 (0)