github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/httptransport/z_route_handler_test.go (about) 1 package httptransport_test 2 3 import ( 4 "net/http" 5 "net/http/httputil" 6 "testing" 7 "time" 8 9 . "github.com/onsi/gomega" 10 11 . "github.com/machinefi/w3bstream/pkg/depends/kit/httptransport" 12 "github.com/machinefi/w3bstream/pkg/depends/kit/httptransport/mock" 13 "github.com/machinefi/w3bstream/pkg/depends/kit/kit" 14 "github.com/machinefi/w3bstream/pkg/depends/testutil/httptransporttestutil/server/cmd/app/routes" 15 ) 16 17 var ( 18 factory = NewRequestTsfmFactory(nil, nil) 19 meta = &ServiceMeta{Name: "service-test", Version: "1.0.0"} 20 ) 21 22 func init() { 23 factory.SetDefault() 24 } 25 26 func TestHttpRouteHandler(t *testing.T) { 27 t.Run("Redirect", func(t *testing.T) { 28 root := kit.NewRouter(Group("/root")) 29 root.Register(kit.NewRouter(routes.Redirect{})) 30 31 route := NewHttpRouteMeta(root.Routes()[0]) 32 hdl := NewRouteHandler(meta, route, factory) 33 34 operator := routes.Redirect{} 35 req, err := factory.NewRequest(operator.Method(), "/", operator) 36 NewWithT(t).Expect(err).To(BeNil()) 37 38 rw := mock.NewMockResponseWriter() 39 hdl.ServeHTTP(rw, req) 40 41 NewWithT(t).Expect(string(rw.MustDumpResponse())).To(Equal(`HTTP/0.0 302 Found 42 Content-Type: text/html; charset=utf-8 43 Location: /other 44 X-Meta: service-test@1.0.0/Redirect 45 46 <a href="/other">Found</a>. 47 48 `)) 49 }) 50 51 t.Run("RedirectWhenError", func(t *testing.T) { 52 root := kit.NewRouter(Group("/root")) 53 root.Register(kit.NewRouter(routes.RedirectWhenError{})) 54 55 route := NewHttpRouteMeta(root.Routes()[0]) 56 hdl := NewRouteHandler(meta, route, factory) 57 58 operator := routes.RedirectWhenError{} 59 req, err := factory.NewRequest(operator.Method(), "/", operator) 60 NewWithT(t).Expect(err).To(BeNil()) 61 62 rw := mock.NewMockResponseWriter() 63 hdl.ServeHTTP(rw, req) 64 65 NewWithT(t).Expect(string(rw.MustDumpResponse())).To(Equal(`HTTP/0.0 301 Moved Permanently 66 Location: /other 67 X-Meta: service-test@1.0.0/RedirectWhenError 68 Content-Length: 0 69 70 `)) 71 }) 72 73 t.Run("Cookies", func(t *testing.T) { 74 root := kit.NewRouter(Group("/root")) 75 root.Register(kit.NewRouter(&routes.Cookie{})) 76 77 route := NewHttpRouteMeta(root.Routes()[0]) 78 hdl := NewRouteHandler(meta, route, factory) 79 80 operator := routes.Cookie{} 81 req, err := factory.NewRequest(operator.Method(), "/", operator) 82 NewWithT(t).Expect(err).To(BeNil()) 83 84 cookie := &http.Cookie{ 85 Name: "token", 86 Value: "test", 87 Expires: time.Now().Add(24 * time.Hour), 88 } 89 90 req.AddCookie(cookie) 91 92 rw := mock.NewMockResponseWriter() 93 hdl.ServeHTTP(rw, req) 94 95 NewWithT(t).Expect(string(rw.MustDumpResponse())).To(Equal( 96 `HTTP/0.0 204 No Content 97 Set-Cookie: ` + cookie.String() + ` 98 X-Meta: service-test@1.0.0/Cookie 99 100 `)) 101 }) 102 103 t.Run("ReturnOK", func(t *testing.T) { 104 root := kit.NewRouter(Group("/root")) 105 root.Register(kit.NewRouter(routes.DataProvider{}, routes.GetByID{})) 106 107 route := NewHttpRouteMeta(root.Routes()[0]) 108 hdl := NewRouteHandler(meta, route, factory) 109 110 reqData := struct { 111 routes.DataProvider 112 routes.GetByID 113 }{ 114 DataProvider: routes.DataProvider{ 115 ID: "123456", 116 }, 117 GetByID: routes.GetByID{ 118 Label: []string{"label"}, 119 }, 120 } 121 122 req, err := factory.NewRequestWithContext( 123 EnableQueryInBodyForGet(bgctx), 124 (routes.GetByID{}).Method(), 125 reqData.Path(), 126 reqData, 127 ) 128 NewWithT(t).Expect(err).To(BeNil()) 129 130 dumped, _ := httputil.DumpRequest(req, true) 131 NewWithT(t).Expect(string(dumped)). 132 To(Equal("GET /123456 HTTP/1.1\r\nContent-Type: application/x-www-form-urlencoded; param=value\r\n\r\nlabel=label")) 133 134 rw := mock.NewMockResponseWriter() 135 hdl.ServeHTTP(rw, req) 136 137 NewWithT(t).Expect(string(rw.MustDumpResponse())). 138 To(Equal(`HTTP/0.0 200 OK 139 Content-Type: application/json; charset=utf-8 140 X-Meta: service-test@1.0.0/GetByID 141 142 {"id":"123456","label":"label"} 143 `)) 144 }) 145 146 t.Run("PostReturnOK", func(t *testing.T) { 147 root := kit.NewRouter(Group("/root")) 148 root.Register(kit.NewRouter(routes.Create{})) 149 150 route := NewHttpRouteMeta(root.Routes()[0]) 151 hdl := NewRouteHandler(meta, route, factory) 152 153 reqData := routes.Create{ 154 Data: routes.Data{ 155 ID: "123456", 156 Label: "123", 157 }, 158 } 159 160 req, err := factory.NewRequest((routes.Create{}).Method(), "/", reqData) 161 NewWithT(t).Expect(err).To(BeNil()) 162 163 rw := mock.NewMockResponseWriter() 164 hdl.ServeHTTP(rw, req) 165 166 NewWithT(t).Expect(string(rw.MustDumpResponse())).To(Equal(`HTTP/0.0 201 Created 167 Content-Type: application/json; charset=utf-8 168 X-Meta: service-test@1.0.0/Create 169 170 {"id":"123456","label":"123"} 171 `)) 172 }) 173 174 t.Run("PostReturnBadRequest", func(t *testing.T) { 175 root := kit.NewRouter(Group("/root")) 176 root.Register(kit.NewRouter(routes.Create{})) 177 178 route := NewHttpRouteMeta(root.Routes()[0]) 179 hdl := NewRouteHandler(meta, route, factory) 180 181 reqData := routes.Create{ 182 Data: routes.Data{ 183 ID: "123456", 184 }, 185 } 186 187 req, err := factory.NewRequest((routes.Create{}).Method(), "/", reqData) 188 NewWithT(t).Expect(err).To(BeNil()) 189 190 rw := mock.NewMockResponseWriter() 191 hdl.ServeHTTP(rw, req) 192 193 NewWithT(t).Expect(string(rw.MustDumpResponse())).To(Equal(`HTTP/0.0 400 Bad Request 194 Content-Type: application/json; charset=utf-8 195 X-Meta: service-test@1.0.0/Create 196 197 {"key":"badRequest","code":400000000,"msg":"invalid parameters","desc":"","canBeTalk":false,"id":"","sources":["service-test@1.0.0"],"fields":[{"field":"label","msg":"missing required field","in":"body"}]} 198 `)) 199 }) 200 201 t.Run("ReturnNil", func(t *testing.T) { 202 root := kit.NewRouter(Group("/root")) 203 root.Register(kit.NewRouter(routes.DataProvider{}, routes.RemoveByID{})) 204 205 route := NewHttpRouteMeta(root.Routes()[0]) 206 hdl := NewRouteHandler(meta, route, factory) 207 208 reqData := routes.DataProvider{ 209 ID: "123456", 210 } 211 212 req, err := factory.NewRequest((routes.RemoveByID{}).Method(), reqData.Path(), reqData) 213 NewWithT(t).Expect(err).To(BeNil()) 214 215 rw := mock.NewMockResponseWriter() 216 hdl.ServeHTTP(rw, req) 217 218 NewWithT(t).Expect(string(rw.MustDumpResponse())).To(Equal(`HTTP/0.0 500 Internal Server Error 219 Content-Type: application/json; charset=utf-8 220 X-Meta: service-test@1.0.0/RemoveByID 221 X-Num: 1 222 223 {"key":"InternalServerError","code":500999001,"msg":"InternalServerError","desc":"","canBeTalk":false,"id":"","sources":["service-test@1.0.0"],"fields":null} 224 `)) 225 }) 226 227 t.Run("ReturnAttachment", func(t *testing.T) { 228 root := kit.NewRouter(Group("/root")) 229 root.Register(kit.NewRouter(routes.DownloadFile{})) 230 231 route := NewHttpRouteMeta(root.Routes()[0]) 232 hdl := NewRouteHandler(meta, route, factory) 233 234 op := routes.DownloadFile{} 235 req, err := factory.NewRequest(op.Method(), op.Path(), op) 236 NewWithT(t).Expect(err).To(BeNil()) 237 238 rw := mock.NewMockResponseWriter() 239 hdl.ServeHTTP(rw, req) 240 241 NewWithT(t).Expect(string(rw.MustDumpResponse())).To(Equal(`HTTP/0.0 200 OK 242 Content-Disposition: attachment; filename=text.txt 243 Content-Type: text/plain 244 X-Meta: service-test@1.0.0/DownloadFile 245 246 123123123`)) 247 }) 248 249 t.Run("ReturnWithProcessError", func(t *testing.T) { 250 root := kit.NewRouter(Group("/root")) 251 root.Register(kit.NewRouter(routes.DataProvider{}, routes.UpdateByID{})) 252 253 route := NewHttpRouteMeta(root.Routes()[0]) 254 hdl := NewRouteHandler(meta, route, factory) 255 256 reqData := routes.DataProvider{ 257 ID: "123456", 258 } 259 260 req, err := factory.NewRequest((routes.GetByID{}).Method(), reqData.Path(), struct { 261 routes.DataProvider 262 routes.UpdateByID 263 }{ 264 DataProvider: reqData, 265 UpdateByID: routes.UpdateByID{ 266 Data: routes.Data{ 267 ID: "11", 268 Label: "11", 269 }, 270 }, 271 }) 272 NewWithT(t).Expect(err).To(BeNil()) 273 274 rw := mock.NewMockResponseWriter() 275 hdl.ServeHTTP(rw, req) 276 277 NewWithT(t).Expect(string(rw.MustDumpResponse())).To(Equal(`HTTP/0.0 500 Internal Server Error 278 Content-Type: application/json; charset=utf-8 279 X-Meta: service-test@1.0.0/UpdateByID 280 281 {"key":"UnknownError","code":500000000,"msg":"UnknownError","desc":"something wrong","canBeTalk":false,"id":"","sources":["service-test@1.0.0"],"fields":null} 282 `)) 283 }) 284 285 t.Run("ReturnWithValidateError", func(t *testing.T) { 286 root := kit.NewRouter(Group("/root")) 287 root.Register(kit.NewRouter(routes.DataProvider{}, routes.GetByID{})) 288 289 route := NewHttpRouteMeta(root.Routes()[0]) 290 hdl := NewRouteHandler(meta, route, factory) 291 292 reqData := routes.DataProvider{ 293 ID: "10", 294 } 295 296 req, err := factory.NewRequest((routes.GetByID{}).Method(), reqData.Path(), reqData) 297 NewWithT(t).Expect(err).To(BeNil()) 298 299 rw := mock.NewMockResponseWriter() 300 hdl.ServeHTTP(rw, req) 301 302 NewWithT(t).Expect(string(rw.MustDumpResponse())).To(Equal(`HTTP/0.0 400 Bad Request 303 Content-Type: application/json; charset=utf-8 304 X-Meta: service-test@1.0.0/GetByID 305 306 {"key":"badRequest","code":400000000,"msg":"invalid parameters","desc":"","canBeTalk":false,"id":"","sources":["service-test@1.0.0"],"fields":[{"field":"id","msg":"string length should be larger than 6, but got invalid value 2","in":"path"}]} 307 `)) 308 }) 309 }