|
1 | 1 | package handler |
2 | | - |
3 | | -import ( |
4 | | - "bytes" |
5 | | - "encoding/json" |
6 | | - "errors" |
7 | | - "net/http" |
8 | | - "net/http/httptest" |
9 | | - "testing" |
10 | | - |
11 | | - "github.com/gin-gonic/gin" |
12 | | - t_http "github.com/muxi-Infra/muxi-micro/pkg/transport/http" |
13 | | - "github.com/stretchr/testify/assert" |
14 | | -) |
15 | | - |
16 | | -type demoReq struct { |
17 | | - Name string `json:"name" form:"name" binding:"required"` |
18 | | -} |
19 | | - |
20 | | -func handleDemoReq(c *gin.Context, r demoReq) { |
21 | | - HandleResponse(c, t_http.Response{ |
22 | | - HttpCode: http.StatusOK, |
23 | | - Code: 0, |
24 | | - Message: "ok", |
25 | | - Data: r, |
26 | | - }) |
27 | | -} |
28 | | - |
29 | | -func handleNoBody(c *gin.Context) { |
30 | | - HandleResponse(c, t_http.Response{ |
31 | | - HttpCode: http.StatusOK, |
32 | | - Code: 0, |
33 | | - Message: "pong", |
34 | | - }) |
35 | | -} |
36 | | - |
37 | | -func decodeResp[T any](body []byte) (T, error) { |
38 | | - var resp t_http.FinalResp |
39 | | - var t T |
40 | | - err := json.Unmarshal(body, &resp) |
41 | | - if err != nil { |
42 | | - return t, err |
43 | | - } |
44 | | - db, _ := json.Marshal(resp.Data) |
45 | | - err = json.Unmarshal(db, &t) |
46 | | - return t, err |
47 | | -} |
48 | | - |
49 | | -func setErrorHandlerFunc() gin.HandlerFunc { |
50 | | - return func(c *gin.Context) { |
51 | | - c.Error(errors.New("test")) |
52 | | - } |
53 | | -} |
54 | | - |
55 | | -func TestWrapReq(t *testing.T) { |
56 | | - gin.SetMode(gin.TestMode) |
57 | | - g := gin.New() |
58 | | - g.POST("/demo", WrapReq(handleDemoReq)) |
59 | | - |
60 | | - tests := []struct { |
61 | | - name string |
62 | | - body string |
63 | | - status int |
64 | | - expect string |
65 | | - }{ |
66 | | - {"非法参数", `{"foo":"bar"}`, http.StatusBadRequest, ""}, |
67 | | - {"正常参数", `{"name":"bar"}`, http.StatusOK, "bar"}, |
68 | | - } |
69 | | - |
70 | | - for _, tt := range tests { |
71 | | - t.Run(tt.name, func(t *testing.T) { |
72 | | - w := httptest.NewRecorder() |
73 | | - req, _ := http.NewRequest(http.MethodPost, "/demo", bytes.NewBufferString(tt.body)) |
74 | | - req.Header.Set("Content-Type", "application/json") |
75 | | - g.ServeHTTP(w, req) |
76 | | - |
77 | | - assert.Equal(t, tt.status, w.Code) |
78 | | - if tt.status == http.StatusOK { |
79 | | - res, err := decodeResp[demoReq](w.Body.Bytes()) |
80 | | - assert.NoError(t, err) |
81 | | - assert.Equal(t, tt.expect, res.Name) |
82 | | - } |
83 | | - }) |
84 | | - } |
85 | | - |
86 | | - t.Run("error in front Middleware", func(t *testing.T) { |
87 | | - g.GET("/testFrontError", setErrorHandlerFunc(), WrapReq(handleDemoReq)) |
88 | | - w := httptest.NewRecorder() |
89 | | - req, _ := http.NewRequest(http.MethodGet, "/testFrontError", bytes.NewBufferString("{\"name\":\"bar\"}")) |
90 | | - req.Header.Set("Content-Type", "application/json") |
91 | | - g.ServeHTTP(w, req) |
92 | | - |
93 | | - assert.Equal(t, http.StatusOK, w.Code) |
94 | | - |
95 | | - }) |
96 | | -} |
97 | | - |
98 | | -func TestWrap(t *testing.T) { |
99 | | - gin.SetMode(gin.TestMode) |
100 | | - g := gin.New() |
101 | | - |
102 | | - t.Run("请求正常", func(t *testing.T) { |
103 | | - g.GET("/ping", Wrap(handleNoBody)) |
104 | | - |
105 | | - w := httptest.NewRecorder() |
106 | | - req, _ := http.NewRequest(http.MethodGet, "/ping", nil) |
107 | | - g.ServeHTTP(w, req) |
108 | | - |
109 | | - assert.Equal(t, http.StatusOK, w.Code) |
110 | | - var resp t_http.FinalResp |
111 | | - _ = json.Unmarshal(w.Body.Bytes(), &resp) |
112 | | - assert.Equal(t, "pong", resp.Message) |
113 | | - }) |
114 | | - |
115 | | - t.Run("error in front Middleware", func(t *testing.T) { |
116 | | - g.GET("/testFrontError", setErrorHandlerFunc(), Wrap(handleNoBody)) |
117 | | - w := httptest.NewRecorder() |
118 | | - req, _ := http.NewRequest(http.MethodGet, "/testFrontError", bytes.NewBufferString("{\"name\":\"bar\"}")) |
119 | | - req.Header.Set("Content-Type", "application/json") |
120 | | - g.ServeHTTP(w, req) |
121 | | - |
122 | | - assert.Equal(t, http.StatusOK, w.Code) |
123 | | - |
124 | | - }) |
125 | | - |
126 | | -} |
0 commit comments