Skip to content

Commit

Permalink
Fixed matcher for content-type (#305)
Browse files Browse the repository at this point in the history
* Fixed matcher for content-type

* added VerifyMimeType function
  • Loading branch information
kucherenkovova authored and williammartin committed Nov 2, 2018
1 parent 7615b94 commit 69d9b43
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
11 changes: 10 additions & 1 deletion ghttp/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/url"
"reflect"
"strings"

"github.com/golang/protobuf/proto"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -55,6 +56,14 @@ func VerifyContentType(contentType string) http.HandlerFunc {
}
}

//VerifyMimeType returns a handler that verifies that a request has a specified mime type set
//in Content-Type header
func VerifyMimeType(mimeType string) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
Expect(strings.Split(req.Header.Get("Content-Type"), ";")[0]).Should(Equal(mimeType))
}
}

//VerifyBasicAuth returns a handler that verifies the request contains a BasicAuth Authorization header
//matching the passed in username and password
func VerifyBasicAuth(username string, password string) http.HandlerFunc {
Expand Down Expand Up @@ -109,7 +118,7 @@ func VerifyBody(expectedBody []byte) http.HandlerFunc {
//VerifyJSON also verifies that the request's content type is application/json
func VerifyJSON(expectedJSON string) http.HandlerFunc {
return CombineHandlers(
VerifyContentType("application/json"),
VerifyMimeType("application/json"),
func(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
req.Body.Close()
Expand Down
37 changes: 37 additions & 0 deletions ghttp/test_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,17 @@ var _ = Describe("TestServer", func() {
})
Expect(failures).Should(HaveLen(1))
})

It("should verify the content type", func() {
req, err := http.NewRequest("GET", s.URL()+"/foo", nil)
Expect(err).ShouldNot(HaveOccurred())
req.Header.Set("Content-Type", "application/octet-stream; charset=utf-8")

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

Describe("Verify BasicAuth", func() {
Expand Down Expand Up @@ -509,6 +520,26 @@ var _ = Describe("TestServer", func() {
})
})

Describe("VerifyMimeType", func() {
BeforeEach(func() {
s.AppendHandlers(CombineHandlers(
VerifyMimeType("application/json"),
))
})

It("should verify the mime type in content-type header", func() {
resp, err = http.Post(s.URL()+"/foo", "application/json; charset=utf-8", bytes.NewReader([]byte(`{}`)))
Expect(err).ShouldNot(HaveOccurred())
})

It("should verify the mime type in content-type header", func() {
failures := InterceptGomegaFailures(func() {
http.Post(s.URL()+"/foo", "text/plain", bytes.NewReader([]byte(`{}`)))
})
Expect(failures).Should(HaveLen(1))
})
})

Describe("VerifyJSON", func() {
BeforeEach(func() {
s.AppendHandlers(CombineHandlers(
Expand All @@ -535,6 +566,12 @@ var _ = Describe("TestServer", func() {
})
Expect(failures).Should(HaveLen(1))
})


It("should verify the json body and the content type", func() {
resp, err = http.Post(s.URL()+"/foo", "application/json; charset=utf-8", bytes.NewReader([]byte(`{"b":2, "a":3}`)))
Expect(err).ShouldNot(HaveOccurred())
})
})

Describe("VerifyJSONRepresenting", func() {
Expand Down

0 comments on commit 69d9b43

Please sign in to comment.