-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
72 lines (65 loc) · 1.98 KB
/
utils.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
package testutils
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
"golang.org/x/net/http2"
)
// Server starts an insecure http2 enabled server for testing a route
func Server(pattern string, mock http.HandlerFunc) *httptest.Server {
handler := http.NewServeMux()
handler.HandleFunc(pattern, mock)
srv := httptest.NewUnstartedServer(handler)
srv.EnableHTTP2 = true
srv.TLS = srv.Config.TLSConfig
return srv
}
// Client returns a http client compatible with the http2 test server
func Client() *http.Client {
tr := &http.Transport{}
if err := http2.ConfigureTransport(tr); err != nil {
panic(fmt.Errorf("Failed to configure http2 transport: %v", err))
}
tr.TLSClientConfig.InsecureSkipVerify = true
return &http.Client{Transport: tr}
}
// StreamingHandler matches an initial JSON request body with the given in param, then
// writes each of the given outs params to the response waiting 100ms inbetween each
func StreamingHandler(t *testing.T, in interface{}, outs ...interface{}) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
v := reflect.New(reflect.TypeOf(in).Elem()).Interface()
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, in, v)
for _, out := range outs {
time.Sleep(time.Millisecond * 100)
err = json.NewEncoder(w).Encode(out)
if err != nil {
t.Fatal(err)
}
}
}
}
// Handler returns a http handler which decodes and matches JSON request bodies with the in interface{},
// and writes the out interface{} as JSON to the response writer
func Handler(t *testing.T, in interface{}, out interface{}) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
v := reflect.New(reflect.TypeOf(in).Elem()).Interface()
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, in, v)
err = json.NewEncoder(w).Encode(out)
if err != nil {
t.Fatal(err)
}
}
}