Skip to content

Commit

Permalink
test: ensure add intercept method is idempotent and does not create d…
Browse files Browse the repository at this point in the history
…uplicates
  • Loading branch information
Thiago Perrotta committed Oct 10, 2023
1 parent 7cce83e commit 090d324
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/bidiMapper/domains/network/NetworkStorage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,35 @@ describe('NetworkStorage', () => {
2
);
});

it('is idempotent', () => {
const intercept1 = networkStorage.addIntercept({
urlPatterns: [
{
type: 'string',
pattern: 'http://example.com',
},
],
phases: [Network.InterceptPhase.BeforeRequestSent],
});
const intercept2 = networkStorage.addIntercept({
urlPatterns: [
{
type: 'string',
pattern: 'http://example.com',
},
],
phases: [Network.InterceptPhase.BeforeRequestSent],
});

expect(intercept1).to.match(UUID_REGEX);
expect(intercept2).to.match(UUID_REGEX);
expect(intercept1).to.be.equal(intercept2);

expect(networkStorage.getFetchEnableParams().patterns).to.have.lengthOf(
1
);
});
});

it('remove intercept', () => {
Expand Down
14 changes: 13 additions & 1 deletion src/bidiMapper/domains/network/NetworkStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,20 @@ export class NetworkStorage {
urlPatterns: Network.UrlPattern[];
phases: Network.InterceptPhase[];
}): Network.Intercept {
const interceptId: Network.Intercept = uuidv4();
// Check if the given intercept entry already exists.
for (const [
interceptId,
{urlPatterns, phases},
] of this.#interceptMap.entries()) {
if (
JSON.stringify(value.urlPatterns) === JSON.stringify(urlPatterns) &&
JSON.stringify(value.phases) === JSON.stringify(phases)
) {
return interceptId;
}
}

const interceptId: Network.Intercept = uuidv4();
this.#interceptMap.set(interceptId, value);

return interceptId;
Expand Down

0 comments on commit 090d324

Please sign in to comment.