mmock icon indicating copy to clipboard operation
mmock copied to clipboard

Partial match and unordered match of request body

Open mattburman opened this issue 4 years ago • 4 comments

I want to match bodies with at least a set of specific key-value pairs in either a JSON-encoded or www-form-urlencoded body.

This is a probably insecure example but: if Content-Type is application/json and the body is { "username": "*", "password": "*" } then i respond with 200. Any valid json string specifying the username and password would ideally match that, but instead it has to match exactly, spaces and all. So the order of the fields shouldn't matter either e.g.: { "password": "*", "username": "*" } should be 200 too.

So then I could then match on just the username too for example, and say password required if there's at least some valid string with a username. So I could match { "username": "*" } or {"username" :"*"} or any other valid json string to return something like password required.

And a similar behaviour for application/x-www-form-urlencoded - the order shouldn't matter in the string as long as the specified values match.

Is that kind of thing currently possible? I can't see an easy way to do it when body is just a string match.

mattburman avatar Apr 30 '20 14:04 mattburman

I am running into this exact same issue. I have a body where I want to match on one parameter, but the others will vary by test case and aren't relevant to the data I'm submitting. Example:

{ "A": "THIS FIELD IS CONSISTENT", "B": "THIS FIELD VARIES AND CAN BE IGNORED" }

I would like to be able to specify the body as

{ "A": "A REQUIRED VALUE", "B": * }

dkoontz avatar Aug 05 '20 18:08 dkoontz

I am running into this exact same issue. I have a body where I want to match on one parameter, but the others will vary by test case and aren't relevant to the data I'm submitting. Example:

{ "A": "THIS FIELD IS CONSISTENT", "B": "THIS FIELD VARIES AND CAN BE IGNORED" }

I would like to be able to specify the body as

{ "A": "A REQUIRED VALUE", "B": * }

@dkoontz I had a similar issue until I figured that body payload must match the text exactly, the wildcard just allow anything in that position of the string. check if your submitted payload matches the break lines and spaces in your request config. I had a similar issue because my payload was sent as compacted JSON (one line, no spaces) and the config had break lines.

For example:

request:
 method: GET
  path: /
  body: >
    { "A": "A REQUIRED VALUE", "B": * }

should match a payload

{ "A": "A REQUIRED VALUE", "B": "THIS FIELD VARIES AND CAN BE IGNORED" }

but won't match

{ "A": "A REQUIRED VALUE", 
  "B": "THIS FIELD VARIES AND CAN BE IGNORED" }

ericchaves avatar Aug 24 '20 12:08 ericchaves

I also got stuck with this issue and was surprised to find it's doing string matching on a JSON body. Request matching already has queryStringParameters to match on a proper object which is great. I imagine the next logical extension would be doing this for request bodies, whether they be JSON or HTML multi-part.

Something like this for a parsable body.

  • The "*" lives-on as a string wildcard.
  • Fields can be marked as optional
  • Constraints on enum-type fields. Though this may be getting out of scope for a first draft of this.
request:
  method: POST
  path: /auth/sessions
  bodyParams:
    username: "*"
    password: "*"
    remember_me:
      one_of: ["true", "false"]
    another_field:
      optional: true

Same thing with JSON config:

{
  "request": {
    "method": "POST",
    "path": "/auth/sessions",
    "bodyParams": {
      "username": "*",
      "password": "*",
      "remember_me": {
        "one_of": [
          "true",
          "false"
        ]
      },
      "another_field": {
        "optional": true
      }
    }
  }
}

mroach avatar Sep 15 '20 08:09 mroach

apparently it counts spaces as well on wildcard matching and ignores the application/json header to compare the objects. so currently it supports only full body wildcard and not a partial matching

dennypenta avatar Feb 20 '24 13:02 dennypenta