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

Body matching for Request

richiejenkins opened this issue · comments

I'm trying to match the body of a request as shown below using BodyString on Request. However I am receiving gock: cannot match any request. When I use it without BodyString it is fine. gock.Observe(gock.DumpRequest) confirms the value is indeed in the request. Any help much appreciated.

func TestContentClient_UpdateNode(t *testing.T) {
	dummy := struct {
		ServerUrl string
		Token     string
		NodeId    string
		Body      string
	}{
		ServerUrl: "http://test.server",
		Token:     "TOKEN",
		NodeId:    "100",
		Body:      "Hello",
	}

	defer gock.Off()
	gock.Observe(gock.DumpRequest)
	gock.New(dummy.ServerUrl).
		Put("/nodes/"+dummy.NodeId+"/content").
		MatchHeader("Content-Type", "application/octet-stream").
		BodyString(dummy.Body).
		Reply(http.StatusOK).
		JSON(NodeEntry{
			Node{
				ID: dummy.NodeId,
			},
		})

	client := NewContentClient(dummy.ServerUrl, dummy.Token)
	node, err := client.UpdateNode(dummy.NodeId, dummy.Body)

	assert.NoError(t, err)
	assert.Equal(t, dummy.NodeId, node.ID)
}