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 with T #376

Merged
merged 1 commit into from
Apr 29, 2020
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
167 changes: 123 additions & 44 deletions ghttp/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,21 @@ import (
"strings"

"github.com/golang/protobuf/proto"
"github.com/onsi/gomega"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/types"
)

type GHTTPWithGomega struct {
gomega Gomega
}

func NewGHTTPWithGomega(gomega Gomega) *GHTTPWithGomega {
return &GHTTPWithGomega{
gomega: gomega,
}
}

//CombineHandler takes variadic list of handlers and produces one handler
//that calls each handler in order.
func CombineHandlers(handlers ...http.HandlerFunc) http.HandlerFunc {
Expand All @@ -32,51 +43,51 @@ func CombineHandlers(handlers ...http.HandlerFunc) http.HandlerFunc {
//
//For path, you may pass in a string, in which case strict equality will be applied
//Alternatively you can pass in a matcher (ContainSubstring("/foo") and MatchRegexp("/foo/[a-f0-9]+") for example)
func VerifyRequest(method string, path interface{}, rawQuery ...string) http.HandlerFunc {
func (g GHTTPWithGomega) VerifyRequest(method string, path interface{}, rawQuery ...string) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
Expect(req.Method).Should(Equal(method), "Method mismatch")
g.gomega.Expect(req.Method).Should(Equal(method), "Method mismatch")
switch p := path.(type) {
case types.GomegaMatcher:
Expect(req.URL.Path).Should(p, "Path mismatch")
g.gomega.Expect(req.URL.Path).Should(p, "Path mismatch")
default:
Expect(req.URL.Path).Should(Equal(path), "Path mismatch")
g.gomega.Expect(req.URL.Path).Should(Equal(path), "Path mismatch")
}
if len(rawQuery) > 0 {
values, err := url.ParseQuery(rawQuery[0])
Expect(err).ShouldNot(HaveOccurred(), "Expected RawQuery is malformed")
g.gomega.Expect(err).ShouldNot(HaveOccurred(), "Expected RawQuery is malformed")

Expect(req.URL.Query()).Should(Equal(values), "RawQuery mismatch")
g.gomega.Expect(req.URL.Query()).Should(Equal(values), "RawQuery mismatch")
}
}
}

//VerifyContentType returns a handler that verifies that a request has a Content-Type header set to the
//specified value
func VerifyContentType(contentType string) http.HandlerFunc {
func (g GHTTPWithGomega) VerifyContentType(contentType string) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
Expect(req.Header.Get("Content-Type")).Should(Equal(contentType))
g.gomega.Expect(req.Header.Get("Content-Type")).Should(Equal(contentType))
}
}

//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 {
func (g GHTTPWithGomega) 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))
g.gomega.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 {
func (g GHTTPWithGomega) VerifyBasicAuth(username string, password string) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
auth := req.Header.Get("Authorization")
Expect(auth).ShouldNot(Equal(""), "Authorization header must be specified")
g.gomega.Expect(auth).ShouldNot(Equal(""), "Authorization header must be specified")

decoded, err := base64.StdEncoding.DecodeString(auth[6:])
Expect(err).ShouldNot(HaveOccurred())
g.gomega.Expect(err).ShouldNot(HaveOccurred())

Expect(string(decoded)).Should(Equal(fmt.Sprintf("%s:%s", username, password)), "Authorization mismatch")
g.gomega.Expect(string(decoded)).Should(Equal(fmt.Sprintf("%s:%s", username, password)), "Authorization mismatch")
}
}

Expand All @@ -85,31 +96,31 @@ func VerifyBasicAuth(username string, password string) http.HandlerFunc {
//
//The request must contain *all* the passed in headers, but it is allowed to have additional headers
//beyond the passed in set.
func VerifyHeader(header http.Header) http.HandlerFunc {
func (g GHTTPWithGomega) VerifyHeader(header http.Header) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
for key, values := range header {
key = http.CanonicalHeaderKey(key)
Expect(req.Header[key]).Should(Equal(values), "Header mismatch for key: %s", key)
g.gomega.Expect(req.Header[key]).Should(Equal(values), "Header mismatch for key: %s", key)
}
}
}

//VerifyHeaderKV returns a handler that verifies the request contains a header matching the passed in key and values
//(recall that a `http.Header` is a mapping from string (key) to []string (values))
//It is a convenience wrapper around `VerifyHeader` that allows you to avoid having to create an `http.Header` object.
func VerifyHeaderKV(key string, values ...string) http.HandlerFunc {
func (g GHTTPWithGomega) VerifyHeaderKV(key string, values ...string) http.HandlerFunc {
return VerifyHeader(http.Header{key: values})
}

//VerifyBody returns a handler that verifies that the body of the request matches the passed in byte array.
//It does this using Equal().
func VerifyBody(expectedBody []byte) http.HandlerFunc {
func (g GHTTPWithGomega) VerifyBody(expectedBody []byte) http.HandlerFunc {
return CombineHandlers(
func(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
req.Body.Close()
Expect(err).ShouldNot(HaveOccurred())
Expect(body).Should(Equal(expectedBody), "Body Mismatch")
g.gomega.Expect(err).ShouldNot(HaveOccurred())
g.gomega.Expect(body).Should(Equal(expectedBody), "Body Mismatch")
},
)
}
Expand All @@ -118,24 +129,24 @@ func VerifyBody(expectedBody []byte) http.HandlerFunc {
//matching the passed in JSON string. It does this using Gomega's MatchJSON method
//
//VerifyJSON also verifies that the request's content type is application/json
func VerifyJSON(expectedJSON string) http.HandlerFunc {
func (g GHTTPWithGomega) VerifyJSON(expectedJSON string) http.HandlerFunc {
return CombineHandlers(
VerifyMimeType("application/json"),
func(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
req.Body.Close()
Expect(err).ShouldNot(HaveOccurred())
Expect(body).Should(MatchJSON(expectedJSON), "JSON Mismatch")
g.gomega.Expect(err).ShouldNot(HaveOccurred())
g.gomega.Expect(body).Should(MatchJSON(expectedJSON), "JSON Mismatch")
},
)
}

//VerifyJSONRepresenting is similar to VerifyJSON. Instead of taking a JSON string, however, it
//takes an arbitrary JSON-encodable object and verifies that the requests's body is a JSON representation
//that matches the object
func VerifyJSONRepresenting(object interface{}) http.HandlerFunc {
func (g GHTTPWithGomega) VerifyJSONRepresenting(object interface{}) http.HandlerFunc {
data, err := json.Marshal(object)
Expect(err).ShouldNot(HaveOccurred())
g.gomega.Expect(err).ShouldNot(HaveOccurred())
return CombineHandlers(
VerifyContentType("application/json"),
VerifyJSON(string(data)),
Expand All @@ -146,45 +157,45 @@ func VerifyJSONRepresenting(object interface{}) http.HandlerFunc {
//
//The request must contain *all* of the specified values, but it is allowed to have additional
//form values beyond the passed in set.
func VerifyForm(values url.Values) http.HandlerFunc {
func (g GHTTPWithGomega) VerifyForm(values url.Values) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
Expect(err).ShouldNot(HaveOccurred())
g.gomega.Expect(err).ShouldNot(HaveOccurred())
for key, vals := range values {
Expect(r.Form[key]).Should(Equal(vals), "Form mismatch for key: %s", key)
g.gomega.Expect(r.Form[key]).Should(Equal(vals), "Form mismatch for key: %s", key)
}
}
}

//VerifyFormKV returns a handler that verifies a request contains a form key with the specified values.
//
//It is a convenience wrapper around `VerifyForm` that lets you avoid having to create a `url.Values` object.
func VerifyFormKV(key string, values ...string) http.HandlerFunc {
func (g GHTTPWithGomega) VerifyFormKV(key string, values ...string) http.HandlerFunc {
return VerifyForm(url.Values{key: values})
}

//VerifyProtoRepresenting returns a handler that verifies that the body of the request is a valid protobuf
//representation of the passed message.
//
//VerifyProtoRepresenting also verifies that the request's content type is application/x-protobuf
func VerifyProtoRepresenting(expected proto.Message) http.HandlerFunc {
func (g GHTTPWithGomega) VerifyProtoRepresenting(expected proto.Message) http.HandlerFunc {
return CombineHandlers(
VerifyContentType("application/x-protobuf"),
func(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
Expect(err).ShouldNot(HaveOccurred())
g.gomega.Expect(err).ShouldNot(HaveOccurred())
req.Body.Close()

expectedType := reflect.TypeOf(expected)
actualValuePtr := reflect.New(expectedType.Elem())

actual, ok := actualValuePtr.Interface().(proto.Message)
Expect(ok).Should(BeTrue(), "Message value is not a proto.Message")
g.gomega.Expect(ok).Should(BeTrue(), "Message value is not a proto.Message")

err = proto.Unmarshal(body, actual)
Expect(err).ShouldNot(HaveOccurred(), "Failed to unmarshal protobuf")
g.gomega.Expect(err).ShouldNot(HaveOccurred(), "Failed to unmarshal protobuf")

Expect(actual).Should(Equal(expected), "ProtoBuf Mismatch")
g.gomega.Expect(actual).Should(Equal(expected), "ProtoBuf Mismatch")
},
)
}
Expand All @@ -202,7 +213,7 @@ Body may be a string or []byte

Also, RespondWith can be given an optional http.Header. The headers defined therein will be added to the response headers.
*/
func RespondWith(statusCode int, body interface{}, optionalHeader ...http.Header) http.HandlerFunc {
func (g GHTTPWithGomega) RespondWith(statusCode int, body interface{}, optionalHeader ...http.Header) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
if len(optionalHeader) == 1 {
copyHeader(optionalHeader[0], w.Header())
Expand All @@ -214,7 +225,7 @@ func RespondWith(statusCode int, body interface{}, optionalHeader ...http.Header
case []byte:
w.Write(x)
default:
Expect(body).Should(BeNil(), "Invalid type for body. Should be string or []byte.")
g.gomega.Expect(body).Should(BeNil(), "Invalid type for body. Should be string or []byte.")
}
}
}
Expand All @@ -228,7 +239,7 @@ to share the same setup but specify different status codes and bodies.
Also, RespondWithPtr can be given an optional http.Header. The headers defined therein will be added to the response headers.
Since the http.Header can be mutated after the fact you don't need to pass in a pointer.
*/
func RespondWithPtr(statusCode *int, body interface{}, optionalHeader ...http.Header) http.HandlerFunc {
func (g GHTTPWithGomega) RespondWithPtr(statusCode *int, body interface{}, optionalHeader ...http.Header) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
if len(optionalHeader) == 1 {
copyHeader(optionalHeader[0], w.Header())
Expand All @@ -241,7 +252,7 @@ func RespondWithPtr(statusCode *int, body interface{}, optionalHeader ...http.He
case *[]byte:
w.Write(*x)
default:
Expect(body).Should(BeNil(), "Invalid type for body. Should be string or []byte.")
g.gomega.Expect(body).Should(BeNil(), "Invalid type for body. Should be string or []byte.")
}
}
}
Expand All @@ -253,9 +264,9 @@ containing the JSON-encoding of the passed in object

Also, RespondWithJSONEncoded can be given an optional http.Header. The headers defined therein will be added to the response headers.
*/
func RespondWithJSONEncoded(statusCode int, object interface{}, optionalHeader ...http.Header) http.HandlerFunc {
func (g GHTTPWithGomega) RespondWithJSONEncoded(statusCode int, object interface{}, optionalHeader ...http.Header) http.HandlerFunc {
data, err := json.Marshal(object)
Expect(err).ShouldNot(HaveOccurred())
g.gomega.Expect(err).ShouldNot(HaveOccurred())

var headers http.Header
if len(optionalHeader) == 1 {
Expand All @@ -279,10 +290,10 @@ objects.
Also, RespondWithJSONEncodedPtr can be given an optional http.Header. The headers defined therein will be added to the response headers.
Since the http.Header can be mutated after the fact you don't need to pass in a pointer.
*/
func RespondWithJSONEncodedPtr(statusCode *int, object interface{}, optionalHeader ...http.Header) http.HandlerFunc {
func (g GHTTPWithGomega) RespondWithJSONEncodedPtr(statusCode *int, object interface{}, optionalHeader ...http.Header) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
data, err := json.Marshal(object)
Expect(err).ShouldNot(HaveOccurred())
g.gomega.Expect(err).ShouldNot(HaveOccurred())
var headers http.Header
if len(optionalHeader) == 1 {
headers = optionalHeader[0]
Expand All @@ -302,10 +313,10 @@ func RespondWithJSONEncodedPtr(statusCode *int, object interface{}, optionalHead
//containing the protobuf serialization of the provided message.
//
//Also, RespondWithProto can be given an optional http.Header. The headers defined therein will be added to the response headers.
func RespondWithProto(statusCode int, message proto.Message, optionalHeader ...http.Header) http.HandlerFunc {
func (g GHTTPWithGomega) RespondWithProto(statusCode int, message proto.Message, optionalHeader ...http.Header) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
data, err := proto.Marshal(message)
Expect(err).ShouldNot(HaveOccurred())
g.gomega.Expect(err).ShouldNot(HaveOccurred())

var headers http.Header
if len(optionalHeader) == 1 {
Expand All @@ -322,3 +333,71 @@ func RespondWithProto(statusCode int, message proto.Message, optionalHeader ...h
w.Write(data)
}
}

func VerifyRequest(method string, path interface{}, rawQuery ...string) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).VerifyRequest(method, path, rawQuery...)
}

func VerifyContentType(contentType string) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).VerifyContentType(contentType)
}

func VerifyMimeType(mimeType string) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).VerifyMimeType(mimeType)
}

func VerifyBasicAuth(username string, password string) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).VerifyBasicAuth(username, password)
}

func VerifyHeader(header http.Header) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).VerifyHeader(header)
}

func VerifyHeaderKV(key string, values ...string) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).VerifyHeaderKV(key, values...)
}

func VerifyBody(expectedBody []byte) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).VerifyBody(expectedBody)
}

func VerifyJSON(expectedJSON string) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).VerifyJSON(expectedJSON)
}

func VerifyJSONRepresenting(object interface{}) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).VerifyJSONRepresenting(object)
}

func VerifyForm(values url.Values) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).VerifyForm(values)
}

func VerifyFormKV(key string, values ...string) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).VerifyFormKV(key, values...)
}

func VerifyProtoRepresenting(expected proto.Message) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).VerifyProtoRepresenting(expected)
}

func RespondWith(statusCode int, body interface{}, optionalHeader ...http.Header) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).RespondWith(statusCode, body, optionalHeader...)
}

func RespondWithPtr(statusCode *int, body interface{}, optionalHeader ...http.Header) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).RespondWithPtr(statusCode, body, optionalHeader...)
}

func RespondWithJSONEncoded(statusCode int, object interface{}, optionalHeader ...http.Header) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).RespondWithJSONEncoded(statusCode, object, optionalHeader...)
}

func RespondWithJSONEncodedPtr(statusCode *int, object interface{}, optionalHeader ...http.Header) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).RespondWithJSONEncodedPtr(statusCode, object, optionalHeader...)
}

func RespondWithProto(statusCode int, message proto.Message, optionalHeader ...http.Header) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).RespondWithProto(statusCode, message, optionalHeader...)
}
31 changes: 30 additions & 1 deletion gomega_dsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func Consistently(actual interface{}, intervals ...interface{}) AsyncAssertion {
return ConsistentlyWithOffset(0, actual, intervals...)
}

// ConsistentlyWithOffset operates like Consistnetly but takes an additional
// ConsistentlyWithOffset operates like Consistently but takes an additional
// initial argument to indicate an offset in the call stack. This is useful when building helper
// functions that contain matchers. To learn more, read about `ExpectWithOffset`.
func ConsistentlyWithOffset(offset int, actual interface{}, intervals ...interface{}) AsyncAssertion {
Expand Down Expand Up @@ -432,3 +432,32 @@ func toDuration(input interface{}) time.Duration {

panic(fmt.Sprintf("%v is not a valid interval. Must be time.Duration, parsable duration string or a number.", input))
}

// Gomega describes the essential Gomega DSL. This interface allows libraries
// to abstract between the standard package-level function implementations
// and alternatives like *WithT.
type Gomega interface {
Expect(actual interface{}, extra ...interface{}) Assertion
Eventually(actual interface{}, intervals ...interface{}) AsyncAssertion
Consistently(actual interface{}, intervals ...interface{}) AsyncAssertion
}

type globalFailHandlerGomega struct{}

// DefaultGomega supplies the standard package-level implementation
var Default Gomega = globalFailHandlerGomega{}

// Expect is used to make assertions. See documentation for Expect.
func (globalFailHandlerGomega) Expect(actual interface{}, extra ...interface{}) Assertion {
return Expect(actual, extra...)
}

// Eventually is used to make asynchronous assertions. See documentation for Eventually.
func (globalFailHandlerGomega) Eventually(actual interface{}, extra ...interface{}) AsyncAssertion {
return Eventually(actual, extra...)
}

// Consistently is used to make asynchronous assertions. See documentation for Consistently.
func (globalFailHandlerGomega) Consistently(actual interface{}, extra ...interface{}) AsyncAssertion {
return Consistently(actual, extra...)
}