-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroute_group_test.go
148 lines (141 loc) · 4.01 KB
/
route_group_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
package nine
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/i9si-sistemas/assert"
)
func TestRouteGroup(t *testing.T) {
testServer := NewServer(8080)
type Account struct {
Name string `json:"name"`
Age int `json:"age"`
}
accounts := make(GenericJSON[string, Account], 0)
testServer.Route("/account", func(router *RouteGroup) {
router.Post("/create", func(c *Context) error {
var body Account
if err := Body(c.Request, &body); err != nil {
return c.Status(http.StatusBadRequest).JSON(JSON{
"message": "invalid body",
})
}
_, ok := accounts[body.Name]
response := JSON{"created": !ok}
if !ok {
accounts[body.Name] = body
return c.Status(http.StatusCreated).JSON(response)
}
return c.JSON(response)
})
router.Get("/:name", func(c *Context) error {
acc, ok := accounts[c.Param("name")]
if !ok {
return c.SendStatus(http.StatusNotFound)
}
return c.JSON(JSON{
"account": acc,
})
})
})
assertGroupEndpoints(t, testServer)
req := httptest.NewRequest(http.MethodGet, "/account/Gabriel%20Luiz", nil)
w := testServer.Test().Request(req)
assert.Equal(t, w.Result().StatusCode, http.StatusOK)
b := w.Body.Bytes()
var account struct {
Account `json:"account"`
}
err := DecodeJSON(b, &account)
assert.NoError(t, err)
assert.Equal(t, account.Name, "Gabriel Luiz")
assert.Equal(t, account.Age, 23)
}
func TestGroup(t *testing.T) {
testServer := NewServer(5024)
type Account struct {
Name string `json:"name"`
Age int `json:"age"`
Money int64 `json:"money"`
}
accounts := make(GenericJSON[string, Account], 0)
accountGroup := testServer.Group("/account")
accountGroup.Post("/create", func(c *Context) error {
var body Account
if err := Body(c.Request, &body); err != nil {
return c.Status(http.StatusBadRequest).JSON(JSON{
"message": "invalid body",
})
}
_, ok := accounts[body.Name]
response := JSON{"created": !ok}
if !ok {
accounts[body.Name] = body
return c.Status(http.StatusCreated).JSON(response)
}
return c.JSON(response)
})
profileGroup := accountGroup.Group("/profile")
profileGroup.Get("/:name", func(c *Context) error {
acc, ok := accounts[c.Param("name")]
if !ok {
return c.SendStatus(http.StatusNotFound)
}
return c.JSON(JSON{
"account": acc,
})
})
profileGroup.Route("/photo", func(router *RouteGroup) {
router.Get("/:name", func(c *Context) error {
return c.Send([]byte(c.Param("name")))
})
})
assertGroupEndpoints(t, testServer)
req := httptest.NewRequest(http.MethodGet, "/account/profile/Gabriel%20Luiz", nil)
w := testServer.Test().Request(req)
assert.Equal(t, w.Result().StatusCode, http.StatusOK)
b := w.Body.Bytes()
var account struct {
Account `json:"account"`
}
err := DecodeJSON(b, &account)
assert.NoError(t, err)
assert.Equal(t, account.Name, "Gabriel Luiz")
assert.Equal(t, account.Age, 23)
assert.Equal(t, account.Money, int64(5000))
req = httptest.NewRequest(http.MethodGet, "/account/profile/photo/gopher", nil)
w = testServer.Test().Request(req)
assert.Equal(t, w.Result().StatusCode, http.StatusOK)
assert.Equal(t, w.Body.Bytes(), []byte("gopher"))
}
func assertGroupEndpoints(t assert.T, testServer *Server) {
var response struct {
Created bool `json:"created"`
}
payload, err := JSON{
"name": "Gabriel Luiz",
"age": 23,
"money": 5000,
}.Buffer()
assert.NoError(t, err)
req := httptest.NewRequest(http.MethodPost, "/account/create", payload)
w := testServer.Test().Request(req)
assert.Equal(t, w.Result().StatusCode, http.StatusCreated)
b := w.Body.Bytes()
err = DecodeJSON(b, &response)
assert.NoError(t, err)
assert.True(t, response.Created)
payload, err = JSON{
"name": "Gabriel Luiz",
"age": 23,
"money": 5000000,
}.Buffer()
assert.NoError(t, err)
req = httptest.NewRequest(http.MethodPost, "/account/create", payload)
w = testServer.Test().Request(req)
assert.Equal(t, w.Result().StatusCode, http.StatusOK)
b = w.Body.Bytes()
err = DecodeJSON(b, &response)
assert.NoError(t, err)
assert.False(t, response.Created)
}