Gock: cannot match any request json
Joseph94m opened this issue · comments
Hey
Here's my code:
defer gock.Off()
gock.New("https://foo.com").
Post("/bar").
MatchHeader("Authorization", "12345").
MatchHeader("Accept", "^"+regexp.QuoteMeta("application/vnd.api+json")+"$").
MatchHeader("Content-Type", "^"+regexp.QuoteMeta("application/vnd.api+json")+"$").
// MatchType("json").
// JSON(map[string]string{"foo": "bar"}).
Reply(201).
JSON(map[string]string{"bar": "foo"})
var reqBody io.Reader
body:=`{"foo": "bar"}`
fmt.Println(body)
bodied := json.RawMessage(body)
jsonDocument, err1 = json.Marshal(bodied)
reqBody = bytes.NewReader(jsonDocument)
statusCode, resBody, err = request("https://foo.com/bar", "POST", "12345", reqBody)
And the request function:
func request(url string, method string, authorization string, body io.Reader) (int, interface{}, error)
{
req, _ := http.NewRequest(method, url, body)
req.Header.Add("Content-Type", applicationType)
req.Header.Add("Authorization","Bearer "+authorization)
req.Header.Add("Accept", applicationType)
client := &http.Client{}
res, err := client.Do(req)
....
}
Works fine without the MatchType("json")
and JSON(json.RawMessage(
{"foo": "bar"}))
. When I comment either of them, the request fails and prints
gock: cannot match any request
I've tried changing many things. Including: removing the RawMessage transformation, changing the body definition e,g. body:=map[string]string{"foo": "bar"}
.
Not sure what I am doing wrong, can you recommend something?
Edit: Using the request function in an actual deployment does work with {"foo": "bar"}, but not in Gock.
I just ran into this same problem.
I have the same problem upon matching request body.
With version v1.1.2, the following worked for me despite the nested JSON object listing fields in a different order (based on gock.Observe(gock.DumpRequest)
:
gock.New("https://api.github.com").
Post("/graphql").
MatchHeader("Content-Type", "application/json; charset=utf-8").
JSON(map[string]interface{}{
"query": mutationUpdateProjectV2,
"variables": map[string]interface{}{
"id": "PN_2",
"title": "title",
"description": "description",
"body": "body",
"public": true,
},
}).
Reply(200).
JSON(`{
"data": {
"updateProjectV2": {
"projectV2": {
"url": "https://github.com/users/heaths/projects/2"
}
}
}
}`)
Hi I am also having issue with nested JSON object.
I tried the same snippet of the code provided above but I am unable to mock the POST call with body containing nested JSON.
Please find my code snippet
My test output is as follows:
go test
"{\n\t\t"query": "mutationUpdateProjectV2",\n\t\t"variables":{\n\t\t\t"id": "PN_2",\n\t\t\t"title": "title",\n\t\t\t"description": "description",\n\t\t\t"body": "body",\n\t\t\t"public": true,\n\t\t},"
POST /graphql HTTP/1.1
Host: api.github.com
User-Agent: Go-http-client/1.1
Content-Length: 251
Content-Type: application/json
Accept-Encoding: gzip
"{\n\t\t"query": "mutationUpdateProjectV2",\n\t\t"variables":{\n\t\t\t"id": "PN_2",\n\t\t\t"title": "title",\n\t\t\t"description": "description",\n\t\t\t"body": "body",\n\t\t\t"public": true,\n\t\t},"
Matches: false
2023/03/28 18:16:42 An Error Occured Post "https://api.github.com/graphql": gock: cannot match any request
exit status 1
FAIL ut 0.907s
Please provide your inputs If I am missing any?