-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsupertest.go
212 lines (178 loc) · 3.93 KB
/
supertest.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
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
package supertest
import (
"bytes"
"encoding/json"
"fmt"
"github.com/franela/goreq"
"io"
"io/ioutil"
"net/url"
"reflect"
"strings"
)
type Request struct {
base string
method string
path string
done reflect.Value
body interface{}
headers []headerTuple
query url.Values
}
type headerTuple struct {
name string
value string
}
func NewRequest(base string) *Request {
r := &Request{}
r.base = base
return r
}
func (r *Request) Get(path string) *Request {
r.method = "GET"
r.path = path
return r
}
func (r *Request) Post(path string) *Request {
r.method = "POST"
r.path = path
return r
}
func (r *Request) Put(path string) *Request {
r.method = "PUT"
r.path = path
return r
}
func (r *Request) Delete(path string) *Request {
r.method = "DELETE"
r.path = path
return r
}
func (r *Request) Patch(path string) *Request {
r.method = "PATCH"
r.path = path
return r
}
func (r *Request) Options(path string) *Request {
r.method = "OPTIONS"
r.path = path
return r
}
func (r *Request) Head(path string) *Request {
r.method = "HEAD"
r.path = path
return r
}
func (r *Request) Send(body interface{}) *Request {
r.body = body
return r
}
func (r *Request) Set(name, value string) *Request {
r.headers = append(r.headers, headerTuple{name: name, value: value})
return r
}
func (r *Request) Query(name, value string) *Request {
if r.query == nil {
r.query = url.Values{}
}
r.query.Add(name, value)
return r
}
func (r *Request) Expect(code int, args ...interface{}) error {
var bodyToCompare interface{}
if len(args) == 1 {
if reflect.ValueOf(args[0]).Kind() == reflect.Func {
r.done = reflect.ValueOf(args[0])
} else {
bodyToCompare = args[0]
}
}
if len(args) == 2 {
bodyToCompare = args[0]
r.done = reflect.ValueOf(args[1])
}
var err error
var body io.Reader
if r.body != nil {
body, err = prepareRequestBody(r.body)
if err != nil {
return err
}
}
req := goreq.Request{Method: r.method, Uri: r.base + r.path + "?" + r.query.Encode(), Body: body}
for _, tuple := range r.headers {
req.AddHeader(tuple.name, tuple.value)
}
res, e := req.Do()
if e != nil {
err = e
} else {
if res.StatusCode != code {
err = fmt.Errorf("Expected %d, was %d", code, res.StatusCode)
} else if bodyToCompare != nil {
// Read the entire response body
b, _ := ioutil.ReadAll(res.Body)
if s, ok := bodyToCompare.(string); ok {
// It is a string
str := string(b)
if s != str {
err = fmt.Errorf(fmt.Sprintf("%#v", s) + " does not equal " + fmt.Sprintf("%#v", str))
}
} else {
// Try to parse to JSON
ptrNewValue := reflect.New(reflect.TypeOf(bodyToCompare))
newValue := reflect.Indirect(ptrNewValue)
e := json.Unmarshal(b, ptrNewValue.Interface())
if e != nil {
err = fmt.Errorf("Expected: %#v, but got %#v. %s", bodyToCompare, string(b), e)
} else {
if !objectsAreEqual(bodyToCompare, newValue.Interface()) {
err = fmt.Errorf(fmt.Sprintf("%#v", bodyToCompare) + " does not equal " + fmt.Sprintf("%#v", newValue.Interface()))
}
}
}
}
}
if r.done.IsValid() {
if err != nil {
r.done.Call([]reflect.Value{reflect.ValueOf(err)})
} else {
r.done.Call([]reflect.Value{})
}
}
return err
}
func objectsAreEqual(a, b interface{}) bool {
if reflect.DeepEqual(a, b) {
return true
}
if reflect.ValueOf(a) == reflect.ValueOf(b) {
return true
}
if fmt.Sprintf("%#v", a) == fmt.Sprintf("%#v", b) {
return true
}
return false
}
func prepareRequestBody(b interface{}) (io.Reader, error) {
var body io.Reader
if sb, ok := b.(string); ok {
// treat is as text
body = strings.NewReader(sb)
} else if rb, ok := b.(io.Reader); ok {
// treat is as text
body = rb
} else if bb, ok := b.([]byte); ok {
//treat as byte array
body = bytes.NewReader(bb)
} else {
// try to jsonify it
j, err := json.Marshal(b)
if err == nil {
body = bytes.NewReader(j)
} else {
return nil, err
}
}
return body, nil
}