1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
|
// untested sections: 3
package ghttp
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/url"
"reflect"
"strings"
"github.com/onsi/gomega"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/internal/gutil"
"github.com/onsi/gomega/types"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/protoadapt"
"google.golang.org/protobuf/runtime/protoiface"
)
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 {
return func(w http.ResponseWriter, req *http.Request) {
for _, handler := range handlers {
handler(w, req)
}
}
}
// VerifyRequest returns a handler that verifies that a request uses the specified method to connect to the specified path
// You may also pass in an optional rawQuery string which is tested against the request's `req.URL.RawQuery`
//
// 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 (g GHTTPWithGomega) VerifyRequest(method string, path interface{}, rawQuery ...string) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
g.gomega.Expect(req.Method).Should(Equal(method), "Method mismatch")
switch p := path.(type) {
case types.GomegaMatcher:
g.gomega.Expect(req.URL.Path).Should(p, "Path mismatch")
default:
g.gomega.Expect(req.URL.Path).Should(Equal(path), "Path mismatch")
}
if len(rawQuery) > 0 {
values, err := url.ParseQuery(rawQuery[0])
g.gomega.Expect(err).ShouldNot(HaveOccurred(), "Expected RawQuery is malformed")
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 (g GHTTPWithGomega) VerifyContentType(contentType string) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
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 (g GHTTPWithGomega) VerifyMimeType(mimeType string) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
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 (g GHTTPWithGomega) VerifyBasicAuth(username string, password string) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
auth := req.Header.Get("Authorization")
g.gomega.Expect(auth).ShouldNot(Equal(""), "Authorization header must be specified")
decoded, err := base64.StdEncoding.DecodeString(auth[6:])
g.gomega.Expect(err).ShouldNot(HaveOccurred())
g.gomega.Expect(string(decoded)).Should(Equal(fmt.Sprintf("%s:%s", username, password)), "Authorization mismatch")
}
}
// VerifyHeader returns a handler that verifies the request contains the passed in headers.
// The passed in header keys are first canonicalized via http.CanonicalHeaderKey.
//
// The request must contain *all* the passed in headers, but it is allowed to have additional headers
// beyond the passed in set.
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)
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 (g GHTTPWithGomega) VerifyHeaderKV(key string, values ...string) http.HandlerFunc {
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 {
return CombineHandlers(
func(w http.ResponseWriter, req *http.Request) {
body, err := gutil.ReadAll(req.Body)
req.Body.Close()
g.gomega.Expect(err).ShouldNot(HaveOccurred())
g.gomega.Expect(body).Should(Equal(expectedBody), "Body Mismatch")
},
)
}
// VerifyJSON returns a handler that verifies that the body of the request is a valid JSON representation
// 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 (g GHTTPWithGomega) VerifyJSON(expectedJSON string) http.HandlerFunc {
return CombineHandlers(
g.VerifyMimeType("application/json"),
func(w http.ResponseWriter, req *http.Request) {
body, err := gutil.ReadAll(req.Body)
req.Body.Close()
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 (g GHTTPWithGomega) VerifyJSONRepresenting(object interface{}) http.HandlerFunc {
data, err := json.Marshal(object)
g.gomega.Expect(err).ShouldNot(HaveOccurred())
return CombineHandlers(
g.VerifyMimeType("application/json"),
g.VerifyJSON(string(data)),
)
}
// VerifyForm returns a handler that verifies a request contains the specified form values.
//
// The request must contain *all* of the specified values, but it is allowed to have additional
// form values beyond the passed in set.
func (g GHTTPWithGomega) VerifyForm(values url.Values) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
g.gomega.Expect(err).ShouldNot(HaveOccurred())
for key, vals := range values {
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 (g GHTTPWithGomega) VerifyFormKV(key string, values ...string) http.HandlerFunc {
return g.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 (g GHTTPWithGomega) VerifyProtoRepresenting(expected protoiface.MessageV1) http.HandlerFunc {
return CombineHandlers(
g.VerifyContentType("application/x-protobuf"),
func(w http.ResponseWriter, req *http.Request) {
body, err := gutil.ReadAll(req.Body)
g.gomega.Expect(err).ShouldNot(HaveOccurred())
req.Body.Close()
expectedType := reflect.TypeOf(expected)
actualValuePtr := reflect.New(expectedType.Elem())
actual, ok := actualValuePtr.Interface().(protoiface.MessageV1)
g.gomega.Expect(ok).Should(BeTrueBecause("Message value should be a protoiface.MessageV1"))
err = proto.Unmarshal(body, protoadapt.MessageV2Of(actual))
g.gomega.Expect(err).ShouldNot(HaveOccurred(), "Failed to unmarshal protobuf")
g.gomega.Expect(proto.Equal(protoadapt.MessageV2Of(expected), protoadapt.MessageV2Of(actual))).
Should(BeTrue(), "ProtoBuf Mismatch")
},
)
}
func copyHeader(src http.Header, dst http.Header) {
for key, value := range src {
dst[key] = value
}
}
/*
RespondWith returns a handler that responds to a request with the specified status code and body
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 (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())
}
w.WriteHeader(statusCode)
switch x := body.(type) {
case string:
w.Write([]byte(x))
case []byte:
w.Write(x)
default:
g.gomega.Expect(body).Should(BeNil(), "Invalid type for body. Should be string or []byte.")
}
}
}
/*
RespondWithPtr returns a handler that responds to a request with the specified status code and body
Unlike RespondWith, you pass RepondWithPtr a pointer to the status code and body allowing different tests
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 (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())
}
w.WriteHeader(*statusCode)
if body != nil {
switch x := (body).(type) {
case *string:
w.Write([]byte(*x))
case *[]byte:
w.Write(*x)
default:
g.gomega.Expect(body).Should(BeNil(), "Invalid type for body. Should be string or []byte.")
}
}
}
}
/*
RespondWithJSONEncoded returns a handler that responds to a request with the specified status code and a body
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 (g GHTTPWithGomega) RespondWithJSONEncoded(statusCode int, object interface{}, optionalHeader ...http.Header) http.HandlerFunc {
data, err := json.Marshal(object)
g.gomega.Expect(err).ShouldNot(HaveOccurred())
var headers http.Header
if len(optionalHeader) == 1 {
headers = optionalHeader[0]
} else {
headers = make(http.Header)
}
if _, found := headers["Content-Type"]; !found {
headers["Content-Type"] = []string{"application/json"}
}
return RespondWith(statusCode, string(data), headers)
}
/*
RespondWithJSONEncodedPtr behaves like RespondWithJSONEncoded but takes a pointer
to a status code and object.
This allows different tests to share the same setup but specify different status codes and JSON-encoded
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 (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)
g.gomega.Expect(err).ShouldNot(HaveOccurred())
var headers http.Header
if len(optionalHeader) == 1 {
headers = optionalHeader[0]
} else {
headers = make(http.Header)
}
if _, found := headers["Content-Type"]; !found {
headers["Content-Type"] = []string{"application/json"}
}
copyHeader(headers, w.Header())
w.WriteHeader(*statusCode)
w.Write(data)
}
}
// RespondWithProto returns a handler that responds to a request with the specified status code and a body
// 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 (g GHTTPWithGomega) RespondWithProto(statusCode int, message protoadapt.MessageV1, optionalHeader ...http.Header) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
data, err := proto.Marshal(protoadapt.MessageV2Of(message))
g.gomega.Expect(err).ShouldNot(HaveOccurred())
var headers http.Header
if len(optionalHeader) == 1 {
headers = optionalHeader[0]
} else {
headers = make(http.Header)
}
if _, found := headers["Content-Type"]; !found {
headers["Content-Type"] = []string{"application/x-protobuf"}
}
copyHeader(headers, w.Header())
w.WriteHeader(statusCode)
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 VerifyHost(host interface{}) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).VerifyHost(host)
}
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 protoiface.MessageV1) 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 protoadapt.MessageV1, optionalHeader ...http.Header) http.HandlerFunc {
return NewGHTTPWithGomega(gomega.Default).RespondWithProto(statusCode, message, optionalHeader...)
}
|