-
Notifications
You must be signed in to change notification settings - Fork 162
/
util.go
136 lines (114 loc) · 3.14 KB
/
util.go
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
package test
import (
"encoding/json"
"fmt"
"io/ioutil"
"mime"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// MakeSimpleRequest returns a http.Request. The returned request object can be
// further prepared by adding headers and query string parmaters, for instance.
func MakeSimpleRequest(method string, urlStr string, payload interface{}) *http.Request {
var s string
if payload != nil {
b, err := json.Marshal(payload)
if err != nil {
panic(err)
}
s = fmt.Sprintf("%s", b)
}
r, err := http.NewRequest(method, urlStr, strings.NewReader(s))
if err != nil {
panic(err)
}
r.Header.Set("Accept-Encoding", "gzip")
if payload != nil {
r.Header.Set("Content-Type", "application/json")
}
return r
}
// CodeIs compares the rescorded status code
func CodeIs(t *testing.T, r *httptest.ResponseRecorder, expectedCode int) {
if r.Code != expectedCode {
t.Errorf("Code %d expected, got: %d", expectedCode, r.Code)
}
}
// HeaderIs tests the first value for the given headerKey
func HeaderIs(t *testing.T, r *httptest.ResponseRecorder, headerKey, expectedValue string) {
value := r.HeaderMap.Get(headerKey)
if value != expectedValue {
t.Errorf(
"%s: %s expected, got: %s",
headerKey,
expectedValue,
value,
)
}
}
func ContentTypeIsJson(t *testing.T, r *httptest.ResponseRecorder) {
mediaType, params, _ := mime.ParseMediaType(r.HeaderMap.Get("Content-Type"))
charset := params["charset"]
if mediaType != "application/json" {
t.Errorf(
"Content-Type media type: application/json expected, got: %s",
mediaType,
)
}
if charset != "" && strings.ToUpper(charset) != "UTF-8" {
t.Errorf(
"Content-Type charset: must be empty or UTF-8, got: %s",
charset,
)
}
}
func ContentEncodingIsGzip(t *testing.T, r *httptest.ResponseRecorder) {
HeaderIs(t, r, "Content-Encoding", "gzip")
}
func BodyIs(t *testing.T, r *httptest.ResponseRecorder, expectedBody string) {
body := r.Body.String()
if body != expectedBody {
t.Errorf("Body '%s' expected, got: '%s'", expectedBody, body)
}
}
func DecodeJsonPayload(r *httptest.ResponseRecorder, v interface{}) error {
content, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
err = json.Unmarshal(content, v)
if err != nil {
return err
}
return nil
}
type Recorded struct {
T *testing.T
Recorder *httptest.ResponseRecorder
}
// RunRequest runs a HTTP request through the given handler
func RunRequest(t *testing.T, handler http.Handler, request *http.Request) *Recorded {
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, request)
return &Recorded{t, recorder}
}
func (rd *Recorded) CodeIs(expectedCode int) {
CodeIs(rd.T, rd.Recorder, expectedCode)
}
func (rd *Recorded) HeaderIs(headerKey, expectedValue string) {
HeaderIs(rd.T, rd.Recorder, headerKey, expectedValue)
}
func (rd *Recorded) ContentTypeIsJson() {
ContentTypeIsJson(rd.T, rd.Recorder)
}
func (rd *Recorded) ContentEncodingIsGzip() {
rd.HeaderIs("Content-Encoding", "gzip")
}
func (rd *Recorded) BodyIs(expectedBody string) {
BodyIs(rd.T, rd.Recorder, expectedBody)
}
func (rd *Recorded) DecodeJsonPayload(v interface{}) error {
return DecodeJsonPayload(rd.Recorder, v)
}