github.com/wfusion/gofusion@v1.1.14/test/http/cases/group_test.go (about) 1 package cases 2 3 import ( 4 "bytes" 5 "context" 6 "io" 7 "net/http" 8 "net/http/httptest" 9 "testing" 10 11 "github.com/gin-gonic/gin" 12 "github.com/stretchr/testify/suite" 13 14 "github.com/wfusion/gofusion/common/utils" 15 "github.com/wfusion/gofusion/log" 16 17 fusHtp "github.com/wfusion/gofusion/http" 18 testHtp "github.com/wfusion/gofusion/test/http" 19 ) 20 21 func TestGroup(t *testing.T) { 22 testingSuite := &Group{Test: new(testHtp.Test)} 23 testingSuite.Init(testingSuite) 24 suite.Run(t, testingSuite) 25 } 26 27 type Group struct { 28 *testHtp.Test 29 } 30 31 func (t *Group) BeforeTest(suiteName, testName string) { 32 t.Catch(func() { 33 log.Info(context.Background(), "right before %s %s", suiteName, testName) 34 }) 35 } 36 37 func (t *Group) AfterTest(suiteName, testName string) { 38 t.Catch(func() { 39 log.Info(context.Background(), "right after %s %s", suiteName, testName) 40 }) 41 } 42 43 func (t *Group) TestGroupDispatch() { 44 t.Catch(func() { 45 // Given 46 type reqStruct struct { 47 ID *string `json:"id" binding:"required"` 48 NumList []int `json:"num_list"` 49 Embed *struct { 50 FUID *string `json:"fuid"` 51 Boolean *bool `json:"boolean"` 52 } `json:"embed"` 53 } 54 method := http.MethodPost 55 path := "/test" 56 group := "/TestGroupDispatch" 57 hd := func(c *gin.Context, req *reqStruct) error { 58 t.Require().NotNil(req) 59 t.Require().NotEmpty(req.ID) 60 t.Require().NotEmpty(req.NumList) 61 t.Require().NotEmpty(req.Embed) 62 t.Require().NotEmpty(req.Embed.FUID) 63 t.Require().NotNil(req.Embed.Boolean) 64 return io.ErrUnexpectedEOF 65 } 66 reqBody := bytes.NewReader(utils.MustJsonMarshal(&reqStruct{ 67 ID: utils.AnyPtr(utils.UUID()), 68 NumList: []int{1, 2, 3, 4, 5, 6}, 69 Embed: &struct { 70 FUID *string `json:"fuid"` 71 Boolean *bool `json:"boolean"` 72 }{ 73 FUID: utils.AnyPtr(utils.UUID()), 74 Boolean: utils.AnyPtr(true), 75 }, 76 })) 77 78 w := httptest.NewRecorder() 79 req, err := http.NewRequest(method, group+path, reqBody) 80 req.Header.Set("Content-Type", "application/json") 81 t.Require().NoError(err) 82 83 groupRouter := fusHtp.Use(fusHtp.AppName(t.AppName())).Group(group) 84 groupRouter.POST(path, hd) 85 86 // When 87 fusHtp.Use(fusHtp.AppName(t.AppName())).ServeHTTP(w, req) 88 89 // Then 90 t.Equal(http.StatusOK, w.Code) 91 t.Contains(w.Body.String(), io.ErrUnexpectedEOF.Error()) 92 }) 93 } 94 95 func (t *Group) TestUseDispatch() { 96 t.Catch(func() { 97 // Given 98 type reqStruct struct { 99 ID *string `json:"id" binding:"required"` 100 NumList []int `json:"num_list"` 101 Embed *struct { 102 FUID *string `json:"fuid"` 103 Boolean *bool `json:"boolean"` 104 } `json:"embed"` 105 } 106 method := http.MethodPost 107 path := "/test" 108 group := "/TestUseDispatch" 109 hd := func(c *gin.Context, req *reqStruct) error { 110 t.Require().NotNil(req) 111 t.Require().NotEmpty(req.ID) 112 t.Require().NotEmpty(req.NumList) 113 t.Require().NotEmpty(req.Embed) 114 t.Require().NotEmpty(req.Embed.FUID) 115 t.Require().NotNil(req.Embed.Boolean) 116 return io.ErrUnexpectedEOF 117 } 118 reqBody := bytes.NewReader(utils.MustJsonMarshal(&reqStruct{ 119 ID: utils.AnyPtr(utils.UUID()), 120 NumList: []int{1, 2, 3, 4, 5, 6}, 121 Embed: &struct { 122 FUID *string `json:"fuid"` 123 Boolean *bool `json:"boolean"` 124 }{ 125 FUID: utils.AnyPtr(utils.UUID()), 126 Boolean: utils.AnyPtr(true), 127 }, 128 })) 129 130 w := httptest.NewRecorder() 131 req, err := http.NewRequest(method, group+path, reqBody) 132 req.Header.Set("Content-Type", "application/json") 133 t.Require().NoError(err) 134 135 cnt := 0 136 groupRouter := fusHtp.Use(fusHtp.AppName(t.AppName())).Group(group).Use(func(c *gin.Context) { cnt++ }) 137 groupRouter.POST(path, hd) 138 139 // When 140 fusHtp.Use(fusHtp.AppName(t.AppName())).ServeHTTP(w, req) 141 142 // Then 143 t.EqualValues(cnt, 1) 144 t.Equal(http.StatusOK, w.Code) 145 t.Contains(w.Body.String(), io.ErrUnexpectedEOF.Error()) 146 }) 147 }