-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebtest.go
66 lines (54 loc) · 1.34 KB
/
webtest.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
package webtest
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
)
type RequestTestCase struct {
Method string
URL string
Body io.Reader
Header http.Header
ResponseStatus int
ResponseBody io.Reader
}
type RequestTestSuite []RequestTestCase
func (ts RequestTestSuite) Execute(c *http.Client, errFn func(err error)) {
for i, tc := range ts {
if tc.Method == "" || tc.URL == "" {
panic("httpTestCase requires Method and URL to be set")
}
if tc.ResponseStatus == 0 {
tc.ResponseStatus = http.StatusOK
}
req, err := http.NewRequest(tc.Method, tc.URL, tc.Body)
if err != nil {
panic(err)
}
if tc.Header != nil {
req.Header = tc.Header
}
res, err := c.Do(req)
if err != nil {
panic(err)
}
if res.StatusCode != tc.ResponseStatus {
errFn(fmt.Errorf("Expected test case #%d (%s %s) to respond with %d but got %d", i, tc.Method, tc.URL, tc.ResponseStatus, res.StatusCode))
}
if tc.ResponseBody != nil {
eb, err := ioutil.ReadAll(tc.ResponseBody)
if err != nil {
panic(err)
}
ab, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
if strings.TrimSpace(string(ab)) != strings.TrimSpace(string(eb)) {
errFn(fmt.Errorf("Expected test case #%d (%s %s) response to be:\n%s\nbut got:\n%s", i, tc.Method, tc.URL, eb, ab))
}
}
}
}