-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathhttperror_test.go
More file actions
186 lines (169 loc) · 4.51 KB
/
httperror_test.go
File metadata and controls
186 lines (169 loc) · 4.51 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
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
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors
package echo
import (
"errors"
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestHTTPError_StatusCode(t *testing.T) {
var err error = &HTTPError{Code: http.StatusBadRequest, Message: "my error message"}
code := 0
var sc HTTPStatusCoder
if errors.As(err, &sc) {
code = sc.StatusCode()
}
assert.Equal(t, http.StatusBadRequest, code)
}
func TestHTTPError_Error(t *testing.T) {
var testCases = []struct {
name string
error error
expect string
}{
{
name: "ok, without message",
error: &HTTPError{Code: http.StatusBadRequest},
expect: "code=400, message=Bad Request",
},
{
name: "ok, with message",
error: &HTTPError{Code: http.StatusBadRequest, Message: "my error message"},
expect: "code=400, message=my error message",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.expect, tc.error.Error())
})
}
}
func TestHTTPError_WrapUnwrap(t *testing.T) {
err := &HTTPError{Code: http.StatusBadRequest, Message: "bad"}
wrapped := err.Wrap(errors.New("my_error")).(*HTTPError)
err.Code = http.StatusOK
err.Message = "changed"
assert.Equal(t, http.StatusBadRequest, wrapped.Code)
assert.Equal(t, "bad", wrapped.Message)
assert.Equal(t, errors.New("my_error"), wrapped.Unwrap())
assert.Equal(t, "code=400, message=bad, err=my_error", wrapped.Error())
}
func TestNewHTTPError(t *testing.T) {
err := NewHTTPError(http.StatusBadRequest, "bad")
err2 := &HTTPError{Code: http.StatusBadRequest, Message: "bad"}
assert.Equal(t, err2, err)
}
func TestStatusCode(t *testing.T) {
var testCases = []struct {
name string
err error
expect int
}{
{
name: "ok, HTTPError",
err: &HTTPError{Code: http.StatusNotFound},
expect: http.StatusNotFound,
},
{
name: "ok, sentinel error",
err: ErrNotFound,
expect: http.StatusNotFound,
},
{
name: "ok, wrapped HTTPError",
err: fmt.Errorf("wrapped: %w", &HTTPError{Code: http.StatusTeapot}),
expect: http.StatusTeapot,
},
{
name: "nok, normal error",
err: errors.New("error"),
expect: 0,
},
{
name: "nok, nil",
err: nil,
expect: 0,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.expect, StatusCode(tc.err))
})
}
}
func TestResolveResponseStatus(t *testing.T) {
someErr := errors.New("some error")
var testCases = []struct {
name string
whenResp http.ResponseWriter
whenErr error
expectStatus int
expectResp bool
}{
{
name: "nil resp, nil err -> 200",
whenResp: nil,
whenErr: nil,
expectStatus: http.StatusOK,
expectResp: false,
},
{
name: "resp suggested status used when no error",
whenResp: &Response{Status: http.StatusCreated},
whenErr: nil,
expectStatus: http.StatusCreated,
expectResp: true,
},
{
name: "error overrides suggested status with StatusCode(err)",
whenResp: &Response{Status: http.StatusAccepted},
whenErr: ErrBadRequest,
expectStatus: http.StatusBadRequest,
expectResp: true,
},
{
name: "error overrides suggested status with 500 when StatusCode(err)==0",
whenResp: &Response{Status: http.StatusAccepted},
whenErr: ErrInternalServerError,
expectStatus: http.StatusInternalServerError,
expectResp: true,
},
{
name: "nil resp, error -> 500 when StatusCode(err)==0",
whenResp: nil,
whenErr: someErr,
expectStatus: http.StatusInternalServerError,
expectResp: false,
},
{
name: "committed response wins over error",
whenResp: &Response{Committed: true, Status: http.StatusNoContent},
whenErr: someErr,
expectStatus: http.StatusNoContent,
expectResp: true,
},
{
name: "committed response with status 0 falls back to 200 (defensive)",
whenResp: &Response{Committed: true, Status: 0},
whenErr: someErr,
expectStatus: http.StatusOK,
expectResp: true,
},
{
name: "resp with status 0 and no error -> 200",
whenResp: &Response{Status: 0},
whenErr: nil,
expectStatus: http.StatusOK,
expectResp: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
resp, status := ResolveResponseStatus(tc.whenResp, tc.whenErr)
assert.Equal(t, tc.expectResp, resp != nil)
assert.Equal(t, tc.expectStatus, status)
})
}
}