Skip to content

Nil error on request without body and body matcher #62

@Scott-Meyer

Description

@Scott-Meyer

Describe the bug
If you submit a request to the Mocha server without a body, and you have a matcher with a body match, mocha gets a memory panic

To Reproduce
Create mocha server

func TestMochaNil(t *testing.T) {
	m := mocha.New(t, mocha.Configure().Addr("localhost:8080").Build()).CloseOnCleanup(t)

	// Start mock CCM API
	m.Start()

	m.AddMocks(
		// Missing data for runConfig
		mocha.Post(expect.URLPath("/test")).
			Body(expect.JSONPath("data", expect.ToContain("aaa"))).
			Reply(reply.OK().BodyJSON("hello world")),
		// Metrics cost mapper
		mocha.Get(expect.URLPath("/test")).
			Reply(reply.OK().BodyJSON("hello world")),
	)

	url := "http://localhost:8080/test"

	// Create a new HTTP client
	client := &http.Client{}

	// Create a new GET request
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		fmt.Println("Error creating request:", err)
		return
	}

	// Send the request
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("Error making request:", err)
		return
	}
	defer resp.Body.Close()

}

Expected behavior
At the very least, when the check for method "Post" fails, the loop of expect checks should short stop before the body check ever happens.

But the body check should fail if the body is nil

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions