-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_test.go
130 lines (99 loc) · 3.83 KB
/
api_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
package rest
import (
"net/http"
"net/http/httptest"
"testing"
)
var _api *api
var _router *router
var _requestHandler *requestHandler
var task = func(ctx Context) {}
var errTask = func(err error, ctx Context) {}
func init() {
_router = new(router)
_requestHandler = &requestHandler{
router: _router,
}
_api = &api{
prefix: trim("/"),
router: _router,
requestHandler: _requestHandler,
}
}
func TestApi_Use(t *testing.T) {
_api.Use(task)
if !(len(_router.middlewares) == 1 && len(_router.routes) == 0 && len(_router.exceptions) == 0 && _router.uncaughtException == nil) {
t.Error("api.Use should add middleware in the router")
}
if _router.middlewares[0].pattern == nil || _router.middlewares[0].task == nil {
t.Error("api.Use should add type of middleware with pattern and task")
}
}
func TestApi_Get(t *testing.T) {
_api.Get("/", task)
if !(len(_router.middlewares) == 1 && len(_router.routes) == 1 && len(_router.exceptions) == 0 && _router.uncaughtException == nil) {
t.Error("api.Get should add route in the router")
}
if _router.routes[0].method != http.MethodGet || _router.routes[0].pattern == nil || _router.routes[0].task == nil {
t.Error("api.Get should add type of route with method, pattern and task")
}
}
func TestApi_Post(t *testing.T) {
_api.Post("/", task)
if !(len(_router.middlewares) == 1 && len(_router.routes) == 2 && len(_router.exceptions) == 0 && _router.uncaughtException == nil) {
t.Error("api.Post should add route in the router")
}
if _router.routes[1].method != http.MethodPost || _router.routes[1].pattern == nil || _router.routes[1].task == nil {
t.Error("api.Post should add type of route with method, pattern and task")
}
}
func TestApi_Put(t *testing.T) {
_api.Put("/", task)
if !(len(_router.middlewares) == 1 && len(_router.routes) == 3 && len(_router.exceptions) == 0 && _router.uncaughtException == nil) {
t.Error("api.Put should add route in the router")
}
if _router.routes[2].method != http.MethodPut || _router.routes[2].pattern == nil || _router.routes[2].task == nil {
t.Error("api.Put should add type of route with method, pattern and task")
}
}
func TestApi_Delete(t *testing.T) {
_api.Delete("/", task)
if !(len(_router.middlewares) == 1 && len(_router.routes) == 4 && len(_router.exceptions) == 0 && _router.uncaughtException == nil) {
t.Error("api.Delete should add route in the router")
}
if _router.routes[3].method != http.MethodDelete || _router.routes[3].pattern == nil || _router.routes[3].task == nil {
t.Error("api.Delete should add type of route with method, pattern and task")
}
}
func TestApi_Group(t *testing.T) {
user := _api.Router("/user")
if user == nil {
t.Error("api.Group should return api reference")
}
}
func TestApi_Exception(t *testing.T) {
_api.CatchError(ErrCodeNotFound, errTask)
if !(len(_router.middlewares) == 1 && len(_router.routes) == 4 && len(_router.exceptions) == 1 && _router.uncaughtException == nil) {
t.Error("api.Exception should add exception handler in the router")
}
if _router.exceptions[0].code != ErrCodeNotFound || _router.exceptions[0].task == nil {
t.Error("api.OnError should add type of exception with code and task")
}
}
func TestApi_UncaughtException(t *testing.T) {
_api.UncaughtException(errTask)
if !(len(_router.middlewares) == 1 && len(_router.routes) == 4 && len(_router.exceptions) == 1 && _router.uncaughtException != nil) {
t.Error("api.OnUncaughtException should add uncaught exception handler in the router")
}
if _router.uncaughtException == nil {
t.Error("api.OnUncaughtException should add type of uncaughtException with task")
}
}
func TestApi_ServeHTTP(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
_api.ServeHTTP(w, r)
if w.Result().StatusCode != 200 {
t.Error("api.ServeHTTP should respond on every request")
}
}