/
client.go
194 lines (158 loc) · 3.99 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
package httptesting
import (
"crypto/x509"
"fmt"
"net/http"
"net/http/cookiejar"
"net/http/httptest"
"net/url"
"strings"
"sync"
"testing"
"golang.org/x/net/websocket"
)
// Client defines request component of httptesting.
//
// NOTE: Client is not safe for concurrency, please use client.New(t) after initialized.
type Client struct {
mux sync.RWMutex
server *httptest.Server
host string
certs *x509.CertPool
jar *cookiejar.Jar
isTLS bool
}
// New returns an initialized *Client ready for testing
func New(host string, isTLS bool) *Client {
// adjust host
if strings.HasPrefix(host, "http://") || strings.HasPrefix(host, "https://") {
urlobj, err := url.Parse(host)
if err == nil {
isTLS = strings.HasPrefix(host, "https://")
host = urlobj.Host
}
}
jar, err := cookiejar.New(nil)
if err != nil {
panic(fmt.Sprintf("httptesting: New: %v", err))
}
return &Client{
host: host,
jar: jar,
isTLS: isTLS,
}
}
// NewWithTLS returns an initialized *Client with custom certificate.
func NewWithTLS(host string, cert *x509.Certificate) *Client {
// adjust host
if strings.HasPrefix(host, "https://") {
urlobj, err := url.Parse(host)
if err == nil {
host = urlobj.Host
}
}
jar, err := cookiejar.New(nil)
if err != nil {
panic(fmt.Sprintf("httptesting: NewWithTLS: %v", err))
}
certs := x509.NewCertPool()
certs.AddCert(cert)
return &Client{
host: host,
certs: certs,
jar: jar,
isTLS: true,
}
}
// Host returns the host and port of the server, e.g. "127.0.0.1:9090"
func (c *Client) Host() string {
if len(c.host) == 0 {
return ""
}
if c.host[0] == ':' {
return "127.0.0.1" + c.host
}
return c.host
}
// Url returns the abs http/isTLS URL of the resource, e.g. "http://127.0.0.1:9090/status".
// The scheme is set to isTLS if http.ssl is set to true in the configuration.
func (c *Client) Url(urlpath string, params ...url.Values) string {
if len(params) > 0 {
if !strings.Contains(urlpath, "?") {
urlpath += "?"
}
urlpath += params[0].Encode()
}
scheme := "http://"
if c.isTLS {
scheme = "https://"
}
return scheme + c.Host() + urlpath
}
// WebsocketUrl returns the abs websocket URL of the resource, e.g. "ws://127.0.0.1:9090/status"
func (c *Client) WebsocketUrl(urlpath string, params ...url.Values) string {
if len(params) > 0 {
if !strings.Contains(urlpath, "?") {
urlpath += "?"
}
urlpath += params[0].Encode()
}
return "ws://" + c.Host() + urlpath
}
// Cookies returns jar related to the host
func (c *Client) Cookies() ([]*http.Cookie, error) {
urlobj, err := url.Parse(c.Url("/"))
if err != nil {
return nil, err
}
return c.jar.Cookies(urlobj), nil
}
// SetCookies sets jar for the host
func (c *Client) SetCookies(cookies []*http.Cookie) error {
c.mux.Lock()
defer c.mux.Unlock()
urlobj, err := url.Parse(c.Url("/"))
if err != nil {
return err
}
c.jar.SetCookies(urlobj, cookies)
return nil
}
// NewClient creates a http client with cookie and tls for the Client.
func (c *Client) NewClient(filters ...RequestFilter) *http.Client {
client := &http.Client{
Transport: NewFilterTransport(filters, c.certs),
Jar: c.jar,
}
return client
}
// NewWebsocket creates a websocket connection to the given path and returns the connection
func (c *Client) NewWebsocket(t *testing.T, path string) *websocket.Conn {
origin := c.WebsocketUrl("/")
target := c.WebsocketUrl(path)
ws, err := websocket.Dial(target, "", origin)
if err != nil {
t.Fatalf("httptesting: NewWebscoket: connect %s with %v\n", path, err)
}
return ws
}
// New returns a *Request which has more customization!
func (c *Client) NewRequest(t *testing.T) *Request {
return NewRequest(t, c)
}
// New is alias of NewRequest for shortcut.
func (c *Client) New(t *testing.T) *Request {
return c.NewRequest(t)
}
// Close tries to
//
// - close *httptest.Server created by NewServer or NewServerWithTLS
func (c *Client) Close() {
c.mux.Lock()
defer c.mux.Unlock()
if c.server != nil {
c.server.Close()
c.server = nil
}
return
}