Skip to content

Commit 0b03b36

Browse files
authoredSep 28, 2023
Add VerifyHost handler to ghttp (#698)
Host is a field on the http.Request struct and it is useful to test that its value is correctly set. One cannot rely on checking the header (ie by VerifyHeader) alone Signed-off-by: toby lorne <toby@toby.codes>
1 parent 55a33f3 commit 0b03b36

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
 

‎ghttp/handlers.go

+19
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,21 @@ func (g GHTTPWithGomega) VerifyHeaderKV(key string, values ...string) http.Handl
112112
return g.VerifyHeader(http.Header{key: values})
113113
}
114114

115+
// VerifyHost returns a handler that verifies the host of a request matches the expected host
116+
// Host is a special header in net/http, which is not set on the request.Header but rather on the Request itself
117+
//
118+
// Host may be a string or a matcher
119+
func (g GHTTPWithGomega) VerifyHost(host interface{}) http.HandlerFunc {
120+
return func(w http.ResponseWriter, req *http.Request) {
121+
switch p := host.(type) {
122+
case types.GomegaMatcher:
123+
g.gomega.Expect(req.Host).Should(p, "Host mismatch")
124+
default:
125+
g.gomega.Expect(req.Host).Should(Equal(host), "Host mismatch")
126+
}
127+
}
128+
}
129+
115130
//VerifyBody returns a handler that verifies that the body of the request matches the passed in byte array.
116131
//It does this using Equal().
117132
func (g GHTTPWithGomega) VerifyBody(expectedBody []byte) http.HandlerFunc {
@@ -358,6 +373,10 @@ func VerifyHeaderKV(key string, values ...string) http.HandlerFunc {
358373
return NewGHTTPWithGomega(gomega.Default).VerifyHeaderKV(key, values...)
359374
}
360375

376+
func VerifyHost(host interface{}) http.HandlerFunc {
377+
return NewGHTTPWithGomega(gomega.Default).VerifyHost(host)
378+
}
379+
361380
func VerifyBody(expectedBody []byte) http.HandlerFunc {
362381
return NewGHTTPWithGomega(gomega.Default).VerifyBody(expectedBody)
363382
}

‎ghttp/test_server_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,69 @@ var _ = Describe("TestServer", func() {
499499
})
500500
})
501501

502+
Describe("VerifyHost", func() {
503+
var (
504+
err error
505+
req *http.Request
506+
)
507+
508+
BeforeEach(func() {
509+
req, err = http.NewRequest("GET", s.URL()+"/host", nil)
510+
Expect(err).ShouldNot(HaveOccurred())
511+
})
512+
513+
When("passed a matcher for host", func() {
514+
BeforeEach(func() {
515+
s.AppendHandlers(CombineHandlers(
516+
VerifyRequest("GET", "/host"),
517+
VerifyHost(Equal("my-host")),
518+
))
519+
})
520+
521+
It("should verify the host", func() {
522+
req.Host = "my-host"
523+
524+
resp, err = http.DefaultClient.Do(req)
525+
Expect(err).ShouldNot(HaveOccurred())
526+
})
527+
528+
It("should reject an invalid host", func() {
529+
req.Host = "not-my-host"
530+
531+
failures := InterceptGomegaFailures(func() {
532+
http.DefaultClient.Do(req)
533+
})
534+
Expect(failures).Should(HaveLen(1))
535+
})
536+
})
537+
538+
When("passed a string for host", func() {
539+
BeforeEach(func() {
540+
s.AppendHandlers(CombineHandlers(
541+
VerifyRequest("GET", "/host"),
542+
VerifyHost("my-host"),
543+
))
544+
})
545+
546+
It("should verify the host", func() {
547+
req.Host = "my-host"
548+
549+
resp, err = http.DefaultClient.Do(req)
550+
Expect(err).ShouldNot(HaveOccurred())
551+
})
552+
553+
It("should reject an invalid host", func() {
554+
req.Host = "not-my-host"
555+
556+
failures := InterceptGomegaFailures(func() {
557+
http.DefaultClient.Do(req)
558+
})
559+
Expect(failures).Should(HaveLen(1))
560+
})
561+
})
562+
563+
})
564+
502565
Describe("VerifyBody", func() {
503566
BeforeEach(func() {
504567
s.AppendHandlers(CombineHandlers(

0 commit comments

Comments
 (0)
Please sign in to comment.