-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathengine_test.go
110 lines (84 loc) · 2.63 KB
/
engine_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
package fox_test
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/fox-gonic/fox"
"github.com/fox-gonic/fox/httperrors"
)
type Foo struct {
A string
B string
}
func MiddlewareFailed(c *fox.Context) (res interface{}, err error) {
c.Logger.Info("MiddlewareFailed")
res = "Middleware"
err = httperrors.ErrInvalidArguments
return
}
func MiddlewareSuccess(c *fox.Context) (res interface{}, err error) {
c.Logger.Info("MiddlewareSuccess")
return
}
type TestInput struct {
Param string `uri:"param"`
Query string `query:"query"`
Body string `json:"body"`
}
func HandleSuccess(c *fox.Context, in TestInput) (res interface{}, err error) {
res = in
return
}
func HandleFailed(c *fox.Context, in *TestInput) (interface{}, error) {
err := &httperrors.Error{
HTTPCode: http.StatusBadRequest,
Code: httperrors.ErrInvalidArguments.Code,
}
_ = err.AddField("message", map[string]interface{}{
"param": "invalid param " + in.Param,
})
return nil, err
}
func Ping(c *fox.Context) (res interface{}, err error) {
c.Logger.Info("PingHandler")
res = Foo{"a", "b"}
return
}
func TestEngine(t *testing.T) {
assert := assert.New(t)
router := fox.New()
router.GET("ping", MiddlewareFailed, Ping)
router.GET("ping2", MiddlewareSuccess, Ping)
router.POST("/handle/:param/success", HandleSuccess)
router.POST("/handle/:param/failed", HandleFailed)
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/ping", nil)
router.ServeHTTP(w, req)
assert.Equal(http.StatusBadRequest, w.Code)
body := w.Body.String()
assert.Equal(`{"code":"INVALID_ARGUMENTS","error":"(400): invalid arguments","meta":"invalid arguments"}`, body)
w = httptest.NewRecorder()
req = httptest.NewRequest("GET", "/ping2", nil)
router.ServeHTTP(w, req)
assert.Equal(http.StatusOK, w.Code)
var response Foo
err := json.Unmarshal(w.Body.Bytes(), &response)
assert.Nil(err)
assert.Equal(response.A, "a")
assert.Equal(response.B, "b")
jsonData := map[string]string{"body": "fromBody"}
data, _ := json.Marshal(jsonData)
w = httptest.NewRecorder()
req = httptest.NewRequest("POST", "/handle/hello/success?query=world", bytes.NewReader(data))
router.ServeHTTP(w, req)
assert.Equal(http.StatusOK, w.Code)
assert.Equal(`{"Param":"hello","Query":"world","body":"fromBody"}`, w.Body.String())
w = httptest.NewRecorder()
req = httptest.NewRequest("POST", "/handle/hello/failed?query=world", bytes.NewReader(data))
router.ServeHTTP(w, req)
assert.Equal(http.StatusBadRequest, w.Code)
assert.Equal(`{"code":"INVALID_ARGUMENTS","message":{"param":"invalid param hello"}}`, w.Body.String())
}