-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_middleware_test.go
More file actions
44 lines (34 loc) · 1.16 KB
/
error_middleware_test.go
File metadata and controls
44 lines (34 loc) · 1.16 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
package gomiddlewares
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestErrorResponseWithDefaultErrorDescription(t *testing.T) {
req, err := http.NewRequest("GET", "/test", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := ErrorHandlerMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
panic(errors.New("error description"))
}))
handler.ServeHTTP(rr, req)
assert.Contains(t, rr.Body.String(), "{\"status\":500,\"message\":\"Internal Server Error\"}\n")
assert.Equal(t, http.StatusInternalServerError, rr.Code)
}
func TestErrorResponseWithCustomErrorDescription(t *testing.T) {
req, err := http.NewRequest("GET", "/test", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := ErrorHandlerMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
panic(ErrorWithStatus{Message: "status bad gateway", Status: http.StatusBadGateway})
}))
handler.ServeHTTP(rr, req)
assert.Contains(t, rr.Body.String(), "{\"status\":502,\"message\":\"status bad gateway\"}\n")
assert.Equal(t, http.StatusBadGateway, rr.Code)
}