-
Notifications
You must be signed in to change notification settings - Fork 33
/
hooks_test.go
185 lines (162 loc) · 4.12 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package gosip
import (
"fmt"
"net/http"
"sync/atomic"
"testing"
)
func TestHooks(t *testing.T) {
siteURL := "http://localhost:8989"
closer, err := startFakeServer(":8989", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// produce an error
if r.RequestURI == "/_api/error" {
// intentional 404
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte(`{ "error": "404 Page not found" }`))
return
}
// faking digest response
if r.RequestURI == "/_api/ContextInfo" {
_, _ = fmt.Fprintf(w, `{"d":{"GetContextWebInformation":{"FormDigestValue":"FAKE","FormDigestTimeoutSeconds":120,"LibraryVersion":"FAKE"}}}`)
return
}
// backoff after 2 retries
if r.Header.Get("X-Gosip-Retry") == "2" {
_, _ = fmt.Fprintf(w, `{ "result": "Cool alfter some retries" }`)
return
}
// intentional 503
w.WriteHeader(http.StatusServiceUnavailable)
_, _ = w.Write([]byte(`{ "error": "503 Retry Please" }`))
}))
if err != nil {
t.Fatal(err)
}
defer func() { _ = closer.Close() }()
t.Run("Hooks", func(t *testing.T) {
// Request counters
var requestCounters = struct {
Errors int32
Responses int32
Retries int32
Requests int32
}{
Errors: 0,
Responses: 0,
Retries: 0,
Requests: 0,
}
client := &SPClient{
AuthCnfg: &AnonymousCnfg{SiteURL: siteURL},
RetryPolicies: map[int]int{503: 3},
Hooks: &HookHandlers{
OnError: func(e *HookEvent) {
atomic.AddInt32(&requestCounters.Errors, 1)
},
OnResponse: func(e *HookEvent) {
atomic.AddInt32(&requestCounters.Responses, 1)
},
OnRetry: func(e *HookEvent) {
atomic.AddInt32(&requestCounters.Retries, 1)
},
OnRequest: func(e *HookEvent) {
atomic.AddInt32(&requestCounters.Requests, 1)
},
},
}
if err := simpleCall(client, "/_api/get", nil); err != nil {
t.Error(err)
}
if err := simpleCall(client, "/_api/error", nil); err == nil {
t.Error("should be an error response")
}
// 4 requests
if requestCounters.Requests != 4 {
t.Error("wrong number of requests")
}
// 2 retries
if requestCounters.Retries != 2 {
t.Error("wrong number of retries")
}
// 2 response
if requestCounters.Responses != 2 {
t.Error("wrong number of responses")
}
// 1 error
if requestCounters.Errors != 1 {
t.Error("wrong number of errors")
}
})
t.Run("HooksOptout", func(t *testing.T) {
// Request counters
var requestCounters = struct {
Errors int32
Responses int32
Retries int32
Requests int32
}{
Errors: 0,
Responses: 0,
Retries: 0,
Requests: 0,
}
client := &SPClient{
AuthCnfg: &AnonymousCnfg{SiteURL: siteURL},
RetryPolicies: map[int]int{503: 3},
Hooks: &HookHandlers{
OnError: func(e *HookEvent) {
atomic.AddInt32(&requestCounters.Errors, 1)
},
OnResponse: func(e *HookEvent) {
atomic.AddInt32(&requestCounters.Responses, 1)
},
OnRetry: func(e *HookEvent) {
atomic.AddInt32(&requestCounters.Retries, 1)
},
OnRequest: func(e *HookEvent) {
atomic.AddInt32(&requestCounters.Requests, 1)
},
},
}
if err := simpleCall(client, "/_api/get", map[string]string{
"X-Gosip-NoHooks": "true",
}); err != nil {
t.Error(err)
}
if err := simpleCall(client, "/_api/error", map[string]string{
"X-Gosip-NoHooks": "true",
}); err == nil {
t.Error("should be an error response")
}
if requestCounters.Requests != 0 {
t.Error("wrong number of requests")
}
if requestCounters.Retries != 0 {
t.Error("wrong number of retries")
}
if requestCounters.Responses != 0 {
t.Error("wrong number of responses")
}
if requestCounters.Errors != 0 {
t.Error("wrong number of errors")
}
})
}
func simpleCall(client *SPClient, uri string, headers map[string]string) error {
req, err := http.NewRequest("GET", client.AuthCnfg.GetSiteURL()+uri, nil)
if err != nil {
return err
}
for h, v := range headers {
req.Header.Add(h, v)
}
resp, err := client.Execute(req)
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != 200 {
return fmt.Errorf("can't retry a request")
}
return nil
}