Hello, I have a requirement to change the stub bas...
# help
s
Hello, I have a requirement to change the stub based on http-header value which is of json type. for example : Below is one of my httpHeader "personaldetails":" {"account":"88000000","lastName":bradshaw"} now i want to create a stub. 1. if account regex is 8.* , then send stub1 as response. 2. if account regex is X.* , then send stub2 as response. how do i match the header which is of json type and how do i do regex for that field (in this case it is account) ? Any lead will be truly helpful and appreciable
t
Try creating 2 stubs, a specific one to match the 8s:
Copy code
{
  "name": "Match accounts with with 8",
  "request": {
    "urlPath": "/accounts",
    "method": "GET",
    "headers": {
      "personaldetails": {
        "matchesJsonPath": {
          "expression": "$.account",
          "matches": "8.*"
        }
      }
    }
  },
  "response": {
    "status": 200,
    "body": "1st response"
  },
  "priority": 4
}
and a generic one to match anything else:
Copy code
{
  "name": "Match any other account",
  "request": {
    "urlPath": "/accounts",
    "method": "GET"
  },
  "response": {
    "status": 200,
    "body": "2nd response"
  }
}
s
Thank you so much. My original headers are of this format. headers {"personaldetails":" {"account": {"accountNo":"88000000", "firstName":"x" ,"lastName":bradshaw"}} inside http headers, we have a key personalDetails. personaldetails is key . value is of json type. under json, we are trying to match with accountNo 8*
t
OK, I don’t know your header’s name so I’ll assume
x-my-header
. You can modify the first stub as follows:
Copy code
{
  "name": "Match accounts with with 8",
  "request": {
    "urlPath": "/accounts",
    "method": "GET",
    "headers": {
      "x-my-header": {
        "matchesJsonPath": {
          "expression": "$.personalDetails.account.accountNo",
          "matches": "8.*"
        }
      }
    }
  },
  "response": {
    "status": 200,
    "body": "1st response"
  },
  "priority": 4
}
s
thank you so much for your reply. i have another question. what if accountNo is a array and i want a matcher for the first element { "personaldetails": { "account": { "accountNo": [ "88000000" ], "firstName": "x", "lastName": "bradshaw" } } } does "$.personalDetails.account.accountNo[0]" work ?
t
Yes, that should work
s
I had tested it. it is not working. It is throwing an exception java.util.ArrayList cannot be cast to class java.lang.String. Class clast exception It is happening at com.github.tomakehurst.wiremockhandlerdispatchingservlet
t
Worked for me when I send this header value:
Copy code
{
  "personalDetails": {
    "account": {
      "accountNo": [
        "88000000"
      ],
      "firstName": "x",
      "lastName": "bradshaw"
    }
  }
}
with this stub:
Copy code
{
  "name": "Match accounts with with 8",
  "request": {
    "urlPath": "/accounts",
    "method": "GET",
    "headers": {
      "x-my-header": {
        "matchesJsonPath": {
          "expression": "$.personalDetails.account.accountNo[0]",
          "matches": "8.*"
        }
      }
    }
  },
  "response": {
    "status": 200,
    "body": "1st response"
  },
  "priority": 4
}
s
thank you Tom. this worked. really appreciate your help.
👍 1
Copy code
{
  "personalDetails": {
    "account": {
      "accountNo": [
        "88000000"
      ],
      "firstName": "x",
      "lastName": "bradshaw"
    }
  }
}
another question , can i copy the value of accountNo i.e 88000000 to a field in bodyFileName.json (under mappings) ? i did use transformer and wrote a custom method deserializing and serializing using Gson jackson libraries. is that necessary ? is there much simpler way to copy that value from input request to stub and return stub ?
t
You can copy a value into the response using the templating feature (if that’s what you mean)
Copy code
{{jsonPath request.body 'personalDetails.account.accountNo[0]'}}
(you need to ensure you enable the
response-template
transformer on the stub)
s
For this mapping,
Copy code
{
  "name": "Match accounts with with 8",
  "request": {
    "urlPath": "/accounts",
    "method": "GET",
    "headers": {
      "x-my-header": {
        "matchesJsonPath": {
          "expression": "$.personalDetails.account.accountNo[0]",
          "matches": "8.*"
        }
      }
    }
  },
  "response": {
    "status": 200,
    "bodyfilename": "result.json"
  },
  "priority": 4
}
inside result.json, i should mention this like below
Copy code
{
number_n: {{jsonPath request.headersx-my-header '$.personalDetails.account.accountNo[0]'}}
}
is this right ?
t
You need to add this under
response
:
Copy code
"transformers": ["response-template"],
Your jsonPath helper is also missing a . after
headers
s
thank you Tom. it was a typo. i did use the config. and it is returning me an empty string.for the header
Copy code
{
  "personalDetails": {
    "account": {
      "accountNo": [
        "88000000"
      ],
      "firstName": "x",
      "lastName": "bradshaw"
    }
  }
}
In result.json i used these lines
Copy code
{
number_n: {{jsonPath request.headers.x-my-header '$.personalDetails.account.accountNo[0]'}}
}
t
Try adding an index when accessing the header (otherwise you’re trying to evaluate JSONPath against a list, which we can’t do):
Copy code
{
  number_n: {{jsonPath request.headers.x-my-header.0 '$.personalDetails.account.accountNo[0]'}}
}