2014-11-20 02:52:09 +00:00
|
|
|
package testutil
|
2014-11-14 02:42:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2015-05-07 20:16:52 +00:00
|
|
|
"net/url"
|
2014-11-20 02:06:54 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2014-11-14 02:42:49 +00:00
|
|
|
)
|
|
|
|
|
2014-11-20 02:06:54 +00:00
|
|
|
// RequestResponseMap is an ordered mapping from Requests to Responses
|
2014-11-14 02:42:49 +00:00
|
|
|
type RequestResponseMap []RequestResponseMapping
|
|
|
|
|
2014-11-20 02:06:54 +00:00
|
|
|
// RequestResponseMapping defines a Response to be sent in response to a given
|
|
|
|
// Request
|
2014-11-14 02:42:49 +00:00
|
|
|
type RequestResponseMapping struct {
|
2014-11-20 02:06:54 +00:00
|
|
|
Request Request
|
|
|
|
Response Response
|
2014-11-14 02:42:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(bbland): add support for request headers
|
|
|
|
|
|
|
|
// Request is a simplified http.Request object
|
|
|
|
type Request struct {
|
|
|
|
// Method is the http method of the request, for example GET
|
|
|
|
Method string
|
|
|
|
|
|
|
|
// Route is the http route of this request
|
|
|
|
Route string
|
|
|
|
|
2014-11-20 02:06:54 +00:00
|
|
|
// QueryParams are the query parameters of this request
|
|
|
|
QueryParams map[string][]string
|
|
|
|
|
2014-11-14 02:42:49 +00:00
|
|
|
// Body is the byte contents of the http request
|
|
|
|
Body []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Request) String() string {
|
2014-11-20 02:06:54 +00:00
|
|
|
queryString := ""
|
|
|
|
if len(r.QueryParams) > 0 {
|
|
|
|
keys := make([]string, 0, len(r.QueryParams))
|
2015-05-07 20:16:52 +00:00
|
|
|
queryParts := make([]string, 0, len(r.QueryParams))
|
2014-11-20 02:06:54 +00:00
|
|
|
for k := range r.QueryParams {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
for _, k := range keys {
|
2015-05-07 20:16:52 +00:00
|
|
|
for _, val := range r.QueryParams[k] {
|
|
|
|
queryParts = append(queryParts, fmt.Sprintf("%s=%s", k, url.QueryEscape(val)))
|
|
|
|
}
|
2014-11-20 02:06:54 +00:00
|
|
|
}
|
2015-05-07 20:16:52 +00:00
|
|
|
queryString = "?" + strings.Join(queryParts, "&")
|
2014-11-20 02:06:54 +00:00
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s %s%s\n%s", r.Method, r.Route, queryString, r.Body)
|
2014-11-14 02:42:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Response is a simplified http.Response object
|
|
|
|
type Response struct {
|
|
|
|
// Statuscode is the http status code of the Response
|
|
|
|
StatusCode int
|
|
|
|
|
|
|
|
// Headers are the http headers of this Response
|
|
|
|
Headers http.Header
|
|
|
|
|
|
|
|
// Body is the response body
|
|
|
|
Body []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// testHandler is an http.Handler with a defined mapping from Request to an
|
|
|
|
// ordered list of Response objects
|
|
|
|
type testHandler struct {
|
|
|
|
responseMap map[string][]Response
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHandler returns a new test handler that responds to defined requests
|
|
|
|
// with specified responses
|
|
|
|
// Each time a Request is received, the next Response is returned in the
|
|
|
|
// mapping, until no Responses are defined, at which point a 404 is sent back
|
|
|
|
func NewHandler(requestResponseMap RequestResponseMap) http.Handler {
|
|
|
|
responseMap := make(map[string][]Response)
|
|
|
|
for _, mapping := range requestResponseMap {
|
2014-11-20 02:06:54 +00:00
|
|
|
responses, ok := responseMap[mapping.Request.String()]
|
|
|
|
if ok {
|
|
|
|
responseMap[mapping.Request.String()] = append(responses, mapping.Response)
|
|
|
|
} else {
|
|
|
|
responseMap[mapping.Request.String()] = []Response{mapping.Response}
|
|
|
|
}
|
2014-11-14 02:42:49 +00:00
|
|
|
}
|
|
|
|
return &testHandler{responseMap: responseMap}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
requestBody, _ := ioutil.ReadAll(r.Body)
|
|
|
|
request := Request{
|
2014-11-20 02:06:54 +00:00
|
|
|
Method: r.Method,
|
|
|
|
Route: r.URL.Path,
|
|
|
|
QueryParams: r.URL.Query(),
|
|
|
|
Body: requestBody,
|
2014-11-14 02:42:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
responses, ok := app.responseMap[request.String()]
|
|
|
|
|
|
|
|
if !ok || len(responses) == 0 {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response := responses[0]
|
|
|
|
app.responseMap[request.String()] = responses[1:]
|
|
|
|
|
|
|
|
responseHeader := w.Header()
|
|
|
|
for k, v := range response.Headers {
|
|
|
|
responseHeader[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(response.StatusCode)
|
|
|
|
|
|
|
|
io.Copy(w, bytes.NewReader(response.Body))
|
|
|
|
}
|