-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclient_test.go
98 lines (74 loc) · 1.93 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
89
90
91
92
93
94
95
96
97
98
package yext
import (
"fmt"
"net/http"
"strings"
"testing"
)
func TestErrorDeserialzation(t *testing.T) {
setup()
defer teardown()
errorResp := `{"errors": [{
"message": "We had a problem with our software. Please contact support!",
"errorCode": 9
}]}`
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(errorResp))
})
err := client.DoRequest("", "", nil)
if _, ok := err.(*ErrorResponse); !ok {
t.Error("Expected to recieve *ErrorResponse type, got", err, "instead")
}
}
func TestRetries(t *testing.T) {
setup()
defer teardown()
client.retryAttempts = 3
requests := 0
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
requests++
w.WriteHeader(http.StatusInternalServerError)
})
client.DoRequest("", "", nil)
if requests != 4 {
t.Error("Expected 4 net attempts when error encountered, only got", requests)
}
}
func TestLastRetryError(t *testing.T) {
setup()
defer teardown()
client.retryAttempts = 3
request := 0
errf := func(n int) string {
return fmt.Sprintf("error from request #%d", n)
}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
request++
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(errf(request)))
})
err := client.DoRequest("", "", nil)
expectedErr := errf(client.retryAttempts + 1)
if !strings.Contains(err.Error(), expectedErr) {
t.Errorf("Expected to get error `%s`, instead got `%s`", expectedErr, err)
}
}
func TestBailout(t *testing.T) {
setup()
client.retryAttempts = 3
defer teardown()
requests := 0
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
requests++
if requests == 4 {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
})
err := client.DoRequest("", "", nil)
if err != nil {
t.Error("Expected error to be nil when final attempt succeeded:", err)
}
}