-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
79 lines (63 loc) · 1.5 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
package htest
import (
"github.com/stretchr/testify/assert"
"io"
"net/http"
"testing"
)
type (
Client struct {
handler http.Handler
*testing.T
}
)
func NewClient(t *testing.T) *Client {
return &Client{T: t}
}
func (c Client) To(handler http.Handler) *Client {
c.handler = handler
return &c
}
func (c Client) ToFunc(handlerFunc http.HandlerFunc) *Client {
c.handler = handlerFunc
return &c
}
func (c Client) NewRequest(req *http.Request) *Request {
return &Request{
Request: req,
Handler: c.handler,
T: c.T,
}
}
func (c Client) request(method, path string, body io.Reader) *Request {
req, err := http.NewRequest(method, path, body)
assert.Nil(c.T, err)
return c.NewRequest(req)
}
func (c Client) Get(path string) *Request {
return c.request(GET, path, nil)
}
func (c Client) Head(path string) *Request {
return c.request(HEAD, path, nil)
}
func (c Client) Trace(path string) *Request {
return c.request(TRACE, path, nil)
}
func (c Client) Options(path string) *Request {
return c.request(OPTIONS, path, nil)
}
func (c Client) Connect(path string) *Request {
return c.request(CONNECT, path, nil)
}
func (c Client) Delete(path string) *Request {
return c.request(DELETE, path, nil)
}
func (c Client) Post(path string, body io.Reader) *Request {
return c.request(POST, path, body)
}
func (c Client) Put(path string, body io.Reader) *Request {
return c.request(PUT, path, body)
}
func (c Client) Patch(path string, body io.Reader) *Request {
return c.request(PATCH, path, body)
}