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

ghttp Internal Server Error using NewWithT(t) and VerifyJSON #453

Closed
macaptain opened this issue Jul 7, 2021 · 0 comments · Fixed by #454
Closed

ghttp Internal Server Error using NewWithT(t) and VerifyJSON #453

macaptain opened this issue Jul 7, 2021 · 0 comments · Fixed by #454

Comments

@macaptain
Copy link
Contributor

macaptain commented Jul 7, 2021

If you set up a test using the ghttp server with NewWithT and NewGHTTPWithGomega, then use the VerifyJSON handler, the ghttp server responds with 500. (For more background, see #321 (comment) for an example of using ghttp with the XUnit style of tests calling NewWithT.)

Here's an example with github.com/onsi/gomega@v1.13.0:

package main

import (
	"bytes"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/ghttp"
	"mime"
	"net/http"
	"testing"
)

func Test(t *testing.T) {
	g := NewWithT(t)
	gh := ghttp.NewGHTTPWithGomega(g)

	server := ghttp.NewServer()
	defer server.Close()

	server.AppendHandlers(
		ghttp.CombineHandlers(
			gh.VerifyRequest("POST", "/"),
			gh.VerifyJSON("{}"),
			gh.RespondWith(http.StatusCreated, "OK"),
		),
	)

	r, _ := http.Post(server.URL(), mime.TypeByExtension(".json"), bytes.NewBuffer([]byte("{}")))
	g.Expect(r.StatusCode).To(Equal(http.StatusCreated))
}

Running the test:

 $ go test
--- FAIL: Test (0.00s)
    main_test.go:28: 
        Expected
            <int>: 500
        to equal
            <int>: 201
FAIL
exit status 1

I expect the test to pass instead of fail.

What I think is happening is that the VerifyMimeType call in VerifyJSON is using the package level gomega instead of using the GHTTPWithGomega struct that's passed around.

VerifyMimeType("application/json"),

I think this call should be g.VerifyMimeType("application/json") to use the correct gomega.

I'm happy to submit a PR to fix this so that the verify receiver functions call other receiver functions. Let me know! 😃

macaptain added a commit to Nordix/gomega that referenced this issue Jul 7, 2021
The GHTTPWithGomega struct has receiver functions to allow XUnit style
tests to use the ghttp server. However, some of these receiver functions
call out to Verify functions that use the package level gomega instead
of the struct's gomega.

Fixes onsi#453
@onsi onsi closed this as completed in #454 Jul 7, 2021
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

Successfully merging a pull request may close this issue.

1 participant