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

Add VerifyHost handler to ghttp #698

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions ghttp/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ func (g GHTTPWithGomega) VerifyHeaderKV(key string, values ...string) http.Handl
return g.VerifyHeader(http.Header{key: values})
}

// VerifyHost returns a handler that verifies the host of a request matches the expected host
// Host is a special header in net/http, which is not set on the request.Header but rather on the Request itself
//
// Host may be a string or a matcher
func (g GHTTPWithGomega) VerifyHost(host interface{}) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
switch p := host.(type) {
case types.GomegaMatcher:
g.gomega.Expect(req.Host).Should(p, "Host mismatch")
default:
g.gomega.Expect(req.Host).Should(Equal(host), "Host mismatch")
}
}
}

//VerifyBody returns a handler that verifies that the body of the request matches the passed in byte array.
//It does this using Equal().
func (g GHTTPWithGomega) VerifyBody(expectedBody []byte) http.HandlerFunc {
Expand Down Expand Up @@ -358,6 +373,10 @@ func VerifyHeaderKV(key string, values ...string) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).VerifyHeaderKV(key, values...)
}

func VerifyHost(host interface{}) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).VerifyHost(host)
}

func VerifyBody(expectedBody []byte) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).VerifyBody(expectedBody)
}
Expand Down
63 changes: 63 additions & 0 deletions ghttp/test_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,69 @@ var _ = Describe("TestServer", func() {
})
})

Describe("VerifyHost", func() {
var (
err error
req *http.Request
)

BeforeEach(func() {
req, err = http.NewRequest("GET", s.URL()+"/host", nil)
Expect(err).ShouldNot(HaveOccurred())
})

When("passed a matcher for host", func() {
BeforeEach(func() {
s.AppendHandlers(CombineHandlers(
VerifyRequest("GET", "/host"),
VerifyHost(Equal("my-host")),
))
})

It("should verify the host", func() {
req.Host = "my-host"

resp, err = http.DefaultClient.Do(req)
Expect(err).ShouldNot(HaveOccurred())
})

It("should reject an invalid host", func() {
req.Host = "not-my-host"

failures := InterceptGomegaFailures(func() {
http.DefaultClient.Do(req)
})
Expect(failures).Should(HaveLen(1))
})
})

When("passed a string for host", func() {
BeforeEach(func() {
s.AppendHandlers(CombineHandlers(
VerifyRequest("GET", "/host"),
VerifyHost("my-host"),
))
})

It("should verify the host", func() {
req.Host = "my-host"

resp, err = http.DefaultClient.Do(req)
Expect(err).ShouldNot(HaveOccurred())
})

It("should reject an invalid host", func() {
req.Host = "not-my-host"

failures := InterceptGomegaFailures(func() {
http.DefaultClient.Do(req)
})
Expect(failures).Should(HaveLen(1))
})
})

})

Describe("VerifyBody", func() {
BeforeEach(func() {
s.AppendHandlers(CombineHandlers(
Expand Down