-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttperror_test.go
More file actions
150 lines (127 loc) · 4.64 KB
/
httperror_test.go
File metadata and controls
150 lines (127 loc) · 4.64 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
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
package httperror
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
)
// TestRespond tests the Respond function and SetErrorHandler.
func TestRespond(t *testing.T) {
// Reset handler to default before test
SetErrorHandler(nil)
t.Run("default handler", func(t *testing.T) {
rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)
err := New(http.StatusBadRequest, "Bad Request")
Respond(rr, req, err)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected status %d, got %d", http.StatusBadRequest, rr.Code)
}
if rr.Header().Get("Content-Type") != "application/json; charset=utf-8" {
t.Errorf("expected Content-Type application/json; charset=utf-8, got %s", rr.Header().Get("Content-Type"))
}
})
t.Run("custom handler", func(t *testing.T) {
customHandler := func(w http.ResponseWriter, r *http.Request, err error) {
w.WriteHeader(http.StatusTeapot)
w.Write([]byte("I am a teapot"))
}
SetErrorHandler(customHandler)
defer SetErrorHandler(nil) // Cleanup
rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)
err := errors.New("some error")
Respond(rr, req, err)
if rr.Code != http.StatusTeapot {
t.Errorf("expected status %d, got %d", http.StatusTeapot, rr.Code)
}
if rr.Body.String() != "I am a teapot" {
t.Errorf("expected body 'I am a teapot', got '%s'", rr.Body.String())
}
})
}
// TestDefaultErrorHandler tests the DefaultErrorHandler function.
func TestDefaultErrorHandler(t *testing.T) {
t.Run("with HttpError and JSON default", func(t *testing.T) {
rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)
err := &HttpError{Status: http.StatusNotFound, Message: "Not Found"}
DefaultErrorHandler(rr, req, err)
if rr.Code != http.StatusNotFound {
t.Errorf("expected status %d, got %d", http.StatusNotFound, rr.Code)
}
if rr.Header().Get("Content-Type") != "application/json; charset=utf-8" {
t.Errorf("expected content type application/json, got %s", rr.Header().Get("Content-Type"))
}
var body HttpError
if err := json.NewDecoder(rr.Body).Decode(&body); err != nil {
t.Fatalf("could not decode response body: %v", err)
}
if body.Status != err.Status || body.Message != err.Message {
t.Errorf("expected body %+v, got %+v", err, body)
}
})
t.Run("with HttpError and HTML accept", func(t *testing.T) {
rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set("Accept", "text/html")
err := &HttpError{Status: http.StatusForbidden, Message: "Forbidden"}
DefaultErrorHandler(rr, req, err)
if rr.Code != http.StatusForbidden {
t.Errorf("expected status %d, got %d", http.StatusForbidden, rr.Code)
}
if rr.Header().Get("Content-Type") != "text/html; charset=utf-8" {
t.Errorf("expected content type text/html, got %s", rr.Header().Get("Content-Type"))
}
expectedBody := `<div class="http-error">Forbidden</div>`
if rr.Body.String() != expectedBody {
t.Errorf("expected body '%s', got '%s'", expectedBody, rr.Body.String())
}
})
t.Run("with generic error", func(t *testing.T) {
rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)
err := errors.New("some internal error")
DefaultErrorHandler(rr, req, err)
if rr.Code != http.StatusInternalServerError {
t.Errorf("expected status %d, got %d", http.StatusInternalServerError, rr.Code)
}
var body HttpError
if err := json.NewDecoder(rr.Body).Decode(&body); err != nil {
t.Fatalf("could not decode response body: %v", err)
}
expectedErr := InternalServerErrorError()
if body.Status != expectedErr.Status {
t.Errorf("expected status %d, got %d", expectedErr.Status, body.Status)
}
})
}
func TestSetErrorHandler(t *testing.T) {
// Ensure it resets to default when nil is passed
SetErrorHandler(nil)
// Check by address if possible, or behavior. Go doesn't allow comparing func pointers easily.
// We'll test behavior.
rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)
Respond(rr, req, New(http.StatusOK, "ok"))
if rr.Header().Get("Content-Type") != "application/json; charset=utf-8" {
t.Error("Default handler should set JSON content type")
}
// Set custom
SetErrorHandler(func(w http.ResponseWriter, r *http.Request, err error) {
w.Write([]byte("custom"))
})
rr = httptest.NewRecorder()
Respond(rr, req, errors.New("err"))
if rr.Body.String() != "custom" {
t.Error("Custom handler didn't work")
}
// Reset to default
SetErrorHandler(nil)
rr = httptest.NewRecorder()
Respond(rr, req, New(http.StatusOK, "ok"))
if rr.Header().Get("Content-Type") != "application/json; charset=utf-8" {
t.Error("Should revert to default handler")
}
}