-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpresp.go
128 lines (119 loc) · 2.67 KB
/
httpresp.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
package asserter
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
)
type HttpResponse struct {
T
http.Handler
}
// StatusCode checks the expected status code. Options can be
// io.Reader for body, http.Header for headers or string for an error
// message.
func (t *HttpResponse) StatusCode(exp int, m, p string, opt ...interface{}) {
t.Helper()
body, headers, message := t.parse(opt...)
resp := t.do(m, p, body, headers)
if resp == nil {
return
}
if message == "" {
message = "StatusCode"
}
if resp.StatusCode != exp {
t.Fatalf(
"%s: %s, expected %v %s",
message, resp.Status, exp, http.StatusText(exp),
)
}
}
// BodyIs the same as BodyEquals
func (t *HttpResponse) BodyIs(exp string, m, p string, opt ...interface{}) {
t.Helper()
t.BodyEquals([]byte(exp), m, p, opt...)
}
func (t *HttpResponse) BodyEquals(exp []byte, m, p string, opt ...interface{}) {
t.Helper()
body, headers, message := t.parse(opt...)
resp := t.do(m, p, body, headers)
if resp == nil {
return
}
if message == "" {
message = "Body"
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(message, err)
}
if !bytes.Equal(data, exp) {
t.Error(message, "\ngot:", string(data), "\nexp:", string(exp))
}
}
// Contains fails if body does not contain exp
func (t *HttpResponse) Contains(exp string, m, p string, opt ...interface{}) {
t.Helper()
body, headers, message := t.parse(opt...)
resp := t.do(m, p, body, headers)
if resp == nil {
return
}
if message == "" {
message = "Contains"
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(message, err)
}
if !bytes.Contains(data, []byte(exp)) {
t.Error(message, "\ngot:", string(data), "\nexp:", string(exp))
}
}
// Header checks if the k header matches exp value
func (t *HttpResponse) Header(k, exp string, m, p string, opt ...interface{}) {
t.Helper()
body, headers, message := t.parse(opt...)
resp := t.do(m, p, body, headers)
if message == "" {
message = k
} else {
message = fmt.Sprintf("%s %s", k, message)
}
got := resp.Header.Get(k)
if got != exp {
t.Fatalf("%s: %q expected %q", message, got, exp)
}
}
func (t *HttpResponse) do(
m, p string, body io.Reader, headers http.Header,
) *http.Response {
t.Helper()
w := httptest.NewRecorder()
r, err := http.NewRequest(m, p, body)
if err != nil {
t.Fatal(err)
return nil
}
r.Header = headers
t.ServeHTTP(w, r)
return w.Result()
}
func (t *HttpResponse) parse(options ...interface{}) (
body io.Reader, headers http.Header, message string,
) {
for _, opt := range options {
switch v := opt.(type) {
case io.Reader:
body = v
case http.Header:
headers = v
case string:
message = v
}
}
return
}