This repository has been archived by the owner on Sep 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
227 lines (194 loc) · 4.87 KB
/
client.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
222
223
224
225
226
227
package webtest
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"github.com/podhmo/go-webtest/testclient"
"github.com/podhmo/go-webtest/tripperware"
)
// Option :
type Option interface {
Apply(*Config)
}
type optionFunc func(*Config)
func (f optionFunc) Apply(c *Config) {
f(c)
}
// Response :
type Response = testclient.Response
// Internal :
type Internal interface {
Do(req *http.Request, clientConfig *testclient.Config) (Response, error)
NewRequest(method string, path string, clientConfig *testclient.Config) (*http.Request, error)
}
// Client :
type Client struct {
Internal Internal
}
// Get :
func (c *Client) Get(path string, options ...Option) (Response, error) {
return c.Do("GET", path, options...)
}
// Post :
func (c *Client) Post(path string, options ...Option) (Response, error) {
return c.Do("POST", path, options...)
}
// Put :
func (c *Client) Put(path string, options ...Option) (Response, error) {
return c.Do("PUT", path, options...)
}
// Patch :
func (c *Client) Patch(path string, options ...Option) (Response, error) {
return c.Do("PATCH", path, options...)
}
// Delete :
func (c *Client) Delete(path string, options ...Option) (Response, error) {
return c.Do("DELETE", path, options...)
}
// Head :
func (c *Client) Head(path string, options ...Option) (Response, error) {
return c.Do("HEAD", path, options...)
}
// Do :
func (c *Client) Do(
method string,
path string,
options ...Option,
) (Response, error) {
config := NewConfig()
for _, opt := range options {
opt.Apply(config)
}
req, err := c.Internal.NewRequest(method, path, config.ClientConfig)
if err != nil {
return nil, err
}
return c.communicate(req, config)
}
// DoFromRequest :
func (c *Client) DoFromRequest(
req *http.Request,
options ...Option,
) (Response, error) {
config := NewConfig()
for _, opt := range options {
opt.Apply(config)
}
return c.communicate(req, config)
}
// DoFromRequest :
func (c *Client) communicate(
req *http.Request,
config *Config,
) (Response, error) {
for _, modifyRequest := range config.ModifyRequests {
modifyRequest(req)
}
doRequest := func(req *http.Request) (Response, error) {
return c.Internal.Do(req, config.ClientConfig)
}
return doRequest(req)
}
// NewClientFromTestServer :
func NewClientFromTestServer(ts *httptest.Server) *Client {
return &Client{
Internal: &testclient.RealClient{
URL: ts.URL,
},
}
}
// NewClientFromHandler :
func NewClientFromHandler(handler http.Handler) *Client {
return &Client{
Internal: &testclient.FakeClient{
Handler: handler,
},
}
}
// NewClientFromURL :
func NewClientFromURL(url string) *Client {
return &Client{
Internal: &testclient.RealClient{
URL: url,
},
}
}
// Config :
type Config struct {
BasePath string
Method string
ClientConfig *testclient.Config
ModifyRequests []func(*http.Request) // request modifyRequests
}
// NewConfig :
func NewConfig() *Config {
return &Config{
ClientConfig: &testclient.Config{},
}
}
// Copy :
func (c *Config) Copy() *Config {
return &Config{
BasePath: c.BasePath,
ModifyRequests: append(
make([]func(*http.Request), 0, len(c.ModifyRequests)),
c.ModifyRequests...,
),
ClientConfig: c.ClientConfig.Copy(),
}
}
// WithBasePath set base path
func WithBasePath(basePath string) Option {
return optionFunc(func(c *Config) {
c.ClientConfig.BasePath = basePath
})
}
// WithQuery :
func WithQuery(query url.Values) Option {
return optionFunc(func(c *Config) {
c.ClientConfig.Query = query
})
}
// WithForm setup as send form-data request
func WithForm(data url.Values) Option {
return optionFunc(func(c *Config) {
if c.ClientConfig.Body != nil {
panic("body is already set, enable to set body only once") // xxx
}
c.ClientConfig.Body = strings.NewReader(data.Encode())
c.ModifyRequests = append(c.ModifyRequests, func(req *http.Request) {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
})
})
}
// WithJSON setup as json request
func WithJSON(body io.Reader) Option {
return optionFunc(func(c *Config) {
if c.ClientConfig.Body != nil {
panic("body is already set, enable to set body only once") // xxx
}
c.ClientConfig.Body = body
c.ModifyRequests = append(c.ModifyRequests, func(req *http.Request) {
req.Header.Set("Content-Type", "application/json")
})
})
}
// WithJSONString setup as json request
func WithJSONString(body string) Option {
return WithJSON(bytes.NewBufferString(body))
}
// WithModifyRequest adds request modifyRequest
func WithModifyRequest(modifyRequest func(*http.Request)) Option {
return optionFunc(func(c *Config) {
c.ModifyRequests = append(c.ModifyRequests, modifyRequest)
})
}
// WithTripperware with client side middleware for roundTripper
func WithTripperware(wares ...tripperware.Ware) Option {
return optionFunc(func(c *Config) {
c.ClientConfig.Tripperwares = append(c.ClientConfig.Tripperwares, wares...)
})
}