0

I'm trying to set up some expectations in MockServer where I have a specific expectation for requests matching this path

/api/users/:user_id

using the regex /api/users/.*.

However, I have a few other expectations which I want to match when accessing user-specific resources:

/api/users/:user_id/books
/api/users/:user_id/books/:book_id
/api/users/:user_id/holidays

I'm not really sure how to properly use regexs to match the first path, without also affecting the requests coming for the user-specific resources without (i.e., all requests are matching on the first path).

For example, I believe for the /api/users/:user_id/books path, I can use the regex /api/users/.*/books, but this will never match, because the regex /api/users/.* for the first path will always match these deeper URLs.

I've been reading this page, but can't quite figure out how to correctly use the regexes for this particular case

Sasha Fonseca
  • 2,257
  • 23
  • 41

1 Answers1

0

One general approach here might be to use negative lookahead assertions. For the general case, you could use this pattern:

/api/users/\d+/(?!books|holidays)

Then, for the more specific cases, use patterns which you already had in mind, e.g.

/api/users/\d+/books
/api/users/\d+/holidays

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360