Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Dec 19, 2022
1 parent 492da8f commit 6973376
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tests/scriptlets/prevent-xhr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,8 @@ if (isSupported) {
};

xhr1.send();
xhr2.send();
// use timeout to avoid hit collisions
setTimeout(() => xhr2.send(), 1);
});
} else {
test('unsupported', (assert) => {
Expand Down
43 changes: 43 additions & 0 deletions tests/scriptlets/trusted-replace-xhr-response.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,49 @@ if (isSupported) {
done3();
});
});

test('Works correctly with different parallel XHR requests', async (assert) => {
const URL_TO_PASS = `${FETCH_OBJECTS_PATH}/test02.json`;
const INTACT_RESPONSE_PART = 'test';

const METHOD = 'GET';
const URL_TO_BLOCK = `${FETCH_OBJECTS_PATH}/test01.json`;
const PATTERN = '*';
const REPLACEMENT = '';
const MATCH_DATA = [PATTERN, REPLACEMENT, `${URL}`];

runScriptlet(name, MATCH_DATA);

const done1 = assert.async();
const done2 = assert.async();

const xhr1 = new XMLHttpRequest();
const xhr2 = new XMLHttpRequest();

xhr1.open(METHOD, URL_TO_PASS);
xhr2.open(METHOD, URL_TO_BLOCK);

xhr1.onload = () => {
console.log(xhr1.response);
assert.strictEqual(xhr1.readyState, 4, 'Response done');
assert.ok(xhr1.response.includes(INTACT_RESPONSE_PART), 'Response is intact');

assert.strictEqual(window.hit, undefined, 'hit should not fire');
done1();
};

xhr2.onload = () => {
assert.strictEqual(xhr2.readyState, 4, 'Response done');
assert.notOk(xhr2.response === '', 'Response has been removed');

assert.strictEqual(window.hit, undefined, 'hit function fired');
done2();
};

xhr1.send();
// use timeout to avoid hit collisions
setTimeout(() => xhr2.send(), 1);
});
} else {
test('unsupported', (assert) => {
assert.ok(true, 'Browser does not support it');
Expand Down

0 comments on commit 6973376

Please sign in to comment.