-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors_test.go
100 lines (84 loc) · 2.88 KB
/
errors_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
package signup
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
type JSONErr struct {
Code int `json:"code"`
Message string `json:"message"`
}
func TestHandleHTTPError(t *testing.T) {
t.Run("prints human-readable message for HTTP error status responses", func(t *testing.T) {
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Respond with an error
http.Error(w, "Invalid API key", http.StatusUnauthorized)
}))
resp, err := http.DefaultClient.Get(mockServer.URL)
if err != nil {
t.Fatal(err)
}
err = handleHTTPError(resp)
assertEqual(t, err.Error(), fmt.Sprintf(`HTTP Error:
GET: %s
/
Response:
401 Unauthorized
Invalid API key`+"\n", mockServer.URL))
})
t.Run("does not print HTML bodies", func(t *testing.T) {
respBody := "<html><body>{A giant chunk of HTML we don't want to see in the logs}</body></html>"
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Respond with HTML not found page
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, respBody)
}))
resp, err := http.DefaultClient.Get(mockServer.URL)
if err != nil {
t.Fatal(err)
}
err = handleHTTPError(resp)
assertEqual(t, err.Error(), fmt.Sprintf(`HTTP Error:
GET: %s
/
Response:
404 Not Found
[HTML response removed]`, mockServer.URL))
})
t.Run("prints the request context and HTTP Error response", func(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tooManyErr := JSONErr{
Code: http.StatusTooManyRequests,
Message: "You have exceeded the daily rate limit of (3) for Add meeting registrant API requests for the registrant ([email protected]). You can resume these API requests at GMT 00:00:00.",
}
JSONError(w, tooManyErr, http.StatusTooManyRequests)
}))
resp, err := http.DefaultClient.Get(srv.URL + "/v2/meetings/89012345678/registrants?occurrence_id=1665594000000")
if err != nil {
t.Fatal(err)
}
gotErr := handleHTTPError(resp)
if gotErr == nil {
t.Fatal("unexpected nil err")
}
want := fmt.Sprintf(`HTTP Error:
GET: %s
/v2/meetings/89012345678/registrants?occurrence_id=1665594000000
Response:
429 Too Many Requests
{"code":429,"message":"You have exceeded the daily rate limit of (3) for Add meeting registrant API requests for the registrant ([email protected]). You can resume these API requests at GMT 00:00:00."}
`, srv.URL)
assertEqual(t, gotErr.Error(), want)
})
}
// JSONError writes an HTTP error code and a JSON structured error body to an HTTP response.
func JSONError(w http.ResponseWriter, err interface{}, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
if err := json.NewEncoder(w).Encode(err); err != nil {
panic(err)
}
}