mockttp
mockttp copied to clipboard
Make .forHostname() hostname parameter a RegExp
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;
:+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.
Got you!