Skip to content

Different JSON array body matches #81

@stevenferrer

Description

@stevenferrer

First, thank you for making this amazing project!

I've noticed that when I try to match a request body that is an array, the requests are still matching even though the mock and request body are clearly different.

baseURL := "http://foo.com"
gock.New(baseURL).
    Post("/bar").
    MatchType("json").
    BodyString(`[{"foo":"bar"},{"fizz":"buzz"}]`).
    Reply(http.StatusOK).
    JSON(map[string]interface{})

req, _ := http.NewRequest(http.MethodPost, baseURL+"/bar", bytes.NewBuffer([]byte(`[{"foo":"baz"}]`)))
res, err := http.DefaultClient.Do(req)

On the code, before getting into the lines where the json.Unmarshal is called, I've noticed that the matcher already returns true in the regexp.MatchString part.

I've tried to come-up with a fix, but it seems that it's gonna be difficult without knowing whether the request body contains regular expressions or just a normal JSON.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions