-
Notifications
You must be signed in to change notification settings - Fork 281
/
helpers.go
297 lines (268 loc) · 8.47 KB
/
helpers.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Package test provides utility methods to test APIs based on the common
// API.
package test
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"reflect"
"strings"
"testing"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
peerstore "github.com/libp2p/go-libp2p-core/peerstore"
p2phttp "github.com/libp2p/go-libp2p-http"
)
var (
// SSLCertFile is the location of the certificate file.
// Used in HTTPClient to set the right certificate when
// creating an HTTPs client. Might need adjusting depending
// on where the tests are running.
SSLCertFile = "test/server.crt"
// ClientOrigin sets the Origin header for requests to this.
ClientOrigin = "myorigin"
)
// ProcessResp puts a response into a given type or fails the test.
func ProcessResp(t *testing.T, httpResp *http.Response, err error, resp interface{}) {
if err != nil {
t.Fatal("error making request: ", err)
}
body, err := ioutil.ReadAll(httpResp.Body)
defer httpResp.Body.Close()
if err != nil {
t.Fatal("error reading body: ", err)
}
if len(body) != 0 {
err = json.Unmarshal(body, resp)
if err != nil {
t.Error(string(body))
t.Fatal("error parsing json: ", err)
}
}
}
// ProcessStreamingResp decodes a streaming response into the given type
// and fails the test on error.
func ProcessStreamingResp(t *testing.T, httpResp *http.Response, err error, resp interface{}, trailerError bool) {
if err != nil {
t.Fatal("error making streaming request: ", err)
}
if httpResp.StatusCode > 399 {
// normal response with error
ProcessResp(t, httpResp, err, resp)
return
}
defer httpResp.Body.Close()
dec := json.NewDecoder(httpResp.Body)
// If we passed a slice we fill it in, otherwise we just decode
// on top of the passed value.
tResp := reflect.TypeOf(resp)
if tResp.Elem().Kind() == reflect.Slice {
vSlice := reflect.MakeSlice(reflect.TypeOf(resp).Elem(), 0, 1000)
vType := tResp.Elem().Elem()
for {
v := reflect.New(vType)
err := dec.Decode(v.Interface())
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}
vSlice = reflect.Append(vSlice, v.Elem())
}
reflect.ValueOf(resp).Elem().Set(vSlice)
} else {
for {
err := dec.Decode(resp)
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}
}
}
trailerValues := httpResp.Trailer.Values("X-Stream-Error")
if trailerError && len(trailerValues) <= 1 && trailerValues[0] == "" {
t.Error("expected trailer error")
}
if !trailerError && len(trailerValues) >= 2 {
t.Error("got trailer error: ", trailerValues)
}
}
// CheckHeaders checks that all the headers are set to what is expected.
func CheckHeaders(t *testing.T, expected map[string][]string, url string, headers http.Header) {
for k, v := range expected {
if strings.Join(v, ",") != strings.Join(headers[k], ",") {
t.Errorf("%s does not show configured headers: %s", url, k)
}
}
if headers.Get("Content-Type") != "application/json" {
t.Errorf("%s is not application/json", url)
}
if eh := headers.Get("Access-Control-Expose-Headers"); eh == "" {
t.Error("AC-Expose-Headers not set")
}
}
// API represents what an API is to us.
type API interface {
HTTPAddresses() ([]string, error)
Host() host.Host
Headers() map[string][]string
}
// URLFunc is a function that given an API returns a url string.
type URLFunc func(a API) string
// HTTPURL returns the http endpoint of the API.
func HTTPURL(a API) string {
u, _ := a.HTTPAddresses()
return fmt.Sprintf("http://%s", u[0])
}
// P2pURL returns the libp2p endpoint of the API.
func P2pURL(a API) string {
return fmt.Sprintf("libp2p://%s", peer.Encode(a.Host().ID()))
}
// HttpsURL returns the HTTPS endpoint of the API
func httpsURL(a API) string {
u, _ := a.HTTPAddresses()
return fmt.Sprintf("https://%s", u[0])
}
// IsHTTPS returns true if a url string uses HTTPS.
func IsHTTPS(url string) bool {
return strings.HasPrefix(url, "https")
}
// HTTPClient returns a client that supporst both http/https and
// libp2p-tunneled-http.
func HTTPClient(t *testing.T, h host.Host, isHTTPS bool) *http.Client {
tr := &http.Transport{}
if isHTTPS {
certpool := x509.NewCertPool()
cert, err := ioutil.ReadFile(SSLCertFile)
if err != nil {
t.Fatal("error reading cert for https client: ", err)
}
certpool.AppendCertsFromPEM(cert)
tr = &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: certpool,
}}
}
if h != nil {
tr.RegisterProtocol("libp2p", p2phttp.NewTransport(h))
}
return &http.Client{Transport: tr}
}
// MakeHost makes a libp2p host that knows how to talk to the given API.
func MakeHost(t *testing.T, api API) host.Host {
h, err := libp2p.New()
if err != nil {
t.Fatal(err)
}
h.Peerstore().AddAddrs(
api.Host().ID(),
api.Host().Addrs(),
peerstore.PermanentAddrTTL,
)
return h
}
// MakeGet performs a GET request against the API.
func MakeGet(t *testing.T, api API, url string, resp interface{}) {
h := MakeHost(t, api)
defer h.Close()
c := HTTPClient(t, h, IsHTTPS(url))
req, _ := http.NewRequest(http.MethodGet, url, nil)
req.Header.Set("Origin", ClientOrigin)
httpResp, err := c.Do(req)
ProcessResp(t, httpResp, err, resp)
CheckHeaders(t, api.Headers(), url, httpResp.Header)
}
// MakePost performs a POST request agains the API with the given body.
func MakePost(t *testing.T, api API, url string, body []byte, resp interface{}) {
MakePostWithContentType(t, api, url, body, "application/json", resp)
}
// MakePostWithContentType performs a POST with the given body and content-type.
func MakePostWithContentType(t *testing.T, api API, url string, body []byte, contentType string, resp interface{}) {
h := MakeHost(t, api)
defer h.Close()
c := HTTPClient(t, h, IsHTTPS(url))
req, _ := http.NewRequest(http.MethodPost, url, bytes.NewReader(body))
req.Header.Set("Content-Type", contentType)
req.Header.Set("Origin", ClientOrigin)
httpResp, err := c.Do(req)
ProcessResp(t, httpResp, err, resp)
CheckHeaders(t, api.Headers(), url, httpResp.Header)
}
// MakeDelete performs a DELETE request against the given API.
func MakeDelete(t *testing.T, api API, url string, resp interface{}) {
h := MakeHost(t, api)
defer h.Close()
c := HTTPClient(t, h, IsHTTPS(url))
req, _ := http.NewRequest(http.MethodDelete, url, bytes.NewReader([]byte{}))
req.Header.Set("Origin", ClientOrigin)
httpResp, err := c.Do(req)
ProcessResp(t, httpResp, err, resp)
CheckHeaders(t, api.Headers(), url, httpResp.Header)
}
// MakeOptions performs an OPTIONS request against the given api.
func MakeOptions(t *testing.T, api API, url string, reqHeaders http.Header) http.Header {
h := MakeHost(t, api)
defer h.Close()
c := HTTPClient(t, h, IsHTTPS(url))
req, _ := http.NewRequest(http.MethodOptions, url, nil)
req.Header = reqHeaders
httpResp, err := c.Do(req)
ProcessResp(t, httpResp, err, nil)
return httpResp.Header
}
// MakeStreamingPost performs a POST request and uses ProcessStreamingResp
func MakeStreamingPost(t *testing.T, api API, url string, body io.Reader, contentType string, resp interface{}) {
h := MakeHost(t, api)
defer h.Close()
c := HTTPClient(t, h, IsHTTPS(url))
req, _ := http.NewRequest(http.MethodPost, url, body)
req.Header.Set("Content-Type", contentType)
req.Header.Set("Origin", ClientOrigin)
httpResp, err := c.Do(req)
ProcessStreamingResp(t, httpResp, err, resp, false)
CheckHeaders(t, api.Headers(), url, httpResp.Header)
}
// MakeStreamingGet performs a GET request and uses ProcessStreamingResp
func MakeStreamingGet(t *testing.T, api API, url string, resp interface{}, trailerError bool) {
h := MakeHost(t, api)
defer h.Close()
c := HTTPClient(t, h, IsHTTPS(url))
req, _ := http.NewRequest(http.MethodGet, url, nil)
req.Header.Set("Origin", ClientOrigin)
httpResp, err := c.Do(req)
ProcessStreamingResp(t, httpResp, err, resp, trailerError)
CheckHeaders(t, api.Headers(), url, httpResp.Header)
}
// Func is a function that runs a test with a given URL.
type Func func(t *testing.T, url URLFunc)
// BothEndpoints runs a test.Func against the http and p2p endpoints.
func BothEndpoints(t *testing.T, test Func) {
t.Run("in-parallel", func(t *testing.T) {
t.Run("http", func(t *testing.T) {
t.Parallel()
test(t, HTTPURL)
})
t.Run("libp2p", func(t *testing.T) {
t.Parallel()
test(t, P2pURL)
})
})
}
// HTTPSEndPoint runs the given test.Func against an HTTPs endpoint.
func HTTPSEndPoint(t *testing.T, test Func) {
t.Run("in-parallel", func(t *testing.T) {
t.Run("https", func(t *testing.T) {
t.Parallel()
test(t, httpsURL)
})
})
}