This repository has been archived by the owner on Mar 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
context.go
221 lines (171 loc) · 4.34 KB
/
context.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
213
214
215
216
217
218
219
220
221
package httptest
import (
"fmt"
"io"
"net/http"
"strings"
)
// ---------------------------------------------------------------------------
type TransportComposer interface {
Compose(base http.RoundTripper) http.RoundTripper
}
type Executor interface {
Exec(ctx *Context, code string)
}
// ---------------------------------------------------------------------------
func mimeType(ct string) string {
if ct == "form" {
return "application/x-www-form-urlencoded"
}
if ct == "binary" {
return "application/octet-stream"
}
if strings.Index(ct, "/") < 0 {
return "application/" + ct
}
return ct
}
// ---------------------------------------------------------------------------
type Request struct {
method string
url string
auth TransportComposer
ctx *Context
header http.Header
bodyType string
body string
}
func NewRequest(ctx *Context, method, url string) *Request {
ctx.DeleteVar("resp")
p := &Request{
ctx: ctx,
method: method,
url: url,
header: make(http.Header),
}
ctx.Log(" ====>", method, url)
return p
}
func (p *Request) WithAuth(v interface{}) *Request {
if v == nil {
p.auth = nil
return p
}
if name, ok := v.(string); ok {
auth, ok := p.ctx.auths[name]
if !ok {
p.ctx.Fatal("WithAuth failed: auth not found -", name)
}
p.auth = auth
return p
}
if auth, ok := v.(TransportComposer); ok {
p.auth = auth
return p
}
p.ctx.Fatal("WithAuth failed: invalid auth -", v)
return p
}
func (p *Request) WithHeader(key string, values ...string) *Request {
p.header[key] = values
return p
}
func (p *Request) WithBody(bodyType, body string) *Request {
p.bodyType = mimeType(bodyType)
p.body = body
return p
}
func (p *Request) WithBodyf(bodyType, format string, v ...interface{}) *Request {
p.bodyType = mimeType(bodyType)
p.body = fmt.Sprintf(format, v...)
return p
}
func mergeHeader(to, from http.Header) {
for k, v := range from {
to[k] = v
}
}
func (p *Request) send() (resp *http.Response, err error) {
var body io.Reader
if len(p.body) > 0 {
body = strings.NewReader(p.body)
}
req, err := p.ctx.newRequest(p.method, p.url, body)
if err != nil {
p.ctx.Fatal("http.NewRequest failed:", p.method, p.url, p.body, err)
return
}
mergeHeader(req.Header, p.ctx.DefaultHeader)
if body != nil {
if p.bodyType != "" {
req.Header.Set("Content-Type", p.bodyType)
}
req.ContentLength = int64(len(p.body))
}
mergeHeader(req.Header, p.header)
t := p.ctx.transport
if p.auth != nil {
t = p.auth.Compose(t)
}
c := &http.Client{Transport: t}
return c.Do(req)
}
func (p *Request) Ret(code int) (resp *Response) {
resp1, err := p.send()
resp = newResponse(p, resp1, err)
p.ctx.MatchVar("resp", map[string]interface{}{
"body": resp.BodyObj,
"header": resp.Header,
"code": float64(resp.StatusCode),
})
return resp.matchCode(code)
}
// ---------------------------------------------------------------------------
type TestingT interface {
Fatal(args ...interface{})
Log(args ...interface{})
}
type NilTestingT struct{}
func (p NilTestingT) Fatal(args ...interface{}) {}
func (p NilTestingT) Log(args ...interface{}) {}
// ---------------------------------------------------------------------------
type Context struct {
TestingT
varsMgr
hostsMgr
transport http.RoundTripper
auths map[string]TransportComposer
DefaultHeader http.Header
MatchResponseError func(message string, req *Request, resp *Response)
}
func New(t TestingT) *Context {
auths := make(map[string]TransportComposer)
p := &Context{
TestingT: t,
auths: auths,
transport: http.DefaultTransport,
DefaultHeader: make(http.Header),
MatchResponseError: matchRespError,
}
p.initHostsMgr()
p.initVarsMgr()
return p
}
func (p *Context) SetTransport(transport http.RoundTripper) {
p.transport = transport
}
func (p *Context) SetAuth(name string, auth TransportComposer) {
p.auths[name] = auth
}
func (p *Context) Exec(executor Executor, code string) *Context {
executor.Exec(p, code)
return p
}
func (p *Context) Request(method, url string) *Request {
return NewRequest(p, method, url)
}
func (p *Context) Requestf(method, format string, v ...interface{}) *Request {
url := fmt.Sprintf(format, v...)
return NewRequest(p, method, url)
}
// ---------------------------------------------------------------------------