| 1 |
package test |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"fmt" |
| 6 |
"io/ioutil" |
| 7 |
"mime" |
| 8 |
"net/http" |
| 9 |
"net/http/httptest" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
// MakeSimpleRequest returns a http.Request. The returned request object can be |
| 15 |
// further prepared by adding headers and query string parmaters, for instance. |
| 16 |
func MakeSimpleRequest(method string, urlStr string, payload interface{}) *http.Request { |
| 17 |
var s string |
| 18 |
|
| 19 |
if payload != nil { |
| 20 |
b, err := json.Marshal(payload) |
| 21 |
if err != nil { |
| 22 |
panic(err) |
| 23 |
} |
| 24 |
s = fmt.Sprintf("%s", b) |
| 25 |
} |
| 26 |
|
| 27 |
r, err := http.NewRequest(method, urlStr, strings.NewReader(s)) |
| 28 |
if err != nil { |
| 29 |
panic(err) |
| 30 |
} |
| 31 |
r.Header.Set("Accept-Encoding", "gzip") |
| 32 |
if payload != nil { |
| 33 |
r.Header.Set("Content-Type", "application/json") |
| 34 |
} |
| 35 |
|
| 36 |
return r |
| 37 |
} |
| 38 |
|
| 39 |
// CodeIs compares the rescorded status code |
| 40 |
func CodeIs(t *testing.T, r *httptest.ResponseRecorder, expectedCode int) { |
| 41 |
if r.Code != expectedCode { |
| 42 |
t.Errorf("Code %d expected, got: %d", expectedCode, r.Code) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
// HeaderIs tests the first value for the given headerKey |
| 47 |
func HeaderIs(t *testing.T, r *httptest.ResponseRecorder, headerKey, expectedValue string) { |
| 48 |
value := r.HeaderMap.Get(headerKey) |
| 49 |
if value != expectedValue { |
| 50 |
t.Errorf( |
| 51 |
"%s: %s expected, got: %s", |
| 52 |
headerKey, |
| 53 |
expectedValue, |
| 54 |
value, |
| 55 |
) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
func ContentTypeIsJson(t *testing.T, r *httptest.ResponseRecorder) { |
| 60 |
|
| 61 |
mediaType, params, _ := mime.ParseMediaType(r.HeaderMap.Get("Content-Type")) |
| 62 |
charset := params["charset"] |
| 63 |
|
| 64 |
if mediaType != "application/json" { |
| 65 |
t.Errorf( |
| 66 |
"Content-Type media type: application/json expected, got: %s", |
| 67 |
mediaType, |
| 68 |
) |
| 69 |
} |
| 70 |
|
| 71 |
if charset != "" && strings.ToUpper(charset) != "UTF-8" { |
| 72 |
t.Errorf( |
| 73 |
"Content-Type charset: must be empty or UTF-8, got: %s", |
| 74 |
charset, |
| 75 |
) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
func ContentEncodingIsGzip(t *testing.T, r *httptest.ResponseRecorder) { |
| 80 |
HeaderIs(t, r, "Content-Encoding", "gzip") |
| 81 |
} |
| 82 |
|
| 83 |
func BodyIs(t *testing.T, r *httptest.ResponseRecorder, expectedBody string) { |
| 84 |
body := r.Body.String() |
| 85 |
if body != expectedBody { |
| 86 |
t.Errorf("Body '%s' expected, got: '%s'", expectedBody, body) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
func DecodeJsonPayload(r *httptest.ResponseRecorder, v interface{}) error { |
| 91 |
content, err := ioutil.ReadAll(r.Body) |
| 92 |
if err != nil { |
| 93 |
return err |
| 94 |
} |
| 95 |
err = json.Unmarshal(content, v) |
| 96 |
if err != nil { |
| 97 |
return err |
| 98 |
} |
| 99 |
return nil |
| 100 |
} |
| 101 |
|
| 102 |
type Recorded struct { |
| 103 |
T *testing.T |
| 104 |
Recorder *httptest.ResponseRecorder |
| 105 |
} |
| 106 |
|
| 107 |
// RunRequest runs a HTTP request through the given handler |
| 108 |
func RunRequest(t *testing.T, handler http.Handler, request *http.Request) *Recorded { |
| 109 |
recorder := httptest.NewRecorder() |
| 110 |
handler.ServeHTTP(recorder, request) |
| 111 |
return &Recorded{t, recorder} |
| 112 |
} |
| 113 |
|
| 114 |
func (rd *Recorded) CodeIs(expectedCode int) { |
| 115 |
CodeIs(rd.T, rd.Recorder, expectedCode) |
| 116 |
} |
| 117 |
|
| 118 |
func (rd *Recorded) HeaderIs(headerKey, expectedValue string) { |
| 119 |
HeaderIs(rd.T, rd.Recorder, headerKey, expectedValue) |
| 120 |
} |
| 121 |
|
| 122 |
func (rd *Recorded) ContentTypeIsJson() { |
| 123 |
ContentTypeIsJson(rd.T, rd.Recorder) |
| 124 |
} |
| 125 |
|
| 126 |
func (rd *Recorded) ContentEncodingIsGzip() { |
| 127 |
rd.HeaderIs("Content-Encoding", "gzip") |
| 128 |
} |
| 129 |
|
| 130 |
func (rd *Recorded) BodyIs(expectedBody string) { |
| 131 |
BodyIs(rd.T, rd.Recorder, expectedBody) |
| 132 |
} |
| 133 |
|
| 134 |
func (rd *Recorded) DecodeJsonPayload(v interface{}) error { |
| 135 |
return DecodeJsonPayload(rd.Recorder, v) |
| 136 |
} |