添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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

tl;dr : I need the g flag or so-called Global match (at least in the JavaScript terminology)

Currently I'm limited in the "regexExtract" if I want to extract multiple matches.

{{ regexExtract '/path/1/path/2' '\d+' 'parts' }}{{ parts.0 }} {{ parts.1 }}

Expected result: 1 2
Actual result: 1

I know that parts is currently reserved only for match groups. And multiple matches as such are simple not implemented in https://github.com/tomakehurst/wiremock/blob/2.26.3/src/main/java/com/github/tomakehurst/wiremock/extension/responsetemplating/helpers/RegexExtractHelper.java#L33

We can also think about an alternative helper for that, e.g.

{{ regexExtractAll '/path/1/path/2' '(\s+)\\(\d+)' 'parts' }}{{ parts.0.0 }} {{ parts.0.1 }} {{ parts.1.0 }} {{ parts.1.1 }}
Expected result: path 1 path 2

PS: sorry if I made some mistakes in the regexpes above, not my strongest point, however I hope that the idea is clear

thanks for a quick response!

I looked into your unit tests here https://github.com/tomakehurst/wiremock/pull/1400/files

it's a bit uncommon to me to see comma separated values in the path rather than query params, but technically speaking it's kinda the same, so I feel like this is a work in the right direction

my use case is a bit more than just that and it's my bad I didn't explain it in detail in this issue

I also need uniformity in the way how single value or a comma separated list of values are parsed, let me illustrate it with the code based on your unit tests

// ...
mockRequest()
  .url("/items?ids=1"),
aResponse()
  .withBody("{\"test\": \"{{regexExtract request.query.ids '(\d+)' 'parts'}}{{#each parts}}{{this}} {{/each}}\"}").build(),
// ...
assertThat(responseDefinition.getBody(), is("{\"test\": \"1 \"}"));
// ...
mockRequest()
  .url("/items?ids=1,2,3"),
aResponse()
  .withBody("{\"test\": \"{{regexExtract request.query.ids '(\d+)' 'parts'}}{{#each parts}}{{this}} {{/each}}\"}").build(),
// ...
assertThat(responseDefinition.getBody(), is("{\"test\": \"1 2 3 \"}"));

please also take a look into #1344 which has a slightly different perspective on this problem, maybe even more standard compliant in the way how arrays are passed in query params

my problem can be solved either way, because I have enough control over the frontend and backend to choose between /items?ids=1,2,3 or /items?ids[]=1&ids[]=2&ids[]=3

Yep, exactly, it supports your other examples as well. It is just a regex matcher on a string value.

For the /items?ids[]=1&ids[]=2&ids[]=3 use case, this is a different case I think, and should be handled separately in #1344 , do you agree?

@kwoding yes, from the wiremock perspective it's a different case, therefore I created a separate issue for it

I'd like to test your code locally with my project (not sure how to make a custom build of wiremock, I will need to dive into that), but so far looks promising

meanwhile maybe it's nice to involve the author or one of the contributors to take a look at this.