-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathrequest_builder_hook_test.go
More file actions
94 lines (89 loc) · 2.82 KB
/
request_builder_hook_test.go
File metadata and controls
94 lines (89 loc) · 2.82 KB
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
package fastshot
import (
"net/http"
"testing"
)
func TestRequestHookBuilder(t *testing.T) {
tests := []struct {
name string
setup func(*RequestBuilder) *RequestBuilder
assertFunc func(*testing.T, *RequestBuilder)
}{
{
name: "Add before request hook",
setup: func(rb *RequestBuilder) *RequestBuilder {
return rb.Hook().OnBeforeRequest(func(req *http.Request) error {
return nil
})
},
assertFunc: func(t *testing.T, rb *RequestBuilder) {
if got := len(rb.request.config.BeforeRequestHooks()); got != 1 {
t.Errorf("before request hooks count got %d, want 1", got)
}
},
},
{
name: "Add after response hook",
setup: func(rb *RequestBuilder) *RequestBuilder {
return rb.Hook().OnAfterResponse(func(req *http.Request, resp *http.Response) {})
},
assertFunc: func(t *testing.T, rb *RequestBuilder) {
//nolint:bodyclose // False positive: reading hook slice length, not handling a response body.
if got := len(rb.request.config.AfterResponseHooks()); got != 1 {
t.Errorf("after response hooks count got %d, want 1", got)
}
},
},
{
name: "Add multiple hooks",
setup: func(rb *RequestBuilder) *RequestBuilder {
return rb.
Hook().OnBeforeRequest(func(req *http.Request) error { return nil }).
Hook().OnBeforeRequest(func(req *http.Request) error { return nil }).
Hook().OnAfterResponse(func(req *http.Request, resp *http.Response) {}).
Hook().OnAfterResponse(func(req *http.Request, resp *http.Response) {})
},
assertFunc: func(t *testing.T, rb *RequestBuilder) {
if got := len(rb.request.config.BeforeRequestHooks()); got != 2 {
t.Errorf("before request hooks count got %d, want 2", got)
}
//nolint:bodyclose // False positive: reading hook slice length, not handling a response body.
if got := len(rb.request.config.AfterResponseHooks()); got != 2 {
t.Errorf("after response hooks count got %d, want 2", got)
}
},
},
{
name: "No hooks by default",
setup: func(rb *RequestBuilder) *RequestBuilder {
return rb
},
assertFunc: func(t *testing.T, rb *RequestBuilder) {
if got := len(rb.request.config.BeforeRequestHooks()); got != 0 {
t.Errorf("before request hooks count got %d, want 0", got)
}
//nolint:bodyclose // False positive: reading hook slice length, not handling a response body.
if got := len(rb.request.config.AfterResponseHooks()); got != 0 {
t.Errorf("after response hooks count got %d, want 0", got)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Arrange
rb := &RequestBuilder{
request: &Request{
config: newRequestConfigBase("", ""),
},
}
// Act
result := tt.setup(rb)
// Assert
if result != rb {
t.Errorf("got different builder, want same")
}
tt.assertFunc(t, rb)
})
}
}