smocker-dev / smocker

Smocker is a simple and efficient HTTP mock server and proxy
https://smocker.dev
MIT License
1.14k stars 61 forks source link

JSON body match vs one field match possible bug #280

Closed adaamz closed 1 year ago

adaamz commented 1 year ago

Hey, I would like to have mock matching on one specific field (other fields are present but I don't care about their values yet). When I create the mock, it looks like this

- request:
    path:
        matcher: ShouldEqual
        value: /data
    method:
        matcher: ShouldEqual
        value: POST
    body: |
        obj:
            matcher: ShouldEqual
            value: aaa
  response:
    body: |-
        {"columns": ["asdf"], "rows": [["def"]]}
    status: 200
  context: {}
  state:
    id: dfsfsdfds
    times_count: 0
    locked: false
    creation_date: 2023-05-26T09:40:08.828683581Z

after I open UI, the body is then changed to

    body: |
        matcher: ShouldEqual
        value: "obj:\n    matcher: ShouldEqual\n    value: application\n"

which is match on whole request body, not only on one specific field - and this causes that my requests are not matched and smocker returns mock not found error.

Is this bug? Thanks for help.

gwleclerc commented 1 year ago

HI, Body matcher (body in request) is a plain object. You can see an example here: https://smocker.dev/technical-documentation/mock-definition.html#body-matcher

So you should try:

- request:
    path:
        matcher: ShouldEqual
        value: /data
    method:
        matcher: ShouldEqual
        value: POST
    body:
        obj:
            matcher: ShouldEqual
            value: aaa
  response:
    body: |-
        {"columns": ["asdf"], "rows": [["def"]]}
    status: 200
  context: {}
  state:
    id: dfsfsdfds
    times_count: 0
    locked: false
    creation_date: 2023-05-26T09:40:08.828683581Z
adaamz commented 1 year ago

Yeah, looks like the GUI generated wrong yaml, I fixed it in my yaml and it probably works 🙂 thx