6

I need to use wiremock to test a POST request that's sending data like this:

{
    "name": "known fixed value",
    "dateOfBirth": 5123456789000,
    "email": "known fixed value",
    "currentDate": any numeric value,
    "status": any text value with alphabets, numbers and symbols
}

The 1st 3 fields, name, dateOfBirth and email are fixed, known values, that don't change from one request to the next.

The last 2 fields, currentDate and status change randomly from one request to the next, but are mandatory fields that can hold any value.

How do I design a mapping that tests this?

Thanks in advance.

johncougar
  • 276
  • 2
  • 3
  • 10

2 Answers2

4

You can use a JsonPath regex request body matcher, for example in your case you should use this JsonPath:

$[?(@.name == 'known fixed value' && @.dateOfBirth == 5123456789000 && @.email == 'known fixed value' && @.currentDate =~ /[0-9]*/i && @.status =~ /.*/i)]

Which will match an example request body:

{
"name": "known fixed value",
"dateOfBirth": 5123456789000,
"email": "known fixed value",
"currentDate": 23123,
"status": "rfjhg33443"
}
Wojtek
  • 1,410
  • 2
  • 16
  • 31
3

If you use JSON stubbing, you can write

  "request": {
    "bodyPatterns": [
      {
        "equalToJson": {
          "name": "known fixed value",
          "dateOfBirth": 5123456789000,
          "email": "known fixed value",
          "currentDate": "${json-unit.any-number}",
          "status": "${json-unit.any-string}"
        }
      }
    ]
  }

Ref: section "Placeholders" in http://wiremock.org/docs/request-matching/

michaldo
  • 4,195
  • 1
  • 39
  • 65