Skip to content

Commit

Permalink
fix: implement pattern matching
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Nov 27, 2024
1 parent a818647 commit c8e19c4
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 16 deletions.
6 changes: 0 additions & 6 deletions src/bidiMapper/modules/network/NetworkProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,6 @@ export class NetworkProcessor {
);
}

try {
new URLPattern(urlPattern);
} catch (error) {
throw new InvalidArgumentException(`${error}`);
}

return urlPattern;
}
});
Expand Down
40 changes: 40 additions & 0 deletions src/bidiMapper/modules/network/NetworkUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,44 @@ describe('NetworkUtils', () => {
expect(networkUtils.getTiming(1)).to.equal(1);
});
});

describe('matchUrlPattern', () => {
it('should match urls against string patterns', () => {
expect(
networkUtils.matchUrlPattern(
{
type: 'string',
pattern: 'https://example.test/test?query',
},
'https://example.test/test?query',
),
).to.equal(true);

expect(
networkUtils.matchUrlPattern(
{
type: 'string',
pattern: 'https://example.test:333',
},
'https://example.test:333',
),
).to.equal(true);
});

it('should match urls against object patterns', () => {
expect(
networkUtils.matchUrlPattern(
{
type: 'pattern',
protocol: 'https:',
hostname: 'example.test',
port: '333',
pathname: '/test',
search: '?query',
},
'https://example.test:333/test?query',
),
).to.equal(true);
});
});
});
58 changes: 48 additions & 10 deletions src/bidiMapper/modules/network/NetworkUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,22 +312,60 @@ type RequireStringPattern = Omit<
/** Matches the given URLPattern against the given URL. */
export function matchUrlPattern(
urlPattern: Network.UrlPattern,
url: string | undefined,
url: string,
): boolean {
let pattern: Network.UrlPatternPattern;
switch (urlPattern.type) {
case 'string': {
const pattern = new URLPattern(urlPattern.pattern);
return new URLPattern({
protocol: pattern.protocol,
hostname: pattern.hostname,
port: pattern.port,
pathname: pattern.pathname,
search: pattern.search,
} satisfies RequireStringPattern).test(url);
const patternUrl = new URL(urlPattern.pattern);
pattern = {
type: 'pattern',
protocol: patternUrl.protocol,
hostname: patternUrl.hostname,
port: patternUrl.port,
pathname: patternUrl.pathname,
search: patternUrl.search,
};
break;
}
case 'pattern':
return new URLPattern(urlPattern).test(url);
pattern = urlPattern;
break;
}

// See https://w3c.github.io/webdriver-bidi/#match-url-pattern.
const parsedUrl = new URL(url);

if (
pattern.protocol !== undefined &&
pattern.protocol !== parsedUrl.protocol
) {
return false;
}

if (
pattern.hostname !== undefined &&
pattern.hostname !== parsedUrl.hostname
) {
return false;
}

if (pattern.port !== parsedUrl.port) {
return false;
}

if (
pattern.pathname !== undefined &&
pattern.pathname !== parsedUrl.pathname
) {
return false;
}

if (pattern.pathname !== undefined && pattern.search !== parsedUrl.search) {
return false;
}

return true;
}

export function bidiBodySizeFromCdpPostDataEntries(
Expand Down

0 comments on commit c8e19c4

Please sign in to comment.