Skip to content

Commit df90680

Browse files
stas-pavlovpimterry
authored andcommitted
Base fix
1 parent 3ee5f31 commit df90680

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/rules/base-rule-builder.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ import {
3030
CallbackMatcher,
3131
HostnameMatcher,
3232
PortMatcher,
33-
ProtocolMatcher
33+
ProtocolMatcher,
34+
RegexUrlMatcher,
3435
} from "./matchers";
3536

3637
/**
@@ -244,6 +245,15 @@ export abstract class BaseRuleBuilder {
244245
return this;
245246
}
246247

248+
/**
249+
* Match only requests that has absolute url matching the given RegExp.
250+
* @category Matching
251+
*/
252+
withUrlMatching(pattern: RegExp): this {
253+
this.matchers.push(new RegexUrlMatcher(pattern));
254+
return this;
255+
}
256+
247257
/**
248258
* Match only requests when the callback returns true
249259
* @category Matching

src/rules/matchers.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,33 @@ export class RegexPathMatcher extends Serializable implements RequestMatcher {
260260

261261
}
262262

263+
export class RegexUrlMatcher extends Serializable implements RequestMatcher {
264+
readonly type = 'regex-url';
265+
266+
readonly regexSource: string;
267+
readonly regexFlags: string;
268+
269+
constructor(regex: RegExp) {
270+
super();
271+
this.regexSource = regex.source;
272+
this.regexFlags = regex.flags;
273+
}
274+
275+
matches(request: OngoingRequest) {
276+
const absoluteUrl = normalizeUrl(request.url);
277+
const urlPath = getPathFromAbsoluteUrl(absoluteUrl);
278+
279+
// Test the matcher against both the path alone & the full URL
280+
const urlMatcher = new RegExp(this.regexSource, this.regexFlags);
281+
return urlMatcher.test(absoluteUrl);
282+
}
283+
284+
explain() {
285+
return `matching /${unescapeRegexp(this.regexSource)}/${this.regexFlags ?? ''}`;
286+
}
287+
288+
}
289+
263290
export class HeaderMatcher extends Serializable implements RequestMatcher {
264291
readonly type = 'header';
265292

@@ -590,6 +617,7 @@ export const MatcherLookup = {
590617
'port': PortMatcher,
591618
'simple-path': SimplePathMatcher,
592619
'regex-path': RegexPathMatcher,
620+
'regex-url': RegexUrlMatcher,
593621
'header': HeaderMatcher,
594622
'query': QueryMatcher,
595623
'exact-query-string': ExactQueryMatcher,

0 commit comments

Comments
 (0)