-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
88 lines (80 loc) · 2.39 KB
/
client_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
package hypert
import (
"net/http"
"testing"
)
type noopRequestSanitizer struct{}
func (n noopRequestSanitizer) SanitizeRequest(req *http.Request) *http.Request {
return req
}
type noopRequestValidator struct{}
func (n noopRequestValidator) Validate(_ T, _, _ RequestData) error {
return nil
}
func Test_configWithDefaults(t *testing.T) {
t.Run("should return default config", func(t *testing.T) {
cfg := configWithDefaults(t, false, nil)
if cfg.isRecordMode {
t.Error("expected isRecordMode to be false")
}
if cfg.namingScheme == nil {
t.Error("expected namingScheme to be set")
}
if cfg.requestSanitizer == nil {
t.Error("expected requestSanitizer to be set")
}
if cfg.parentHTTPClient == nil {
t.Error("expected parentHTTPClient to be set")
}
if cfg.requestValidator == nil {
t.Error("expected requestValidator to be set")
}
})
t.Run("should return config with options", func(t *testing.T) {
sanitizer := &noopRequestSanitizer{}
validator := &noopRequestValidator{}
namingScheme := &staticNamingScheme{}
parentHTTPClient := &http.Client{}
cfg := configWithDefaults(t, false, []Option{
WithRequestValidator(validator),
WithRequestSanitizer(sanitizer),
WithNamingScheme(namingScheme),
WithParentHTTPClient(parentHTTPClient),
})
if cfg.isRecordMode {
t.Error("expected isRecordMode to be false")
}
if cfg.namingScheme != namingScheme {
t.Error("expected namingScheme to be set")
}
if cfg.requestSanitizer != sanitizer {
t.Error("expected requestSanitizer to be set")
}
if cfg.parentHTTPClient != parentHTTPClient {
t.Error("expected parentHTTPClient to be set")
}
if cfg.requestValidator == nil || cfg.requestValidator != validator {
t.Error("expected requestValidator to be set")
}
})
}
func Test_TestClient(t *testing.T) {
t.Run("when record mode is on, it should use record transport", func(t *testing.T) {
c := TestClient(t, true)
if c.Transport == nil {
t.Fatal("expected transport to be set")
}
if _, ok := c.Transport.(*recordTransport); !ok {
t.Error("expected transport to be recordTransport")
}
})
t.Run("when record mode is off, it should use replay transport", func(t *testing.T) {
c := TestClient(t, false)
if c.Transport == nil {
t.Fatal("expected transport to be set")
}
if _, ok := c.Transport.(*replayTransport); !ok {
t.Error("expected transport to be replayTransport")
}
})
}