Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nil error on request without body and body matcher #62

Open
Scott-Meyer opened this issue May 22, 2024 · 0 comments
Open

Nil error on request without body and body matcher #62

Scott-Meyer opened this issue May 22, 2024 · 0 comments

Comments

@Scott-Meyer
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant