This repository was 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 pathtripperware_test.go
89 lines (83 loc) · 1.85 KB
/
tripperware_test.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
package tripperware
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
)
func TestTripperware(t *testing.T) {
handler := func(w http.ResponseWriter, req *http.Request) {
fmt.Fprint(w, req.URL.Query().Encode())
}
cases := []struct {
msg string
want string
path string
stack List
}{
{
msg: "zero decorator",
want: "x=1",
path: "/?x=1",
},
{
msg: "one decorator",
want: "x=1&y=2",
path: "/?x=1",
stack: Stack(
func(next http.RoundTripper) http.RoundTripper {
return RoundTripFunc(func(req *http.Request) (*http.Response, error) {
q := req.URL.Query()
q.Add("y", "2")
req.URL.RawQuery = q.Encode()
return next.RoundTrip(req)
})
},
),
},
{
msg: "two decorator",
want: "x=1&y=2&z=3",
path: "/?x=1",
stack: Stack(
func(next http.RoundTripper) http.RoundTripper {
return RoundTripFunc(func(req *http.Request) (*http.Response, error) {
q := req.URL.Query()
q.Add("y", "2")
req.URL.RawQuery = q.Encode()
return next.RoundTrip(req)
})
},
func(next http.RoundTripper) http.RoundTripper {
return RoundTripFunc(func(req *http.Request) (*http.Response, error) {
q := req.URL.Query()
q.Add("z", "3")
req.URL.RawQuery = q.Encode()
return next.RoundTrip(req)
})
},
),
},
}
for _, c := range cases {
t.Run(c.msg, func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(handler))
client := c.stack.DecorateClient(&http.Client{}, true)
res, err := client.Get(fmt.Sprintf("%s%s", ts.URL, c.path))
if err != nil {
t.Fatal(err)
}
var b strings.Builder
if _, err := io.Copy(&b, res.Body); err != nil {
t.Fatal(err)
}
got := b.String()
if !reflect.DeepEqual(got, c.want) {
t.Errorf("want %s, but %s", c.want, got)
}
})
}
}