-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem_details_test.go
278 lines (218 loc) · 7.92 KB
/
problem_details_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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package problem
import (
"errors"
"github.com/gin-gonic/gin"
"github.com/labstack/echo/v4"
custom_errors "github.com/meysamhadeli/problem-details/samples/custom-errors"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
func TestMap_CustomType_Echo(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "http://echo_endpoint1", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
err := echo_endpoint1(c)
Map[custom_errors.BadRequestError](func() ProblemDetailErr {
return &ProblemDetail{
Status: http.StatusBadRequest,
Title: "bad-request",
Detail: err.Error(),
}
})
p, _ := ResolveProblemDetails(c.Response(), c.Request(), err)
assert.Equal(t, c.Response().Status, http.StatusBadRequest)
assert.Equal(t, err.Error(), p.GetDetails())
assert.Equal(t, "bad-request", p.GetTitle())
assert.Equal(t, "https://httpstatuses.io/400", p.GetType())
assert.Equal(t, http.StatusBadRequest, p.GetStatus())
}
func TestMap_Custom_Problem_Err_Echo(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "http://echo_endpoint4", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
err := echo_endpoint4(c)
Map[custom_errors.ConflictError](func() ProblemDetailErr {
return &CustomProblemDetailTest{
ProblemDetailErr: &ProblemDetail{
Status: http.StatusConflict,
Title: "conflict",
Detail: err.Error(),
},
AdditionalInfo: "some additional info...",
Description: "some description...",
}
})
p, _ := ResolveProblemDetails(c.Response(), c.Request(), err)
cp := p.(*CustomProblemDetailTest)
assert.Equal(t, c.Response().Status, http.StatusConflict)
assert.Equal(t, err.Error(), cp.GetDetails())
assert.Equal(t, "conflict", cp.GetTitle())
assert.Equal(t, "https://httpstatuses.io/409", cp.GetType())
assert.Equal(t, http.StatusConflict, cp.GetStatus())
assert.Equal(t, "some description...", cp.Description)
assert.Equal(t, "some additional info...", cp.AdditionalInfo)
}
func TestMap_Status_Echo(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "http://echo_endpoint2", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
err := echo_endpoint2(c)
MapStatus(http.StatusBadGateway, func() ProblemDetailErr {
return &ProblemDetail{
Status: http.StatusUnauthorized,
Title: "unauthorized",
Detail: err.Error(),
}
})
p, _ := ResolveProblemDetails(c.Response(), c.Request(), err)
assert.Equal(t, c.Response().Status, http.StatusUnauthorized)
assert.Equal(t, err.(*echo.HTTPError).Message.(error).Error(), p.GetDetails())
assert.Equal(t, "unauthorized", p.GetTitle())
assert.Equal(t, "https://httpstatuses.io/401", p.GetType())
assert.Equal(t, http.StatusUnauthorized, p.GetStatus())
}
func TestMap_Unhandled_Err_Echo(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "http://echo_endpoint3", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
err := echo_endpoint3(c)
p, _ := ResolveProblemDetails(c.Response(), c.Request(), err)
assert.Equal(t, c.Response().Status, http.StatusInternalServerError)
assert.Equal(t, err.Error(), p.GetDetails())
assert.Equal(t, "Internal Server Error", p.GetTitle())
assert.Equal(t, "https://httpstatuses.io/500", p.GetType())
assert.Equal(t, http.StatusInternalServerError, p.GetStatus())
}
func TestMap_CustomType_Gin(t *testing.T) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
r := gin.Default()
r.GET("/gin_endpoint1", func(ctx *gin.Context) {
err := errors.New("We have a custom type error in our endpoint")
customBadRequestError := custom_errors.BadRequestError{InternalError: err}
_ = c.Error(customBadRequestError)
})
req, _ := http.NewRequest(http.MethodGet, "/gin_endpoint1", nil)
r.ServeHTTP(w, req)
for _, err := range c.Errors {
Map[custom_errors.BadRequestError](func() ProblemDetailErr {
return &ProblemDetail{
Status: http.StatusBadRequest,
Title: "bad-request",
Detail: err.Error(),
}
})
p, _ := ResolveProblemDetails(w, req, err)
assert.Equal(t, http.StatusBadRequest, p.GetStatus())
assert.Equal(t, err.Error(), p.GetDetails())
assert.Equal(t, "bad-request", p.GetTitle())
assert.Equal(t, "https://httpstatuses.io/400", p.GetType())
}
}
func TestMap_Custom_Problem_Err_Gin(t *testing.T) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
r := gin.Default()
r.GET("/gin_endpoint4", func(ctx *gin.Context) {
err := errors.New("We have a custom error with custom problem details error in our endpoint")
customConflictError := custom_errors.ConflictError{InternalError: err}
_ = c.Error(customConflictError)
})
req, _ := http.NewRequest(http.MethodGet, "/gin_endpoint4", nil)
r.ServeHTTP(w, req)
for _, err := range c.Errors {
Map[custom_errors.ConflictError](func() ProblemDetailErr {
return &CustomProblemDetailTest{
ProblemDetailErr: &ProblemDetail{
Status: http.StatusConflict,
Title: "conflict",
Detail: err.Error(),
},
AdditionalInfo: "some additional info...",
Description: "some description...",
}
})
p, _ := ResolveProblemDetails(w, req, err)
cp := p.(*CustomProblemDetailTest)
assert.Equal(t, http.StatusConflict, cp.GetStatus())
assert.Equal(t, err.Error(), cp.GetDetails())
assert.Equal(t, "conflict", cp.GetTitle())
assert.Equal(t, "https://httpstatuses.io/409", cp.GetType())
assert.Equal(t, "some description...", cp.Description)
assert.Equal(t, "some additional info...", cp.AdditionalInfo)
}
}
func TestMap_Status_Gin(t *testing.T) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
r := gin.Default()
r.GET("/gin_endpoint2", func(ctx *gin.Context) {
err := errors.New("We have a specific status code error in our endpoint")
_ = c.AbortWithError(http.StatusBadGateway, err)
})
req, _ := http.NewRequest(http.MethodGet, "/gin_endpoint2", nil)
r.ServeHTTP(w, req)
for _, err := range c.Errors {
MapStatus(http.StatusBadGateway, func() ProblemDetailErr {
return &ProblemDetail{
Status: http.StatusUnauthorized,
Title: "unauthorized",
Detail: err.Error(),
}
})
p, _ := ResolveProblemDetails(w, req, err)
assert.Equal(t, http.StatusUnauthorized, p.GetStatus())
assert.Equal(t, err.Error(), p.GetDetails())
assert.Equal(t, "unauthorized", p.GetTitle())
assert.Equal(t, "https://httpstatuses.io/401", p.GetType())
}
}
func TestMap_Unhandled_Err_Gin(t *testing.T) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
r := gin.Default()
r.GET("/gin_endpoint3", func(ctx *gin.Context) {
err := errors.New("We have a unhandeled error in our endpoint")
_ = c.Error(err)
})
req, _ := http.NewRequest(http.MethodGet, "/gin_endpoint3", nil)
r.ServeHTTP(w, req)
for _, err := range c.Errors {
p, _ := ResolveProblemDetails(w, req, err)
assert.Equal(t, http.StatusInternalServerError, p.GetStatus())
assert.Equal(t, err.Error(), p.GetDetails())
assert.Equal(t, "Internal Server Error", p.GetTitle())
assert.Equal(t, "https://httpstatuses.io/500", p.GetType())
}
}
func echo_endpoint1(c echo.Context) error {
err := errors.New("We have a custom type error in our endpoint")
return custom_errors.BadRequestError{InternalError: err}
}
func echo_endpoint2(c echo.Context) error {
err := errors.New("We have a specific status code error in our endpoint")
return echo.NewHTTPError(http.StatusBadGateway, err)
}
func echo_endpoint3(c echo.Context) error {
err := errors.New("We have a unhandeled error in our endpoint")
return err
}
func echo_endpoint4(c echo.Context) error {
err := errors.New("We have a custom error with custom problem details error in our endpoint")
return custom_errors.ConflictError{InternalError: err}
}
type CustomProblemDetailTest struct {
ProblemDetailErr
Description string `json:"description,omitempty"`
AdditionalInfo string `json:"additionalInfo,omitempty"`
}