1

I've written some tests in Go using Ginkgo/Gomega, but no matter what I do, ghttp returns a 500 status to my client with no content. Here is some sample code:

var _ = Describe("Client", func() {

    var (
        server *ghttp.Server
    )

    BeforeEach(func() {
        server = ghttp.NewServer()
        server.AllowUnhandledRequests = false
        server.Writer = GinkgoWriter
    })

    AfterEach(func() {
        server.Close()
    })

    Describe("fetching a node list", func() {

        BeforeEach(func() {

            server.AppendHandlers(
                ghttp.CombineHandlers(
                    ghttp.VerifyRequest("GET", "/nodes"),
                    ghttp.RespondWith(204, ""),
                ),
            )
        })

        It("should be able to fetch a node list", func() {
            response, err := myclient.Get(NODES)
            Ω(err).ShouldNot(HaveOccurred())
            Ω(response).ShouldNot(BeNil(), "No response was returned from nodes.")

            data, err := httputil.DumpResponse(response, true)
            Ω(err).ShouldNot(HaveOccurred())
            GinkgoWriter.Write(data)

            Ω(response.StatusCode).Should(Equal(204))
        })

    })

})

According to the Gomega changelog:

If a registered handler makes a failing assertion ghttp will return 500.

If a registered handler panics, ghttp will return 500 and fail the test.

However, I can't get these tests to fail, even if I post to a different route, remove the append handlers call, etc. Any suggestions would be appreciated!

Community
  • 1
  • 1
bbengfort
  • 5,254
  • 4
  • 44
  • 57

1 Answers1

0

Hopefully the OP figured it out as this is way late to the game. I had the same issue so sharing for anyone else that comes across it.

In my case, my It function was outside the Context function but you won't get any errors at compile time. Inside the nested Describe You want a structure like:

Describe
  Context
    BeforeEach
    It

Note where the BeforeEach and the It are contained within the Context.

Hope this helps someone.