Brandon Trautmann
09/07/2023, 7:29 PM"ignoreBodyParameters": [
{
"name": "myParameter",
"nestedLevel": "*" // Ignore no matter the depth in body
},
...
]
I’m sure there’s some fancy JSON templating one could do in place of that nesting parameter but that’s the general ideaAaron
09/07/2023, 7:39 PMrequest body parameters
is throwing me for a loopBrandon Trautmann
09/07/2023, 7:41 PM{
"device_name": "...",
"device_id": "...",
...
}
The device_id
parameter is the issue here: We clear app state between our integration tests, and in doing so, our app will generate a new one. I can of course do something more invasive and, based on the app environment (which in this case would be test
), generate the same device id no matter what, but I’d prefer to handle it at the mapping layer if possible!device_id
”Aaron
09/07/2023, 7:44 PMequalToJson
with "ignoreExtraElements": true
https://wiremock.org/docs/request-matching/#less-strict-matching
{
"request": {
...
"bodyPatterns" : [ {
"equalToJson" : "{ \"total_results\": 4 }",
"ignoreArrayOrder" : true,
"ignoreExtraElements" : true
} ]
...
},
...
}
...
"equalToJson": { "total_results": 4 },
"ignoreExtraElements": true,
...
Brandon Trautmann
09/07/2023, 7:49 PMequalToJson
field 🤔{
"deviceId": "${json-unit.any-string}"
}
Aaron
09/07/2023, 7:50 PMBrandon Trautmann
09/07/2023, 7:52 PMrequestBodyPattern
parameter of the recordings/start
API cause that’s how we’re starting recordingAaron
09/07/2023, 7:56 PMBrandon Trautmann
09/07/2023, 7:59 PMAaron
09/07/2023, 7:59 PMBrandon Trautmann
09/07/2023, 7:59 PM"equalToJson" : { "total_results": 4 }
But from the API the key is matcher
which needs to be equalToJson
equalToJson
Aaron
09/07/2023, 8:00 PM"equalToJson": "{ \"total_results\": 4 }"
?Brandon Trautmann
09/07/2023, 8:01 PM{
"targetBaseUrl": "{{Target URL}}",
"captureHeaders": {
"X-Maestro-Test-Id": {}
},
"requestBodyPattern": {
"equalToJson": "{ \"device_id\": \"${json-unit.any-string}\" }"
},
"persist": true,
"repeatsAsScenarios": false
}
{
"errors": [
{
"code": 10,
"source": {
"pointer": "/requestBodyPattern/equalToJson"
},
"title": "Error parsing JSON",
"detail": "Unrecognized field \"equalToJson\" (class com.github.tomakehurst.wiremock.recording.RequestBodyAutomaticPatternFactory), not marked as ignorable"
}
]
}
body
to http://{{Local IP}}:{{Port}}/__admin/recordings/start
{
"targetBaseUrl": "{{Target URL}}",
"captureHeaders": {
"X-Maestro-Test-Id": {}
},
"requestBodyPattern": {
"matcher": "equalToJson"
},
"persist": true,
"repeatsAsScenarios": false
}
it succeeds, but obviously that’s not what I want (it’s not configuring the device id param at all)Aaron
09/07/2023, 8:02 PMBrandon Trautmann
09/07/2023, 8:03 PMAaron
09/07/2023, 8:09 PMrequestBodyPattern
is telling the recorder what to record - in this case saying “hey, record the bodyPatterns
as equalToJson
, and ignore/don’t ignore Extra Elements”.filters
field?"filters" : {
"urlPathPattern" : "/api/.*",
"method" : "GET",
"allowNonProxied": true
},
from the docs, so maybe something like…
"filters" : {
"urlPathPattern" : "/api/.*",
"method" : "GET",
"allowNonProxied": true,
"bodyPatterns": [ {
"equalToJson": "{ \"device_id\": \"${json-unit.any-string}\" }",
"ignoreExtraElements": true
} ]
},
?Brandon Trautmann
09/07/2023, 8:17 PM200
so I’m testing now 👀equalToJson
is being registered/configuredfilters
key… Makes it sound like only the requests matching that filter will be mapped/recorded? 🤔Record all requests/responses for future playbacks, but when playing back, consider this response a match for this request IFF everything about the request EXCEPT the request bodyparameter match (but matching entirely is obviously acceptable as well).device_id
Aaron
09/07/2023, 8:36 PMBrandon Trautmann
09/07/2023, 8:39 PMStubMappingTransformer
allows me to do it in an automated fashion (I meant avoiding the manual find and replace haha)!Aaron
09/07/2023, 8:40 PMBrandon Trautmann
09/07/2023, 8:48 PM"transformers" : [ "modify-response-header" ],
"transformerParameters" : {
"headerValue" : "123"
}
"transformers" : [ "device_id" ],
"transformerParameters" : {
"device_id" : "000000000000000"
}
Or something 😂A transformer is an implementations ofAh, this is the bit that I probably can’t do via CLIand needs to be registered when starting WireMock as described in Extending WireMock.StubMappingTransformer
--extensions
java -jar my-wiremock-extensions-1.0-SNAPSHOT.jar --bind-address 127.0.0.1 --extensions com.expample.DeviceIdTransformer --verbose
and when saving stub mappings it correctly transforms the mapping’s request body matcher to only be "equalToJson": "{\"password\":\"myPassword\",\"email\":\"<mailto:94669@example.org|94669@example.org>\"}"
. I learned late last night that I was thinking about the matching logic wrong. The real logic is “ignore everything from the request body other than email/password, haha. Much simpler to reason about.[custom matcher] | <<<<< custom matcher does not match
in the console when this request is hit. The header I’m diffing on as well as the URL seem to match, it’s just the body matcher that is borkedignoreExtraElements
is that a request body should be able to contain N number of elements and my equalToJson
can contain M number of elements (where M < N) and it can match so long as all elements in M are exactly the same in N, but maybe I’m wrong?null
to the 2 custom matcher fields. Previously I was just passing along the fields from the original stub mapping. Not exactly sure what changed because out output JSON stayed the same, but it worked 🙃Aaron
09/08/2023, 3:35 PM