Slackbot
09/07/2023, 7:29 PMAaron
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!Brandon Trautmann
09/07/2023, 7:42 PMdevice_id
”Aaron
09/07/2023, 7:44 PMAaron
09/07/2023, 7:46 PMequalToJson
with "ignoreExtraElements": true
https://wiremock.org/docs/request-matching/#less-strict-matching
{
"request": {
...
"bodyPatterns" : [ {
"equalToJson" : "{ \"total_results\": 4 }",
"ignoreArrayOrder" : true,
"ignoreExtraElements" : true
} ]
...
},
...
}
Aaron
09/07/2023, 7:47 PM...
"equalToJson": { "total_results": 4 },
"ignoreExtraElements": true,
...
Brandon Trautmann
09/07/2023, 7:49 PMequalToJson
field 🤔Brandon Trautmann
09/07/2023, 7:49 PMBrandon Trautmann
09/07/2023, 7:50 PMBrandon Trautmann
09/07/2023, 7:50 PM{
"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 recordingBrandon Trautmann
09/07/2023, 7:52 PMAaron
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
Brandon Trautmann
09/07/2023, 8:00 PMequalToJson
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
}
Brandon Trautmann
09/07/2023, 8:01 PM{
"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"
}
]
}
Brandon Trautmann
09/07/2023, 8:01 PMbody
to http://{{Local IP}}:{{Port}}/__admin/recordings/start
Brandon Trautmann
09/07/2023, 8:02 PM{
"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”.Aaron
09/07/2023, 8:09 PMfilters
field?Aaron
09/07/2023, 8:11 PM"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 👀Brandon Trautmann
09/07/2023, 8:24 PMequalToJson
is being registered/configuredBrandon Trautmann
09/07/2023, 8:30 PMfilters
key… Makes it sound like only the requests matching that filter will be mapped/recorded? 🤔Brandon Trautmann
09/07/2023, 8:30 PMBrandon Trautmann
09/07/2023, 8:34 PMBrandon Trautmann
09/07/2023, 8:35 PMRecord 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 PMAaron
09/07/2023, 8:38 PMBrandon Trautmann
09/07/2023, 8:39 PMBrandon Trautmann
09/07/2023, 8:40 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"
}
Brandon Trautmann
09/07/2023, 8:48 PMBrandon Trautmann
09/07/2023, 8:49 PM"transformers" : [ "device_id" ],
"transformerParameters" : {
"device_id" : "000000000000000"
}
Or something 😂Brandon Trautmann
09/07/2023, 8:49 PMA 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
Brandon Trautmann
09/07/2023, 8:50 PM--extensions
Brandon Trautmann
09/07/2023, 8:50 PMBrandon Trautmann
09/08/2023, 1:23 PMjava -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.Brandon Trautmann
09/08/2023, 1:23 PM[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 borkedBrandon Trautmann
09/08/2023, 1:24 PMignoreExtraElements
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?Brandon Trautmann
09/08/2023, 1:44 PMnull
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