Skip to content
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

any way to use patterns for a path matcher? #9

Closed
cameronbraid opened this issue Jun 5, 2018 · 5 comments
Closed

any way to use patterns for a path matcher? #9

cameronbraid opened this issue Jun 5, 2018 · 5 comments

Comments

@cameronbraid
Copy link

I would like to be able to use path patterns like
.get("/**")
or
.get("/?**)
or most flexible :

.match(context=>{
// custom matcher here which can access headers, query string parameters, url from the context

})

would this be hard to add ?

@pimterry
Copy link
Member

pimterry commented Jun 5, 2018

Right now there's no way to do that, no. Good idea though!

The former should be fairly easy, though I'd probably lean towards regexes instead. E.g. .get(/.*/). That lets you differentiate by type, so the * in the string isn't ambiguous (does it match a pattern, or a literal * only?).

The latter is trickier, but possible too. The problem there is that for browser support you need a live connection from the mock to the browser, which it can use to run the matching code. That can come with latency too. There are similar features I'm planning anyway (mainly for dynamic responses, rather than request matching), but the former option should definitely come first.

Interested in working on a PR for that? I'll get to it eventually, but if you're keen to use it I'd happily accept a PR adding it yourself.

@pimterry
Copy link
Member

@cameronbraid this is now implemented on master, release coming soon. Is this what you were looking for?

https://github.com/pimterry/mockttp/blob/04fe22edb8c69c33e227210609171f21057c1aaa/test/integration/matchers/method-path-matching.spec.ts#L44-L50

@cameronbraid
Copy link
Author

Nice - that looks great.. thanks

@pimterry
Copy link
Member

Excellent. Now fixed & published as Mockttp 0.7.2, so I'm going to close this. Try it out, let me know how it goes! Thanks for the suggestion 👍

@chrisribe
Copy link

chrisribe commented Jun 17, 2022

Hello,

I was looking at allowing different RegEx patterns in the url matching.
RegEx test cases

I see in the test cases you only run tests with direct regex expressions and not strings.
await server.get(/.*.txt/).thenReply(200, 'Fake file');

Tried to create a regex pattern via new RegExp("some regex pattern string") but I am having match issues.

const regExPattern = new RegExp(regExPatternString, 'g');
await server.get(regExPattern).thenReply(200, 'Fake file');

Any tips ?

[Extracted as a separate issue: #96]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants