Multiple mocks matchers
So I was writing some tests for my code. In one of my functions I was calling two APIs (for different purposes). So I needed to have two mockers. one of them(route2) should have had a matcher for the request body while the other (route1) did not. (For this, I had make two mockers with gock.New) Then I traced an unexpected behavior. When I was calling the API to route2, the matcher registered on route1 mocker was being called. Therefore, I looked up in the code to see the reason and I found out that there was a global variable named "Matchers" in "matcher.go". And this causes all the mockers to have the same matchers which I believe is a bug. My test code:
//route1
gock.New("http://emad.host").
Get("/notifications/bucketname").
Reply(200).
JSON(map[string]interface{}{
"ok": "ok",
})
//route2
gock.New("http://emad.host").
Put("/notifications/bucketname").
AddMatcher(func(r *http.Request, request *gock.Request) (bool, error) {
fmt.Println("matcher called")
return true, nil
}).Reply(400)
client := &http.Client{}
//this request would call matcher of route2 (unexpected behaviour)
request1, _ := http.NewRequest("GET", "http://emad.host/notifications/bucketname", nil)
fmt.Println(client.Do(request1))
request2, _ := http.NewRequest("PUT", "http://emad.host/notifications/bucketname", bytes.NewReader([]byte(`hello world`)))
fmt.Println(client.Do(request2))
This is something that has been discussed previuosly here: https://github.com/h2non/gock/pull/26
You are very welcome to contribute. I'm open to include this capability. I have implemented it in pook and I'm relatively happy with that design decision.
This is a variation of #20 and is fixed by pr #55