Skip to content

Commit

Permalink
add testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Oct 31, 2022
1 parent 522ade9 commit dc8ab9d
Showing 1 changed file with 99 additions and 3 deletions.
102 changes: 99 additions & 3 deletions tests/scriptlets/trusted-replace-fetch-response.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable no-underscore-dangle, no-console */
import { runScriptlet, clearGlobalProps } from '../helpers';
import { startsWith } from '../../src/helpers/string-utils';
import { isEmptyObject } from '../../src/helpers/object-utils';

const { test, module } = QUnit;
const name = 'trusted-replace-fetch-response';
Expand Down Expand Up @@ -33,9 +32,9 @@ if (!isSupported) {
assert.ok(true, 'Browser does not support it');
});
} else {
test('No arguments, logging', async (assert) => {
test('No arguments, no replacement, logging', async (assert) => {
const INPUT_JSON_PATH = `${FETCH_OBJECTS_PATH}/test01.json`;
const TEST_METHOD = 'POST';
const TEST_METHOD = 'GET';
const init = {
method: TEST_METHOD,
};
Expand Down Expand Up @@ -66,4 +65,101 @@ if (!isSupported) {
assert.deepEqual(actualJson, expectedJson);
done();
});

test('Match all requests, replace by substring', async (assert) => {
const INPUT_JSON_PATH = `${FETCH_OBJECTS_PATH}/test01.json`;
const TEST_METHOD = 'GET';
const init = {
method: TEST_METHOD,
};

const done = assert.async();

const PATTERN = 'test';
const REPLACEMENT = 'new content';
runScriptlet(name, [PATTERN, REPLACEMENT]);

const response = await fetch(INPUT_JSON_PATH, init);
const actualJson = await response.json();

const textContent = JSON.stringify(actualJson);

assert.strictEqual(window.hit, 'FIRED', 'hit function fired');
assert.notOk(textContent.includes(PATTERN), 'Pattern is removed');
assert.ok(textContent.includes(REPLACEMENT), 'New content is set');
done();
});

test('Match all requests, replace by regex', async (assert) => {
const INPUT_JSON_PATH = `${FETCH_OBJECTS_PATH}/test01.json`;
const TEST_METHOD = 'GET';
const init = {
method: TEST_METHOD,
};

const done = assert.async();

const PATTERN = /test/;
const REPLACEMENT = 'new content';
runScriptlet(name, [PATTERN, REPLACEMENT]);

const response = await fetch(INPUT_JSON_PATH, init);
const actualJson = await response.json();

const textContent = JSON.stringify(actualJson);

assert.strictEqual(window.hit, 'FIRED', 'hit function fired');
assert.notOk(PATTERN.test(textContent), 'Pattern is removed');
assert.ok(textContent.includes(REPLACEMENT), 'New content is set');
done();
});

test('Match request by url and method, remove all text content', async (assert) => {
const INPUT_JSON_PATH = `${FETCH_OBJECTS_PATH}/test01.json`;
const TEST_METHOD = 'GET';
const init = {
method: TEST_METHOD,
};

const done = assert.async();

const PATTERN = '';
const REPLACEMENT = '';
const PROPS_TO_MATCH = 'test01 method:GET';
runScriptlet(name, [PATTERN, REPLACEMENT, PROPS_TO_MATCH]);

const response = await fetch(INPUT_JSON_PATH, init);
const actualTextContent = await response.text();

assert.strictEqual(window.hit, 'FIRED', 'hit function fired');
assert.strictEqual(actualTextContent, '', 'Content is removed');
done();
});

test('Unmatched request\'s content is not modified', async (assert) => {
const INPUT_JSON_PATH = `${FETCH_OBJECTS_PATH}/test01.json`;
const TEST_METHOD = 'GET';
const init = {
method: TEST_METHOD,
};

const expectedJson = {
a1: 1,
b2: 'test',
c3: 3,
};

const PATTERN = '';
const REPLACEMENT = '';
const PROPS_TO_MATCH = 'test99 method:POST';
runScriptlet(name, [PATTERN, REPLACEMENT, PROPS_TO_MATCH]);

const done = assert.async();
const response = await fetch(INPUT_JSON_PATH, init);
const actualJson = await response.json();

assert.strictEqual(window.hit, 'FIRED', 'hit function fired');
assert.deepEqual(actualJson, expectedJson, 'Content is intact');
done();
});
}

0 comments on commit dc8ab9d

Please sign in to comment.