Bas Dijkstra
04/28/2023, 6:53 AMKaushal Gupta
04/28/2023, 6:54 AMjava -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
Bas Dijkstra
04/28/2023, 7:07 AMKaushal Gupta
04/28/2023, 7:10 AMBas Dijkstra
04/28/2023, 7:17 AMOleg Nenashev
04/28/2023, 7:35 AMNikita Karpuk
04/28/2023, 10:20 AM{{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*’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 +"\"}"
);
Kaushal Gupta
04/28/2023, 10:41 AM{{jsonPath originalRequest.body '$.baseUrl'}}
the value for above is coming as blank only. please refer to the below screenshotNikita Karpuk
04/28/2023, 10:44 AMKaushal Gupta
04/28/2023, 10:45 AM{
"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
}
}
}
]
}
Nikita Karpuk
04/28/2023, 10:49 AMPOST 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 elseKaushal Gupta
04/28/2023, 10:50 AMbaseUrl
is not a part of bodyNikita Karpuk
04/28/2023, 10:52 AMKaushal Gupta
04/28/2023, 10:55 AMNikita Karpuk
04/28/2023, 10:59 AMoriginalRequest.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}}
Kaushal Gupta
04/28/2023, 11:10 AMNikita Karpuk
04/28/2023, 11:11 AMKaushal Gupta
05/02/2023, 4:52 AM"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 workingNikita Karpuk
05/06/2023, 4:48 PMvar id = UUID._randomUUID_().toString()
2. Create dynamic stub with response and postServeAction
wiremockClient.register(post(urlPathEqualTo(CALLBACK_TRIGGER_PATH))
.willReturn(ok()
...
.withBody("id =" + id))
.withPostServeAction("webhook", webhook()
...
.withUrl("..." + "?id=" + id)
.withFixedDelay(1000))
);
Kaushal Gupta
05/08/2023, 4:48 AM{
"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
"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.
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 !!! :)Oleg Nenashev
05/08/2023, 8:46 AM