This repository has been archived by the owner on May 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
hooks_test.go
159 lines (138 loc) · 3.29 KB
/
hooks_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package gotcha
import (
"bytes"
"github.com/sleeyax/gotcha/internal/tests"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func createClient(t *testing.T, hooks Hooks) *Client {
client, err := NewClient(&Options{
Hooks: hooks,
Adapter: &mockAdapter{OnCalledDoRequest: func(options *Options) *Response {
return NewResponse(&http.Response{
Request: &http.Request{
Method: options.Method,
URL: options.FullUrl,
Body: options.Body,
Header: options.Headers,
},
})
}},
})
if err != nil {
t.Fatal(err)
}
return client
}
func TestHooks_Init(t *testing.T) {
var hooked bool
client := createClient(t, Hooks{
Init: []InitHook{
func(options *Options) {
hooked = true
},
},
})
client.Get("https://example.com")
if !hooked {
t.FailNow()
}
}
func TestHooks_BeforeRequest(t *testing.T) {
var hooked bool
client := createClient(t, Hooks{
BeforeRequest: []BeforeRequestHook{
func(options *Options) {
hooked = true
options.Method = http.MethodPost
},
},
})
res, err := client.Get("https://example.com")
if err != nil {
t.FailNow()
}
if !hooked {
t.FailNow()
}
if m := res.Request.Method; m != http.MethodPost {
t.Fatalf(tests.MismatchFormat, "method", http.MethodPost, m)
}
}
func TestHooks_BeforeRedirect(t *testing.T) {
var hooked bool
prefixUrl := "https://redirected.example.com"
client, err := NewClient(&Options{
Hooks: Hooks{
BeforeRedirect: []BeforeRedirectHook{
func(options *Options, response *Response) {
hooked = true
options.PrefixURL = prefixUrl
},
},
},
Adapter: &mockAdapter{OnCalledDoRequest: func(options *Options) *Response {
header := http.Header{}
header.Add("location", "/home")
return NewResponse(&http.Response{Request: &http.Request{URL: options.FullUrl}, StatusCode: 302, Header: header, Body: io.NopCloser(bytes.NewReader([]byte{}))})
}},
FollowRedirect: true,
RedirectOptions: RedirectOptions{
RewriteMethods: false,
Limit: 1,
},
})
if err != nil {
t.Fatal(err)
}
res, err := client.Get("https://example.com")
if err != MaxRetriesExceededError {
t.Fatal(err)
}
if !hooked {
t.FailNow()
}
if u := res.Request.URL.String(); u != prefixUrl+"/home" {
t.Fatalf(tests.MismatchFormat, "full url", prefixUrl+"/home", u)
}
}
func TestHooks_BeforeRetry_And_AfterResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
}))
defer ts.Close()
var hookedBeforeRetry bool
var hookedAfterResponse bool
client := createClient(t, Hooks{
BeforeRetry: []BeforeRetryHook{
func(options *Options, error error, retryCount int) {
hookedBeforeRetry = true
},
},
AfterResponse: []AfterResponseHook{
func(response *Response, retry RetryFunc) (*Response, error) {
hookedAfterResponse = true
response.Body = io.NopCloser(strings.NewReader("hijacked"))
if !hookedBeforeRetry {
return retry(&Options{})
}
return response, nil
},
},
})
res, _ := client.Get(ts.URL)
if !hookedBeforeRetry || !hookedAfterResponse {
t.FailNow()
}
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
body := string(bodyBytes)
if body != "hijacked" {
t.Fatalf(tests.MismatchFormat, "body", "hijacked", body)
}
}