-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add wildcard support for passthrough #171
Add wildcard support for passthrough #171
Conversation
Tests in Node 14 are failing with Node 14 has reached end of life for some time now, how important is it to continue supporting it ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes sense! I think I would also like to include regex support, but I'm happy to support both since it's easy to do so, and I think there are probably other places in the API where expanding to include URLPattern support would be a helpful improvement there too.
Tests in Node 14 are failing with SyntaxError: Unexpected token '??='
Node 14 obviously isn't super important since it's end of life, but it's a breaking change to drop it and I'd gently prefer not to without a strong reason. In this case, it looks like you can just use urlpattern-polyfill v8 instead and it'll work - there's very few changes since that except a minor reduction in polyfill size anyway (which is unlikely to matter in our scenarios).
some of the integration tests were already failing on main - I'm not sure why
Not sure what that means, but sounds like something that needs some investigation! They were passing in CI last week no problem, so that shouldn't really be happening. Let me know if you're still seeing this and what exactly is failing.
We should include at least one test with a wildcard in the integration tests though, just to make sure this is fully covered in the real-world scenario.
Let me know if you're happy with these changes, I'll then update the docs.
In general yes - if we can preferably avoid the breaking change to drop node 14, and we can get all the tests passing. Docs would definitely be good too, we'll want to mention this in the jsdoc for the both options.
That makes perfect sense, I understand. Because simply importing urlpattern-polyfill won't work (that will always create a syntax error on Node 14), we can't add it as a dependency. I see 2 ways then:
In both cases, I think we need to change the type of Array<{ hostname: string } | { hostnameRegex: RegExp }> or Array<{ hostname: string | RegExp }> Same for predicate, but with Regarding the tests, I'm happily surprised to see them all pass today as I tried to reproduce with Node 20.12 🙂 I can absolutely include wildcards in there. Let me know which approach you prefer for |
No, we can - you can just depend on urlpattern-polyfill |
for node 14 compatibility
New dependencies detected. Learn more about Socket for GitHub ↗︎
|
Using v8 is even simpler indeed! I did that, added integration tests for both options and documented the change. Clearly something's still wrong, and tests do pass on main. I'm investigating... |
Oh I can explain this easy enough: the full test suite is built for both node and browser testing, and so the new test is being run as part of the browser test run too, but lots of the code inside (Many other tests avoid this by importing via the package root entrypoint, which uses the Since you're actually just testing this one function, and that function will run just fine in a browser, the easiest option is to extract this into a different file that is browser-compatible (one of the utils files for example) then import just that file in the tests, and then all the tests should run fine. |
That makes sense, thanks. I moved it to a new util file that has its own spec file. Looks like it's all green now! 🥳 |
Nice work, thanks! 👍 |
This PR adds support for hostname wildcards in
passThroughHostnames
andinterceptOnlyHostnames
, as previously requested in #162.Wildcard matching is done using
URLPattern
, an upcoming web standard that's perfect for this use case and may serve other purposes in mockttp. It is already implemented in many browsers and nodeJS is working on it. It is currently implemented usingurlpattern-polyfill
.I changed the signature of
shouldPassThrough
to acceptURLPattern[]
instead ofstring[]
, so we don't need to keep recreating the objects. This is purely internal, the external API is unchanged.shouldPassThrough
is now unit tested. I used unit tests as it seemed fitting and some of the integration tests were already failing onmain
- I'm not sure why.Let me know if you're happy with these changes, I'll then update the docs.