-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors_test.go
55 lines (41 loc) · 1.27 KB
/
errors_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
package dipra_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
. "github.com/didikprabowo/dipra"
"github.com/stretchr/testify/assert"
)
func TestError(t *testing.T) {
d := Default()
err := WrapError{
Code: http.StatusInternalServerError,
Message: http.StatusText(http.StatusInternalServerError),
}
assert.Equal(t, `Code : 500, detail : Internal Server Error`, err.Error())
jsonBytes, _ := json.Marshal(err)
assert.Equal(t, `{"code":500,"message":"Internal Server Error"}`, string(jsonBytes))
assert.Equal(t, err.String(), err.Message)
assert.NotNil(t, jsonBytes)
t.Run("default-error", func(t *testing.T) {
t.Run("404", func(t *testing.T) {
var err error = Err404
jsonBytes404, _ := json.Marshal(err)
r := httptest.NewRequest(http.MethodGet, "/no", nil)
resp := httptest.NewRecorder()
d.ServeHTTP(resp, r)
assert.Equal(t, `{"error":`+string(jsonBytes404)+`}`, resp.Body.String())
})
t.Run("500", func(t *testing.T) {
d.GET("/", func(c *Context) error {
return Err500
})
jsonBytes500, _ := json.Marshal(Err500)
r := httptest.NewRequest(http.MethodGet, "/", nil)
resp := httptest.NewRecorder()
d.ServeHTTP(resp, r)
assert.Equal(t, `{"error":`+string(jsonBytes500)+`}`, resp.Body.String())
})
})
}