-
Notifications
You must be signed in to change notification settings - Fork 1
/
ctx_test.go
133 lines (118 loc) · 4.55 KB
/
ctx_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
package ctx_test
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo"
"github.com/stretchr/testify/assert"
"github.com/cutedogspark/echo-custom-context"
)
func TestCustomCtx(t *testing.T) {
tt := []struct {
name string
givenHandler func(c echo.Context) error
wantJSON string
}{
{
name: "200",
givenHandler: func(c echo.Context) error {
return c.(ctx.CustomCtx).Resp(http.StatusOK).Data("hello world").Do()
},
wantJSON: `{"apiVersion": "1.0", "data": "hello world"}`,
},
{
name: "200 with google json style",
givenHandler: func(c echo.Context) error {
return c.(ctx.CustomCtx).GResp(http.StatusOK).Data("hello world").Out()
},
wantJSON: `{"apiVersion": "1.0", "data": "hello world"}`,
},
{
name: "400 with google json style",
givenHandler: func(c echo.Context) error {
gerrs := ctx.NewGErrors().Append(&ctx.GError{
Code: 40000001,
Domain: "Calendar",
Reason: "ResourceNotFoundException",
Message: "Resources is not exist",
LocationType: "database query",
Location: "query",
ExtendedHelp: "http://help-link",
SendReport: "http://report.dajui.com/",
}).Append(&ctx.GError{
Code: 40000002,
Domain: "global",
Reason: "required",
Message: "Required parameter: part",
LocationType: "parameter",
Location: "part",
})
return c.(ctx.CustomCtx).GResp().Errors(*gerrs...).Out()
},
wantJSON: `{"apiVersion":"1.0","error":{"code":40000001,"message":"Resources is not exist","errors":[{"code": 40000001, "extendedHelp":"http://help-link", "sendReport":"http://report.dajui.com/", "domain":"Calendar", "reason":"ResourceNotFoundException", "message":"Resources is not exist", "location":"query", "locationType":"database query"},{"code": 40000002, "message":"Required parameter: part", "location":"part", "locationType":"parameter", "domain":"global", "reason":"required"}]}}`,
},
{
name: "400 with string errors",
givenHandler: func(c echo.Context) error {
errCode := 40000001
errMsg := "Error Title"
errDate := ctx.NewErrors()
errDate.Add("Error Message 1")
errDate.Add("Error Message 2")
return c.(ctx.CustomCtx).Resp(errCode).Error(fmt.Sprintf("%v", errMsg)).Code(errCode).Errors(errDate.Error()).Do()
},
wantJSON: `{"apiVersion":"1.0","error":{"code":40000001,"message":"Error Title","errors":["Error Message 1","Error Message 2"]}}`,
},
{
name: "400 with custom struct",
givenHandler: func(c echo.Context) error {
errs := []interface{}{}
errs = append(errs, struct {
Name string `json:"name"`
}{"peter"})
return c.(ctx.CustomCtx).Resp(http.StatusOK).Error("this is error message").Errors(errs).Do()
},
wantJSON: `{"apiVersion":"1.0","error":{"code":0, "message":"this is error message", "errors":[{"name":"peter"}]}}`,
},
{
name: "append domain ",
givenHandler: func(c echo.Context) error {
gerrs := ctx.NewGErrors().Append(&ctx.GError{
Code: 40000001,
Domain: "Calendar",
Reason: "ResourceNotFoundException",
Message: "Resources is not exist",
LocationType: "database query",
Location: "query",
ExtendedHelp: "http://help-link",
SendReport: "http://report.dajui.com/",
}).Append(&ctx.GError{
Code: 40000002,
Domain: "global",
Reason: "required",
Message: "Required parameter: part",
LocationType: "parameter",
Location: "part",
})
gerrs.AppendDomain("handler")
return c.(ctx.CustomCtx).GResp().Errors(*gerrs...).Out()
},
wantJSON: `{"apiVersion":"1.0","error":{"code":40000001,"message":"Resources is not exist","errors":[{"code": 40000001, "extendedHelp":"http://help-link", "sendReport":"http://report.dajui.com/", "domain":"handler.Calendar", "reason":"ResourceNotFoundException", "message":"Resources is not exist", "location":"query", "locationType":"database query"},{"code": 40000002, "message":"Required parameter: part", "location":"part", "locationType":"parameter", "domain":"handler.global", "reason":"required"}]}}`,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
return next(ctx.CustomCtx{c})
}
}(tc.givenHandler)(c)
assert.JSONEq(t, tc.wantJSON, rec.Body.String())
})
}
}