https://linen.dev logo
#help
Title
# help
b

Bas Dijkstra

04/28/2023, 6:53 AM
First check: did you enable response templating when starting the standalone server using either —global-response-templating or —local-response-templating? See https://wiremock.org/docs/running-standalone/#command-line-options
k

Kaushal Gupta

04/28/2023, 6:54 AM
I am using —global-response-templating please have a look on my command
Copy code
java -cp "wiremock-webhooks-extension-2.32.0.jar;wiremock-standalone-2.32.0.jar" com.github.tomakehurst.wiremock.standalone.WireMockServerRunner --extensions "org.wiremock.webhooks.Webhooks" --local-response-templating --global-response-templating --port 7879 --verbose
b

Bas Dijkstra

04/28/2023, 7:07 AM
Have you tried starting your WireMock server that way and creating a simple test that uses response templating in a regular response definition? Also, I’m not sure response templating works with post-serve actions, maybe @Oleg Nenashev can give you a more conclusive answer?
k

Kaushal Gupta

04/28/2023, 7:10 AM
Thanks @Bas Dijkstra for the response, I have tried many other scenarios as well and the response template is working but the only issue is with post-serve actions.
b

Bas Dijkstra

04/28/2023, 7:17 AM
You’re welcome. I don’t know the WireMock source code well enough but I suspect that by the time the post-serve action is triggered, the response has been sent and the request has been discarded (except in the log). But I may be completely wrong :) I’m sure someone from the core WireMock team can shed more light on this.
🙌 1
o

Oleg Nenashev

04/28/2023, 7:35 AM
will catch up in 1 hr or so
1
n

Nikita Karpuk

04/28/2023, 10:20 AM
@Kaushal Gupta have you tried to use
Copy code
{{jsonPath originalRequest.body '$.baseUrl'}}
instead of {{request.baseUrl}} It looks like for templating in regular response supposed to be used keyword ‘*request*’ But for templating in postServeActions supposed to be used ‘*originalRequest*’
One of my playground examples
Copy code
var wiremockClient = new WireMock(wiremock.getServerPort());
wiremockClient.register(post(urlPathEqualTo("/" + CALLBACK_TRIGGER_PATH))
    .willReturn(ok()
        .withBody("Please Wait {{jsonPath request.body '$.callbackUrl'}}"))
    .withPostServeAction("webhook", webhook()
        .withMethod("{{jsonPath originalRequest.body '$.callbackMethod'}}")
        .withUrl("{{jsonPath originalRequest.body '$.callbackUrl'}}")
        .withBody("Async processing Finished")
        .withFixedDelay(1000))
);

// when
HttpResponse<String> response = TestClient.newInstance()
    .post(
        wiremock.getRequestURI(CALLBACK_TRIGGER_PATH),
        "{\"callbackMethod\": \"PUT\", \"callbackUrl\": \"" + callbackEndpointForWiremockStub  +"\"}"
    );
k

Kaushal Gupta

04/28/2023, 10:41 AM
@Nikita Karpuk I tried after the replacement {{jsonPath originalRequest.body '$.baseUrl'}} from {{request.baseUrl}} but no luck.
Copy code
{{jsonPath originalRequest.body '$.baseUrl'}}
the value for above is coming as blank only. please refer to the below screenshot
n

Nikita Karpuk

04/28/2023, 10:44 AM
Could you provide example of request you sending to wiremock And example of callback you are expecting
k

Kaushal Gupta

04/28/2023, 10:45 AM
request example
Copy code
{
  "request": {
    "url": "/wiremock/middesk/v1/businesses",
    "method": "POST",
    "bodyPatterns": [
      {
        "matchesJsonPath": {
          "expression": "$..tin",
          "contains": "123"
        }
      }
    ]
  },
  "response": {
    "status": 200,
    "bodyFileName": "middesk-compliance-partner/customer_response/middeskInitiateResponse_success.json",
    "headers": {
      "Content-Type": "application/json; charset=utf-8"
    }
  },
  "postServeActions": [
    {
      "name": "webhook",
      "parameters": {
        "headers": {
          "Content-Type": "application/json"
        },
        "method": "POST",
        "url": "{{jsonPath originalRequest.body '$.baseUrl'}}/callback",
        "delay": {
          "type": "fixed",
          "milliseconds": 10
        }
      }
    }
  ]
}
n

Nikita Karpuk

04/28/2023, 10:49 AM
I mean real request that application sends Like
Copy code
POST 127.0.0.1:8888/wiremock/middesk/v1/businesses
{
 "baseUrl": "my-callback-url"
 "someOtherField" : "some value"
}
I am trying to understand if
baseUrl
a part of body or something else
k

Kaushal Gupta

04/28/2023, 10:50 AM
yes curl --location 'http://127.0.0.1:7879/wiremock/middesk/v1/businesses' \ --header 'Content-Type: application/json' \ --data '{ "tin":"123" }'
baseUrl
is not a part of body
n

Nikita Karpuk

04/28/2023, 10:52 AM
Could you also provide example of expected callback url?
k

Kaushal Gupta

04/28/2023, 10:55 AM
so basically host and port should be remain same as incoming request,,
n

Nikita Karpuk

04/28/2023, 10:59 AM
you could try to play with params like
originalRequest.url
- URL path and query - example
{{originalRequest.url}}
originalRequest.path
- URL path - example
{{originalRequest.path}}
originalRequest.pathSegments.[<n>]
- URL path segment (zero indexed) e.g.
{{originalRequest.pathSegments.[2]}}
And
originalRequest.baseUrl
- URL up to the start of the path e.g. https://my.example.com:8080
{{originalRequest.baseUrl}}
k

Kaushal Gupta

04/28/2023, 11:10 AM
Thanks @Nikita Karpuk it worked finally for me, very very thanks u so much :)
👍 1
n

Nikita Karpuk

04/28/2023, 11:11 AM
@Kaushal Gupta you are very welcome
❤️ 1
k

Kaushal Gupta

05/02/2023, 4:52 AM
@Nikita Karpuk @Oleg Nenashev Good Morning, I need your help as I am trying to perform one more scenario that is I am trying to send the one field value in callback Url as a query parameter from the stubbed response and also using the same host and port of incoming coming request for callback URL: post server action callback URL will be like below:
Copy code
"url": "<incomingRequestHost>:<incomingRequestPort>/middesk/callback?id={{response.id}}

where id is the field inside the stubbed response 
 "id": "{{randomValue type='UUID'}}",
but above one example is not working
n

Nikita Karpuk

05/06/2023, 4:48 PM
@Kaushal Gupta as far as i know you could not get value from response to use it in a postServeAction Personally i did it via Dynamic Stabs using Java 1.
var id = UUID._randomUUID_().toString()
2. Create dynamic stub with response and postServeAction
Copy code
wiremockClient.register(post(urlPathEqualTo(CALLBACK_TRIGGER_PATH))
            .willReturn(ok()
                ...
                .withBody("id =" + id))
            .withPostServeAction("webhook", webhook()
                ...
                .withUrl("..." + "?id=" + id)
                .withFixedDelay(1000))
        );
k

Kaushal Gupta

05/08/2023, 4:48 AM
Hi @Nikita Karpuk I got the work around to resolve this : 1. used ninecookies callback simulator com.ninecookies.wiremock.extensions.CallbackSimulator 2. created one more fields in stubbed response like "baseUrl": "{{request.baseUrl}}". stubbed response body is below:
Copy code
{
  "id": "{{randomValue type='UUID'}}",
  "name": "{{jsonPath request.body '$.name'}}",
  "baseUrl": "{{request.baseUrl}}",
  "tags": [],
  "addresses": [
    {
      "object": "address",
      "address_line1": "{{jsonPath request.body '$.addresses[0].address_line1'}}",
      "address_line2": "{{jsonPath request.body '$.addresses[0].address_line2'}}",
      "city": "{{jsonPath request.body '$.addresses.[0].city'}}",
      "state": "{{jsonPath request.body '$.addresses.[0].state'}}",
      "postal_code": "{{jsonPath request.body '$.addresses[0].postal_code'}}",
      "created_at": "2019-02-04T18:17:20.533Z",
      "updated_at": "2019-02-04T18:17:20.533Z"
    }
  ]
}
3. Created post server action like below
Copy code
"postServeActions": {
    "callback-simulator": {
      "callbacks": [
        {
          "delay": 10,
          "url": "$(response.baseUrl)/middesk/callback",
          "data": {
            "object": "event",
            "id": "$(response.id)",
            "type": "business.updated",
            "data": {
              "object": {
                "id": "$(response.id)",
                "created_at": "2023-05-02T23:54:48.154Z",
                "updated_at": "2023-05-02T23:54:48.154Z",
                "external_id": null,
                "phone_numbers": [],
                "registrations": []
              }
            },
            "created_at": "2023-05-02T23:54:48.239Z"
          }
        }
      ]
    }
  }
4. run the standalone server with below command.
Copy code
java -cp "wiremock-webhooks-extension-2.32.0.jar;wiremock-standalone-2.32.0.jar;wiremock-extensions-0.0.6-jar-with-dependencies.jar" com.github.tomakehurst.wiremock.standalone.WireMockServerRunner --extensions "com.ninecookies.wiremock.extensions.CallbackSimulator,com.ninecookies.wiremock.extensions.JsonBodyTransformer,org.wiremock.webhooks.Webhooks" --port 7879 --verbose --local-response-templating --global-response-templating
It worked successfully !!! :)
👍 1
o

Oleg Nenashev

05/08/2023, 8:46 AM
👍
🙌 1
9 Views