forked from h2non/baloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expect_test.go
157 lines (137 loc) · 4.1 KB
/
expect_test.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
package baloo
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/nbio/st"
"gopkg.in/h2non/gentleman.v2"
)
func TestExpect(t *testing.T) {
req := &Request{Request: gentleman.NewRequest()}
res := &http.Response{StatusCode: 200}
exp := NewExpect(req)
exp.Status(200)
st.Expect(t, exp.run(res, nil), nil)
}
func TestExpectBindTest(t *testing.T) {
req := &Request{Request: gentleman.NewRequest()}
exp := NewExpect(req)
exp.BindTest(t)
st.Expect(t, exp.test, t)
}
func TestExpectStatusOk(t *testing.T) {
req := &Request{Request: gentleman.NewRequest()}
res := &http.Response{StatusCode: 200}
exp := NewExpect(req)
exp.StatusOk()
st.Expect(t, exp.run(res, nil), nil)
}
func TestExpectStatusError(t *testing.T) {
req := &Request{Request: gentleman.NewRequest()}
res := &http.Response{StatusCode: 400}
exp := NewExpect(req)
exp.StatusError()
st.Expect(t, exp.run(res, nil), nil)
}
func TestExpectServerError(t *testing.T) {
req := &Request{Request: gentleman.NewRequest()}
res := &http.Response{StatusCode: 500}
exp := NewExpect(req)
exp.StatusServerError()
st.Expect(t, exp.run(res, nil), nil)
}
func TestExpectStatusClientError(t *testing.T) {
req := &Request{Request: gentleman.NewRequest()}
res := &http.Response{StatusCode: 404}
exp := NewExpect(req)
exp.StatusClientError()
st.Expect(t, exp.run(res, nil), nil)
}
func TestExpectType(t *testing.T) {
req := &Request{Request: gentleman.NewRequest()}
headers := http.Header{"Content-Type": []string{"application/json"}}
res := &http.Response{StatusCode: 404, Header: headers}
exp := NewExpect(req)
exp.Type("json")
st.Expect(t, exp.run(res, nil), nil)
}
func TestExpectHeader(t *testing.T) {
req := &Request{Request: gentleman.NewRequest()}
headers := http.Header{"Foo": []string{"bar"}}
res := &http.Response{StatusCode: 404, Header: headers}
exp := NewExpect(req)
exp.Header("foo", "bar")
st.Expect(t, exp.run(res, nil), nil)
}
func TestExpectHeaderEquals(t *testing.T) {
req := &Request{Request: gentleman.NewRequest()}
headers := http.Header{"Foo": []string{"bar"}}
res := &http.Response{StatusCode: 404, Header: headers}
exp := NewExpect(req)
exp.HeaderEquals("foo", "bar")
st.Expect(t, exp.run(res, nil), nil)
}
func TestExpectHeaderNotEquals(t *testing.T) {
req := &Request{Request: gentleman.NewRequest()}
headers := http.Header{"Foo": []string{"bar"}}
res := &http.Response{StatusCode: 404, Header: headers}
exp := NewExpect(req)
exp.HeaderNotEquals("foo", "foo")
st.Expect(t, exp.run(res, nil), nil)
}
func TestExpectHeaderPresent(t *testing.T) {
req := &Request{Request: gentleman.NewRequest()}
headers := http.Header{"Foo": []string{"bar"}}
res := &http.Response{StatusCode: 404, Header: headers}
exp := NewExpect(req)
exp.HeaderPresent("foo")
st.Expect(t, exp.run(res, nil), nil)
}
func TestExpectHeaderNotPresent(t *testing.T) {
req := &Request{Request: gentleman.NewRequest()}
headers := http.Header{"Foo": []string{"bar"}}
res := &http.Response{StatusCode: 404, Header: headers}
exp := NewExpect(req)
exp.HeaderNotPresent("bar")
st.Expect(t, exp.run(res, nil), nil)
}
func assertStatus(res *http.Response, req *http.Request) error {
if res.StatusCode >= 400 {
return errors.New("Invalid server response (> 400)")
}
return nil
}
func TestRealRequest(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; encoding=utf8")
fmt.Fprintln(w, "Hello, "+r.Header.Get("Foo"))
}))
defer ts.Close()
cli := New(ts.URL)
cli.Get("/foo").
SetHeader("Foo", "Bar").
Expect(t).
Status(200).
Type("json").
AssertFunc(assertStatus).
Done()
}
func TestGlobalAssertFunc(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; encoding=utf8")
fmt.Fprintln(w, "Hello, "+r.Header.Get("Foo"))
}))
defer ts.Close()
AddAssertFunc("foo", assertStatus)
defer FlushAssertFuncs()
cli := New(ts.URL)
cli.Get("/foo").
SetHeader("Foo", "Bar").
Expect(t).
Status(200).
Type("json").
Assert("foo").
Done()
}