github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/znet/bind_router_test.go (about) 1 package znet 2 3 import ( 4 "errors" 5 "fmt" 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 10 "github.com/sohaha/zlsgo" 11 "github.com/sohaha/zlsgo/ztype" 12 ) 13 14 type testErrController struct { 15 } 16 17 func (t *testErrController) Init(e *Engine) error { 18 return errors.New("test error") 19 } 20 21 type testController struct { 22 } 23 24 func (t *testController) Init(e *Engine) { 25 e.Log.Debug("initialization") 26 } 27 28 func (t *testController) GETUser(_ *Context) { 29 30 } 31 32 func (t *testController) GETGetUser(_ *Context) { 33 34 } 35 36 func (t *testController) POSTUserInfo(_ *Context) { 37 38 } 39 40 func (t *testController) PUTUserInfo(_ *Context) { 41 42 } 43 44 func (t *testController) DELETEUserInfo(_ *Context) { 45 46 } 47 48 func (t *testController) PATCHUserInfo(_ *Context) { 49 50 } 51 52 func (t *testController) HEADUserInfo(_ *Context) { 53 54 } 55 56 func (t *testController) OPTIONSUserInfo(_ *Context) { 57 58 } 59 60 func (t *testController) AnyOk(c *Context) error { 61 fmt.Println(c.Request.Method) 62 return errors.New("ok") 63 } 64 65 func (t *testController) IDGET(_ *Context) { 66 67 } 68 69 func (t *testController) IDGETUser(_ *Context) { 70 71 } 72 73 func (t *testController) FullGETFile(_ *Context) { 74 75 } 76 77 func TestBindStruct(t *testing.T) { 78 tt := zlsgo.NewTest(t) 79 r := newServer() 80 r.PanicHandler(func(c *Context, err error) { 81 t.Log("PanicHandler", err) 82 }) 83 prefix := "/test" 84 err := r.BindStruct(prefix, &testErrController{}) 85 tt.Equal("test error", err.Error()) 86 87 err = r.BindStruct(prefix, &testController{}, func(c *Context) { 88 t.Log("go", c.Request.URL) 89 t.Log(c.GetAllParam()) 90 c.Next() 91 }) 92 t.Log(err) 93 tt.EqualNil(err) 94 r.BindStructDelimiter = "" 95 r.BindStructSuffix = ".go" 96 err = r.BindStruct(prefix, &testController{}, func(c *Context) { 97 t.Log("go", c.Request.URL) 98 t.Log(c.GetAllParam()) 99 c.Next() 100 }) 101 tt.Log(err) 102 tt.EqualNil(err) 103 methods := [][]string{ 104 {"GET", prefix + "/user"}, 105 {"GET", prefix + "/get-user"}, 106 {"POST", prefix + "/ok", "500"}, 107 {"POST", prefix + "/user-info"}, 108 {"PUT", prefix + "/user-info"}, 109 {"DELETE", prefix + "/user-info"}, 110 {"PATCH", prefix + "/user-info"}, 111 {"OPTIONS", prefix + "/user-info"}, 112 {"POST", prefix + "/UserInfo.go"}, 113 {"GET", prefix + "/user/233"}, 114 {"GET", prefix + "/User/233"}, 115 {"GET", prefix + "/File/File233"}, 116 {"GET", prefix + "/file/File233"}, 117 {"GET", prefix + "/ok", "500"}, 118 } 119 for _, v := range methods { 120 w := httptest.NewRecorder() 121 req, _ := http.NewRequest(v[0], v[1], nil) 122 r.ServeHTTP(w, req) 123 code := 200 124 if len(v) > 2 { 125 code = ztype.ToInt(v[2]) 126 } 127 tt.Equal(code, w.Code) 128 t.Log("Test:", v[0], v[1]) 129 t.Log(w.Code, w.Body.String()) 130 } 131 132 err = r.BindStruct(prefix, nil) 133 tt.Log(err) 134 } 135 136 func TestBindStructCase(t *testing.T) { 137 tt := zlsgo.NewTest(t) 138 r := newServer() 139 r.BindStructCase = func(s string) string { 140 tt.Log(s) 141 if s == "UserInfo" { 142 return "new-user-info" 143 } 144 return s 145 } 146 err := r.BindStruct("BindStructCase", &testController{}, func(c *Context) { 147 t.Log("go", c.Request.URL) 148 t.Log(c.GetAllParam()) 149 c.Next() 150 }) 151 tt.NoError(err) 152 153 methods := [][]string{ 154 {"POST", "/BindStructCase/new-user-info.go"}, 155 } 156 for _, v := range methods { 157 w := httptest.NewRecorder() 158 req, _ := http.NewRequest(v[0], v[1], nil) 159 r.ServeHTTP(w, req) 160 code := 200 161 if len(v) > 2 { 162 code = ztype.ToInt(v[2]) 163 } 164 tt.Equal(code, w.Code) 165 t.Log("Test:", v[0], v[1]) 166 t.Log(w.Code, w.Body.String()) 167 } 168 }