-
Notifications
You must be signed in to change notification settings - Fork 79
/
error_test.go
200 lines (175 loc) · 6.24 KB
/
error_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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package tigertonic
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"net/url"
"testing"
)
var ttTestAcceptContentType = []struct {
r *http.Request
contentType string
out bool
}{
{&http.Request{Header: http.Header{"Accept": []string{"text/plain; q=0.3, */*"}}}, "image/jpeg", true},
{&http.Request{Header: http.Header{"Accept": []string{"image/*; q=0.5, text/*"}}}, "text/plain", true},
{&http.Request{Header: http.Header{"Accept": []string{"image/jpeg, text/*"}}}, "image/jpeg", true},
{&http.Request{Header: http.Header{"Accept": []string{"image/*; q=0.5, text/*"}}}, "application/json", false},
}
func TestAcceptContentType(t *testing.T) {
for i, tt := range ttTestAcceptContentType {
if x := acceptContentType(tt.r, tt.contentType); x != tt.out {
t.Errorf("Test %d expected %t", i, tt.out)
}
}
}
func TestNamedHTTPEquivError(t *testing.T) {
var err error = OK{testNamedError("foo")}
if "foo" != errorName(err, "error") {
t.Fatal(errorName(err, "error"))
}
}
func TestUnnamedError(t *testing.T) {
var err error = errors.New("foo")
if "error" != errorName(err, "error") {
t.Fatal(errorName(err, "error"))
}
}
func TestUnnamedHTTPEquivError(t *testing.T) {
var err error = OK{errors.New("foo")}
if "tigertonic.OK" != errorName(err, "error") {
t.Fatal(errorName(err, "error"))
}
}
func TestUnnamedSnakeCaseHTTPEquivError(t *testing.T) {
SnakeCaseHTTPEquivErrors = true
defer func() { SnakeCaseHTTPEquivErrors = false }()
var err error = OK{errors.New("foo")}
if "ok" != errorName(err, "error") {
t.Fatal(errorName(err, "error"))
}
}
func TestErrorWriter_DefaultWriter_StandardError(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("POST", "http://example.com/foo", bytes.NewBufferString("{ }"))
r.Header.Set("Accept", "application/json")
r.Header.Set("Content-Type", "application/json")
Marshaled(func(u *url.URL, h http.Header, rq *testRequest) (int, http.Header, *testResponse, error) {
return http.StatusBadRequest, nil, nil, errors.New("TestError")
}).ServeHTTP(w, r)
if http.StatusBadRequest != w.StatusCode {
t.Fatal(w.StatusCode)
}
//log.Printf("Return: %s", w.Body.String())
if "{\"description\":\"TestError\",\"error\":\"error\"}\n" != w.Body.String() {
t.Fatal(w.Body.String())
}
}
func TestErrorWriter_DefaultWriter_CustomError(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("POST", "http://example.com/foo", bytes.NewBufferString("{ }"))
r.Header.Set("Accept", "application/json")
r.Header.Set("Content-Type", "application/json")
Marshaled(func(u *url.URL, h http.Header, rq *testRequest) (int, http.Header, *testResponse, error) {
testErr := TestError{Code: 1, Message: "This is a test"}
return http.StatusBadRequest, nil, nil, testErr
}).ServeHTTP(w, r)
if http.StatusBadRequest != w.StatusCode {
t.Fatal(w.StatusCode)
}
//log.Printf("Return: %s", w.Body.String())
if "{\"description\":\"Code: [1] Message: [This is a test]\",\"error\":\"tigertonic.TestError\"}\n" != w.Body.String() {
t.Fatal(w.Body.String())
}
}
func TestErrorWriter_TestWriter_StandardError(t *testing.T) {
ResponseErrorWriter = TestErrorWriter{}
w := &testResponseWriter{}
r, _ := http.NewRequest("POST", "http://example.com/foo", bytes.NewBufferString("{ }"))
r.Header.Set("Accept", "application/json")
r.Header.Set("Content-Type", "application/json")
Marshaled(func(u *url.URL, h http.Header, rq *testRequest) (int, http.Header, *testResponse, error) {
return http.StatusBadRequest, nil, nil, errors.New("TestError")
}).ServeHTTP(w, r)
if http.StatusBadRequest != w.StatusCode {
t.Fatal(w.StatusCode)
}
//log.Printf("Return: %s", w.Body.String())
if "{\"description\":\"TestError\",\"error\":\"error\"}\n" != w.Body.String() {
t.Fatal(w.Body.String())
}
// Reset error writer after test...
ResponseErrorWriter = defaultErrorWriter{}
}
func TestErrorWriter_TestWriter_CustomError(t *testing.T) {
ResponseErrorWriter = TestErrorWriter{}
w := &testResponseWriter{}
r, _ := http.NewRequest("POST", "http://example.com/foo", bytes.NewBufferString("{ }"))
r.Header.Set("Accept", "application/json")
r.Header.Set("Content-Type", "application/json")
Marshaled(func(u *url.URL, h http.Header, rq *testRequest) (int, http.Header, *testResponse, error) {
testErr := TestError{Code: 1, Message: "This is a test"}
return http.StatusBadRequest, nil, nil, testErr
}).ServeHTTP(w, r)
if http.StatusBadRequest != w.StatusCode {
t.Fatal(w.StatusCode)
}
//log.Printf("Return: %s", w.Body.String())
if "{\"Err\":{\"Code\":1,\"Message\":\"This is a test\"}}\n" != w.Body.String() {
t.Fatal(w.Body.String())
}
// Reset error writer after test...
ResponseErrorWriter = defaultErrorWriter{}
}
// TestError is example error
type TestError struct {
// Satisfy the generic error interface.
error
// Classification of error
Code int
// Detailed information about error
Message string
}
// Error returns the string representation of the error.
func (e TestError) Error() string {
return fmt.Sprintf("Code: [%v] Message: [%s]", e.Code, e.Message)
}
// String returns the string representation of the error.
func (e TestError) String() string {
return e.Error()
}
type TestErrorWriter struct {
}
func (d TestErrorWriter) WriteError(r *http.Request, w http.ResponseWriter, err error) {
if acceptJSON(r) {
d.WriteJSONError(w, err)
} else {
d.WritePlaintextError(w, err)
}
}
func (d TestErrorWriter) WriteJSONError(w http.ResponseWriter, err error) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(errorStatusCode(err))
// Example of custom error formatting based on error type
name := errorName(err, "error")
if name == "tigertonic.TestError" {
if jsonErr := json.NewEncoder(w).Encode(err); nil != jsonErr {
log.Printf("Error marshalling error response into JSON output: %s", jsonErr)
}
} else {
if jsonErr := json.NewEncoder(w).Encode(map[string]string{
"description": err.Error(),
"error": errorName(err, "error"),
}); nil != jsonErr {
log.Printf("Error marshalling error response into JSON output: %s", jsonErr)
}
}
}
func (d TestErrorWriter) WritePlaintextError(w http.ResponseWriter, err error) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(errorStatusCode(err))
fmt.Fprintf(w, "%s: %s", errorName(err, "error"), err)
}