Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wiremock Capture path param and return in the response body

Tags:

wiremock

I am trying to create dynamic mocks using WireMock. I have a situation where if I specify URL like :

http://localhost:8089/api/account/[email protected]

then I should receive response like:

{ 
  "account" : "[email protected]" 
}

In brief, the path param is returned in the response body. I am able to make the request URL generic by using urlPathPattern set to /api/account/([a-z]*) however, I am not sure how should I capture [email protected] and return this in the response using regular expressions.

like image 521
JavaMan Avatar asked Nov 18 '18 03:11

JavaMan


1 Answers

In WireMock the regular expressions can be used to recognize the email format in the Request Matching. For the purpose of this example I used a very crude example. Your production implementation may require a more robust approach.

This request:

http://localhost:8181/api/account/[email protected]

Is matched by this rule:

{
    "request": {
        "method": "GET",
        "urlPathPattern": "/api/account/([a-z]*@[a-z]*.[a-z]*)"
    },
    "response": {
        "status": 200,
        "jsonBody": {
            "account": "{{request.path.[2]}}"
        },
        "transformers": ["response-template"],
        "headers": {
            "Content-Type": "application/json"
        }
    }
}

And returns this response:

{
  "account": "[email protected]"
}

It makes use of a Response Template processing functionality in WireMock. The Request Model variables [{{request.path.[2]}}] can be used to obtain sections from the request.

like image 69
A. Kootstra Avatar answered Sep 29 '22 02:09

A. Kootstra