Hi! I'm hoping this is the right channel to ask th...
# pactflow
r
Hi! I'm hoping this is the right channel to ask this question ... we are using the "Hosted Stubs" feature of PactFlow and are having some trouble coming up with regular expressions for dynamic paths that have path parameters followed by other information. For example, we have two endpoints "/accounts/{accountId}" and "/accounts/{accountId}/transactions" that we would expect two different responses. Currently we are using the regex expressions "\/accounts\/[A-Za-z0-9]+" and "\/accounts\/[A-Za-z0-9]+\/transactions" to try and accomplish this, which works fine for "/accounts/123", but "/accounts/123/transactions" is returning the same response. Anyone have any guidance on how to get around this? It looks like somehow the stub is returning the first path that contains that regex, and does not match it exactly. I have already tried "^\/accounts\/[A-Za-z0-9]+$" but it seems like the ^ and $ are being taken as the characters themselves, not the beginning and end. Any help would be appreciated :)
👋 2
y
Hey hey, this channel is grand. Hard to choose sometimes, we get the same here at the Pact/Pactflow team I think you want an optional capture group on the
/transactions
https://rubular.com/
\/accounts\/[A-Za-z0-9]+(?:\/transactions)?
For example, we have two endpoints "/accounts/{accountId}" and "/accounts/{accountId}/transactions" that we would expect two different responses.
Oh do you want a capture group that only captures
/accounts/123/transactions
but excludes
/accounts/123
?
in that case you can drop the optional group
Copy code
\/accounts\/[A-Za-z0-9]+\/transactions
for just matching
/accounts/123
and excluding anything with an additional forward slash after your alpha/digit capture group
\/accounts\/[A-Za-z0-9]+(?!\/|[A-Za-z0-9])
r
Basically, there are two interactions that I need to account for, so I need both scenarios described 🙂 I would expect /accounts/123 to return one response but /accounts/123/transactions to return another. But that last one may be what I'm looking for! Thanks, let me give it a try
👍 1
Yes that ended up working! The key was to exclude the "/" in the first call. Appreciate the help!
One last update to this - I ended up switching approaches and just adding a "$" at the end to make "\/accounts\/[A-Za-z0-9]+$" etc.. For some reason if I don't set an example the Generex treats the $ as its own character and not the end of the string and throws an error like the below:
Error - value to generate "/accounts/el$" does not match regular expression /\/accounts\/[A-Za-z0-9]+$/
But, if I pass in an example myself that actually fits the regex (ex. /accounts/123) this approach works great.