Hello, I have a problem with the regexExtract help...
# help
k
Hello, I have a problem with the regexExtract helper. I already wrote a test inside the sourcecode, but that succeeds. Hopefully one can help me. I have this mapping:
Copy code
{
      "request": {
        "method": "GET",
        "urlPath": "/api/search",
        "queryParameters": {
          "query": {
            "matches": "source=(.*?)\\^target=(.*?)\\^status=1"
          }
        }
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": "application/json"
        },
        "jsonBody": {
          "result": [
            {
              "id": "{{ regexExtract request.query.query '=(.*?)\\^' 'ids' }}{{ids.0}}-{{ids.1}}"
            }
          ]
        }
      }
This test runs successfully:
Copy code
@Test
  public void canExtractSingleRegexMatchWithCircumflex() {
    final ResponseDefinition responseDefinition =
            transform(
                    transformer,
                    mockRequest().url("/api/?q=filter%3Done%5Efilter%3Dtwo%5Esort%3Ddesc"),
                    aResponse()
                            .withBody("{\"test\": \"{{regexExtract request.query.q 'filter=(.*?)\\^' 'parts'}}{{#each parts}}{{this}} {{/each}}\"}"));

    assertThat(responseDefinition.getBody(), is("{\"test\": \"one two \"}"));
  }
But when I send a request against wiremock I get:
Copy code
[ERROR: Nothing matched =(.*?)\\^]-
I deploy wiremock inside a kubernetes cluster.
I also tried to escaping the circumflex with more or less backslashes or surrounding it like
[=(.*?)\\^]
.
l
What request are you sending to the
/api/search
stub ?
k
the request is:
/api/search?query=source%3D23456789987654322345678998765432%5Etarget%3D98765432234567899876543223456789%5Estatus%3D1
r
the issue is the use of the jsonBody field. response templating and the jsonBody field don't play nice when escaping characters due to how the json is serialised during templating. replacing the entire
"jsonBody"
field with a
"body"
field should fix it.
Copy code
"body" : "{\"result\": [{\"id\": \"{{ regexExtract request.query.query '=(.*?)\\^' 'ids' }}{{ids.0}}-{{ids.1}}\"}]}"
k
Thanks @Rafe Arnold that worked 👍