-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroute.go
107 lines (91 loc) · 2.8 KB
/
route.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
package tests
import (
"fmt"
"gitee.com/falling-ts/gower/app/http/requests"
"gitee.com/falling-ts/gower/services"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
)
type data map[string]any
func TestRoute(t *testing.T) {
fmt.Println("----------------TestResponse 开始----------------")
assert := getAssert(t)
var (
req *http.Request
w *httptest.ResponseRecorder
)
route.GET("/test01", func(c *gin.Context) {
c.JSON(http.StatusOK, data{
"id": "1",
})
})
req = httptest.NewRequest(http.MethodGet, "/test01", nil)
w = httptest.NewRecorder()
route.ServeHTTP(w, req)
assert.Equal(http.StatusOK, w.Code)
route.GET("/test02", func(c *gin.Context) error {
c.JSON(http.StatusOK, data{
"id": "1",
})
return nil
})
req = httptest.NewRequest(http.MethodGet, "/test02", nil)
w = httptest.NewRecorder()
route.ServeHTTP(w, req)
assert.Equal(http.StatusOK, w.Code)
route.GET("/test03", func(c *gin.Context) services.Response {
return res.Ok("请求成功")
})
req = httptest.NewRequest(http.MethodGet, "/test03", nil)
w = httptest.NewRecorder()
route.ServeHTTP(w, req)
assert.Equal(http.StatusOK, w.Code)
route.GET("/test04", func(*gin.Context) (services.Response, error) {
return res.Ok("请求成功"), nil
})
req = httptest.NewRequest(http.MethodGet, "/test04", nil)
w = httptest.NewRecorder()
route.ServeHTTP(w, req)
assert.Equal(http.StatusOK, w.Code)
route.GET("/test05", func() error {
return nil
})
req = httptest.NewRequest(http.MethodGet, "/test05", nil)
w = httptest.NewRecorder()
route.ServeHTTP(w, req)
assert.Equal(http.StatusOK, w.Code)
route.GET("/test06", func() services.Response {
return res.Ok("请求成功")
})
req = httptest.NewRequest(http.MethodGet, "/test06", nil)
w = httptest.NewRecorder()
route.ServeHTTP(w, req)
assert.Equal(http.StatusOK, w.Code)
route.GET("/test07", func() (services.Response, error) {
return res.Ok("请求成功"), nil
})
req = httptest.NewRequest(http.MethodGet, "/test07", nil)
w = httptest.NewRecorder()
route.ServeHTTP(w, req)
assert.Equal(http.StatusOK, w.Code)
route.GET("/test08", "请求成功")
req = httptest.NewRequest(http.MethodGet, "/test08", nil)
w = httptest.NewRecorder()
route.ServeHTTP(w, req)
assert.Equal(http.StatusOK, w.Code)
route.GET("/test09", []any{"请求成功", data{"id": 1}})
req = httptest.NewRequest(http.MethodGet, "/test09", nil)
w = httptest.NewRecorder()
route.ServeHTTP(w, req)
assert.Equal(http.StatusOK, w.Code)
route.GET("/test10", func(req *requests.TestRequest) (string, any) {
return "请求成功", data{"test": req.Test}
})
req = httptest.NewRequest(http.MethodGet, "/test10?test=hello", nil)
w = httptest.NewRecorder()
route.ServeHTTP(w, req)
assert.Equal(http.StatusOK, w.Code)
fmt.Println("----------------TestResponse 结束----------------")
}