-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransport_test.go
67 lines (62 loc) · 1.42 KB
/
transport_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
package httpreplay
import (
"context"
"io"
"net/http"
"path/filepath"
"testing"
)
func Test_getReplayFilePath(t *testing.T) {
type args struct {
dataDir string
req *http.Request
}
testcases := []struct {
given args
expected string
}{
{
given: args{
dataDir: "testdata",
req: newRequest(t, http.MethodGet, "http://example.com/", nil),
},
expected: filepath.Join("testdata", "GET---http%3A%2F%2Fexample.com%2F"),
},
}
for _, tc := range testcases {
actual := getReplayFilePath(tc.given.dataDir, tc.given.req)
if actual != tc.expected {
t.Errorf("expected %q but got %q (%#v)", tc.expected, actual, tc.given)
}
}
}
func newRequest(t *testing.T, method string, url string, body io.Reader) *http.Request {
req, err := http.NewRequest(method, url, body)
if err != nil {
t.Fatal(err)
}
return req
}
func TestNewReplayHandler(t *testing.T) {
ctx := context.Background()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://aereal.org/", nil)
if err != nil {
t.Fatal(err)
}
client := &http.Client{
Transport: NewReplayTransport("./testdata"),
}
resp, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("status code=%d", resp.StatusCode)
}
if resp.Request == nil {
t.Fatalf("incoming request not filled")
}
if resp.Request.URL.String() != req.URL.String() {
t.Errorf("request URI mismatch")
}
}