mockttp icon indicating copy to clipboard operation
mockttp copied to clipboard

Make .forHostname() hostname parameter a RegExp

Open OnkelTem opened this issue 3 years ago • 2 comments

I'd propose to make hostname a RegExp. First, to match things like: .forHostname(/^.+?\.domain.com/) Second, to keep up with the URL matchers (.forGet(), .forPost() etc) which can take RegExp.

I.e.

forHostname(hostname: string): this;

gets:

forHostname(hostname: string | RegExp): this;

OnkelTem avatar May 12 '22 10:05 OnkelTem

:+1: Sounds good to me, PRs welcome. The one tricky bit is serialization for remote clients, since JSON can't represent regexes natively.

You can see how RegexPathMatcher handles it here - it stores the regex as a string, which only gets converted into a regex inside matches(req).

I think I'd suggest creating this as a new matcher like RegexHostnameMatcher, with the forHostname method choosing between the two implementations. That's the same way that path matching works to do strings + regexes, and that way you don't have to differentiate between the raw-string and regex-serialized--as-string data when deserializing rules from remote clients.

pimterry avatar May 12 '22 13:05 pimterry

Got you!

OnkelTem avatar May 12 '22 14:05 OnkelTem