h2non / gock

HTTP traffic mocking and testing made easy in Go ༼ʘ̚ل͜ʘ̚༽

Home Page:https://pkg.go.dev/github.com/h2non/gock

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not match any requests

yoelfme opened this issue · comments

commented

I'm doing tests with Consul lib, and I'm trying to intercept the requests to Consul but I'm receiving the following error:

Expected nil, but got: &url.Error{Op:"Get", URL:"http://127.0.0.1:8500/v1/kv/bots/my-bot/?recurse=", Err:(*net.OpError)(0xc0000ba3c0)}

that means that I'm not intercepting the requests to Consul, and this is my code:

func TestGetBotById(t *testing.T) {
	consulURL := fmt.Sprintf("/v1/kv/bots/%s/", fakeBotID)
	consulData := helperLoadBytes(t, "consul/bots.json")

	defer gock.Off()

	gock.New("http://127.0.0.1:8500").
		Get(consulURL).
		MatchParams(map[string]string{
			"recurse": "",
		}).
		Persist().
		Reply(http.StatusOK).
		BodyString(string(consulData))

	botInfo, err := helpers.GetBotByID(fakeBotID)
	expectedValue := map[string]string{
		"host": "my-host",
		"port": "3000",
	}
	assert.Nil(t, err)
	assert.NotNil(t, botInfo)
	assert.Equal(t, expectedValue, botInfo, "should be equals to")
	assert.True(t, gock.IsDone(), true)
}

the function helpers.GetBotById is making the requests to Consul but grock can't intercept it

does someone has seen the same error?

commented

Make sure your Consul code/client http.Client is being explicitly intercepted by gock.

You have an example here about how to intercept a custom http.Client instance:
https://github.com/h2non/gock/blob/master/_examples/custom_client/client_test.go#L20