github.com/gogf/gf/v2@v2.7.4/net/ghttp/ghttp_z_unit_feature_middleware_basic_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package ghttp_test 8 9 import ( 10 "compress/gzip" 11 "fmt" 12 "net/http" 13 "strings" 14 "testing" 15 "time" 16 17 "github.com/gogf/gf/v2/container/garray" 18 "github.com/gogf/gf/v2/frame/g" 19 "github.com/gogf/gf/v2/net/ghttp" 20 "github.com/gogf/gf/v2/test/gtest" 21 "github.com/gogf/gf/v2/util/guid" 22 "go.opentelemetry.io/otel" 23 "go.opentelemetry.io/otel/trace" 24 ) 25 26 func Test_BindMiddleware_Basic1(t *testing.T) { 27 s := g.Server(guid.S()) 28 s.BindHandler("/test/test", func(r *ghttp.Request) { 29 r.Response.Write("test") 30 }) 31 s.BindMiddleware("/test", func(r *ghttp.Request) { 32 r.Response.Write("1") 33 r.Middleware.Next() 34 r.Response.Write("2") 35 }, func(r *ghttp.Request) { 36 r.Response.Write("3") 37 r.Middleware.Next() 38 r.Response.Write("4") 39 }) 40 s.BindMiddleware("/test/:name", func(r *ghttp.Request) { 41 r.Response.Write("5") 42 r.Middleware.Next() 43 r.Response.Write("6") 44 }, func(r *ghttp.Request) { 45 r.Response.Write("7") 46 r.Middleware.Next() 47 r.Response.Write("8") 48 }) 49 s.SetDumpRouterMap(false) 50 s.Start() 51 defer s.Shutdown() 52 53 time.Sleep(100 * time.Millisecond) 54 gtest.C(t, func(t *gtest.T) { 55 client := g.Client() 56 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 57 58 t.Assert(client.GetContent(ctx, "/"), "Not Found") 59 t.Assert(client.GetContent(ctx, "/test"), "1342") 60 t.Assert(client.GetContent(ctx, "/test/test"), "57test86") 61 }) 62 } 63 64 func Test_BindMiddleware_Basic2(t *testing.T) { 65 s := g.Server(guid.S()) 66 s.BindHandler("/test/test", func(r *ghttp.Request) { 67 r.Response.Write("test") 68 }) 69 s.BindMiddleware("/*", func(r *ghttp.Request) { 70 r.Response.Write("1") 71 r.Middleware.Next() 72 r.Response.Write("2") 73 }) 74 s.SetDumpRouterMap(false) 75 s.Start() 76 defer s.Shutdown() 77 78 time.Sleep(100 * time.Millisecond) 79 gtest.C(t, func(t *gtest.T) { 80 client := g.Client() 81 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 82 83 t.Assert(client.GetContent(ctx, "/"), "12") 84 t.Assert(client.GetContent(ctx, "/test"), "12") 85 t.Assert(client.GetContent(ctx, "/test/test"), "1test2") 86 }) 87 } 88 89 func Test_BindMiddleware_Basic3(t *testing.T) { 90 s := g.Server(guid.S()) 91 s.BindHandler("/test/test", func(r *ghttp.Request) { 92 r.Response.Write("test") 93 }) 94 s.BindMiddleware("PUT:/test", func(r *ghttp.Request) { 95 r.Response.Write("1") 96 r.Middleware.Next() 97 r.Response.Write("2") 98 }, func(r *ghttp.Request) { 99 r.Response.Write("3") 100 r.Middleware.Next() 101 r.Response.Write("4") 102 }) 103 s.BindMiddleware("POST:/test/:name", func(r *ghttp.Request) { 104 r.Response.Write("5") 105 r.Middleware.Next() 106 r.Response.Write("6") 107 }, func(r *ghttp.Request) { 108 r.Response.Write("7") 109 r.Middleware.Next() 110 r.Response.Write("8") 111 }) 112 s.SetDumpRouterMap(false) 113 s.Start() 114 defer s.Shutdown() 115 116 time.Sleep(100 * time.Millisecond) 117 gtest.C(t, func(t *gtest.T) { 118 client := g.Client() 119 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 120 121 t.Assert(client.GetContent(ctx, "/"), "Not Found") 122 t.Assert(client.GetContent(ctx, "/test"), "Not Found") 123 t.Assert(client.PutContent(ctx, "/test"), "1342") 124 t.Assert(client.PostContent(ctx, "/test"), "Not Found") 125 t.Assert(client.GetContent(ctx, "/test/test"), "test") 126 t.Assert(client.PutContent(ctx, "/test/test"), "test") 127 t.Assert(client.PostContent(ctx, "/test/test"), "57test86") 128 }) 129 } 130 131 func Test_BindMiddleware_Basic4(t *testing.T) { 132 s := g.Server(guid.S()) 133 s.Group("/", func(group *ghttp.RouterGroup) { 134 group.Middleware(func(r *ghttp.Request) { 135 r.Response.Write("1") 136 r.Middleware.Next() 137 }) 138 group.Middleware(func(r *ghttp.Request) { 139 r.Middleware.Next() 140 r.Response.Write("2") 141 }) 142 group.ALL("/test", func(r *ghttp.Request) { 143 r.Response.Write("test") 144 }) 145 }) 146 s.SetDumpRouterMap(false) 147 s.Start() 148 defer s.Shutdown() 149 150 time.Sleep(100 * time.Millisecond) 151 gtest.C(t, func(t *gtest.T) { 152 client := g.Client() 153 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 154 155 t.Assert(client.GetContent(ctx, "/"), "Not Found") 156 t.Assert(client.GetContent(ctx, "/test"), "1test2") 157 t.Assert(client.PutContent(ctx, "/test/none"), "Not Found") 158 }) 159 } 160 161 func Test_Middleware_With_Static(t *testing.T) { 162 s := g.Server(guid.S()) 163 s.Group("/", func(group *ghttp.RouterGroup) { 164 group.Middleware(func(r *ghttp.Request) { 165 r.Response.Write("1") 166 r.Middleware.Next() 167 r.Response.Write("2") 168 }) 169 group.ALL("/user/list", func(r *ghttp.Request) { 170 r.Response.Write("list") 171 }) 172 }) 173 s.SetDumpRouterMap(false) 174 s.SetServerRoot(gtest.DataPath("static1")) 175 s.Start() 176 defer s.Shutdown() 177 time.Sleep(100 * time.Millisecond) 178 gtest.C(t, func(t *gtest.T) { 179 client := g.Client() 180 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 181 182 t.Assert(client.GetContent(ctx, "/"), "index") 183 t.Assert(client.GetContent(ctx, "/test.html"), "test") 184 t.Assert(client.GetContent(ctx, "/none"), "Not Found") 185 t.Assert(client.GetContent(ctx, "/user/list"), "1list2") 186 }) 187 } 188 189 func Test_Middleware_Status(t *testing.T) { 190 s := g.Server(guid.S()) 191 s.Group("/", func(group *ghttp.RouterGroup) { 192 group.Middleware(func(r *ghttp.Request) { 193 r.Middleware.Next() 194 r.Response.WriteOver(r.Response.Status) 195 }) 196 group.ALL("/user/list", func(r *ghttp.Request) { 197 r.Response.Write("list") 198 }) 199 }) 200 s.SetDumpRouterMap(false) 201 s.Start() 202 defer s.Shutdown() 203 time.Sleep(100 * time.Millisecond) 204 gtest.C(t, func(t *gtest.T) { 205 client := g.Client() 206 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 207 208 t.Assert(client.GetContent(ctx, "/"), "Not Found") 209 t.Assert(client.GetContent(ctx, "/user/list"), "200") 210 211 resp, err := client.Get(ctx, "/") 212 t.AssertNil(err) 213 defer resp.Close() 214 t.Assert(resp.StatusCode, 404) 215 }) 216 } 217 218 func Test_Middleware_Hook_With_Static(t *testing.T) { 219 s := g.Server(guid.S()) 220 a := garray.New(true) 221 s.Group("/", func(group *ghttp.RouterGroup) { 222 group.Hook("/*", ghttp.HookBeforeServe, func(r *ghttp.Request) { 223 a.Append(1) 224 fmt.Println("HookBeforeServe") 225 r.Response.Write("a") 226 }) 227 group.Hook("/*", ghttp.HookAfterServe, func(r *ghttp.Request) { 228 a.Append(1) 229 fmt.Println("HookAfterServe") 230 r.Response.Write("b") 231 }) 232 group.Middleware(func(r *ghttp.Request) { 233 r.Response.Write("1") 234 r.Middleware.Next() 235 r.Response.Write("2") 236 }) 237 group.ALL("/user/list", func(r *ghttp.Request) { 238 r.Response.Write("list") 239 }) 240 }) 241 s.SetDumpRouterMap(false) 242 s.SetServerRoot(gtest.DataPath("static1")) 243 s.Start() 244 defer s.Shutdown() 245 time.Sleep(100 * time.Millisecond) 246 gtest.C(t, func(t *gtest.T) { 247 client := g.Client() 248 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 249 250 // The length assert sometimes fails, so I added time.Sleep here for debug purpose. 251 252 t.Assert(client.GetContent(ctx, "/"), "index") 253 time.Sleep(100 * time.Millisecond) 254 t.Assert(a.Len(), 2) 255 256 t.Assert(client.GetContent(ctx, "/test.html"), "test") 257 time.Sleep(100 * time.Millisecond) 258 t.Assert(a.Len(), 4) 259 260 t.Assert(client.GetContent(ctx, "/none"), "ab") 261 time.Sleep(100 * time.Millisecond) 262 t.Assert(a.Len(), 6) 263 264 t.Assert(client.GetContent(ctx, "/user/list"), "a1list2b") 265 time.Sleep(100 * time.Millisecond) 266 t.Assert(a.Len(), 8) 267 }) 268 } 269 270 func Test_BindMiddleware_Status(t *testing.T) { 271 s := g.Server(guid.S()) 272 s.BindHandler("/test/test", func(r *ghttp.Request) { 273 r.Response.Write("test") 274 }) 275 s.BindMiddleware("/test/*any", func(r *ghttp.Request) { 276 r.Middleware.Next() 277 }) 278 s.SetDumpRouterMap(false) 279 s.Start() 280 defer s.Shutdown() 281 282 time.Sleep(100 * time.Millisecond) 283 gtest.C(t, func(t *gtest.T) { 284 client := g.Client() 285 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 286 287 t.Assert(client.GetContent(ctx, "/"), "Not Found") 288 t.Assert(client.GetContent(ctx, "/test"), "Not Found") 289 t.Assert(client.GetContent(ctx, "/test/test"), "test") 290 t.Assert(client.GetContent(ctx, "/test/test/test"), "Not Found") 291 }) 292 } 293 294 func Test_BindMiddlewareDefault_Basic1(t *testing.T) { 295 s := g.Server(guid.S()) 296 s.BindHandler("/test/test", func(r *ghttp.Request) { 297 r.Response.Write("test") 298 }) 299 s.BindMiddlewareDefault(func(r *ghttp.Request) { 300 r.Response.Write("1") 301 r.Middleware.Next() 302 r.Response.Write("2") 303 }) 304 s.BindMiddlewareDefault(func(r *ghttp.Request) { 305 r.Response.Write("3") 306 r.Middleware.Next() 307 r.Response.Write("4") 308 }) 309 s.SetDumpRouterMap(false) 310 s.Start() 311 defer s.Shutdown() 312 313 time.Sleep(100 * time.Millisecond) 314 gtest.C(t, func(t *gtest.T) { 315 client := g.Client() 316 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 317 318 t.Assert(client.GetContent(ctx, "/"), "1342") 319 t.Assert(client.GetContent(ctx, "/test/test"), "13test42") 320 }) 321 } 322 323 func Test_BindMiddlewareDefault_Basic2(t *testing.T) { 324 s := g.Server(guid.S()) 325 s.BindHandler("PUT:/test/test", func(r *ghttp.Request) { 326 r.Response.Write("test") 327 }) 328 s.BindMiddlewareDefault(func(r *ghttp.Request) { 329 r.Response.Write("1") 330 r.Middleware.Next() 331 r.Response.Write("2") 332 }) 333 s.BindMiddlewareDefault(func(r *ghttp.Request) { 334 r.Response.Write("3") 335 r.Middleware.Next() 336 r.Response.Write("4") 337 }) 338 s.SetDumpRouterMap(false) 339 s.Start() 340 defer s.Shutdown() 341 342 time.Sleep(100 * time.Millisecond) 343 gtest.C(t, func(t *gtest.T) { 344 client := g.Client() 345 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 346 347 t.Assert(client.GetContent(ctx, "/"), "1342") 348 t.Assert(client.PutContent(ctx, "/"), "1342") 349 t.Assert(client.GetContent(ctx, "/test/test"), "1342") 350 t.Assert(client.PutContent(ctx, "/test/test"), "13test42") 351 }) 352 } 353 354 func Test_BindMiddlewareDefault_Basic3(t *testing.T) { 355 s := g.Server(guid.S()) 356 s.BindHandler("/test/test", func(r *ghttp.Request) { 357 r.Response.Write("test") 358 }) 359 s.BindMiddlewareDefault(func(r *ghttp.Request) { 360 r.Response.Write("1") 361 r.Middleware.Next() 362 }) 363 s.BindMiddlewareDefault(func(r *ghttp.Request) { 364 r.Middleware.Next() 365 r.Response.Write("2") 366 }) 367 s.SetDumpRouterMap(false) 368 s.Start() 369 defer s.Shutdown() 370 371 time.Sleep(100 * time.Millisecond) 372 gtest.C(t, func(t *gtest.T) { 373 client := g.Client() 374 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 375 376 t.Assert(client.GetContent(ctx, "/"), "12") 377 t.Assert(client.GetContent(ctx, "/test/test"), "1test2") 378 }) 379 } 380 381 func Test_BindMiddlewareDefault_Basic4(t *testing.T) { 382 s := g.Server(guid.S()) 383 s.BindHandler("/test/test", func(r *ghttp.Request) { 384 r.Response.Write("test") 385 }) 386 s.BindMiddlewareDefault(func(r *ghttp.Request) { 387 r.Middleware.Next() 388 r.Response.Write("1") 389 }) 390 s.BindMiddlewareDefault(func(r *ghttp.Request) { 391 r.Response.Write("2") 392 r.Middleware.Next() 393 }) 394 s.SetDumpRouterMap(false) 395 s.Start() 396 defer s.Shutdown() 397 398 time.Sleep(100 * time.Millisecond) 399 gtest.C(t, func(t *gtest.T) { 400 client := g.Client() 401 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 402 403 t.Assert(client.GetContent(ctx, "/"), "21") 404 t.Assert(client.GetContent(ctx, "/test/test"), "2test1") 405 }) 406 } 407 408 func Test_BindMiddlewareDefault_Basic5(t *testing.T) { 409 s := g.Server(guid.S()) 410 s.BindHandler("/test/test", func(r *ghttp.Request) { 411 r.Response.Write("test") 412 }) 413 s.BindMiddlewareDefault(func(r *ghttp.Request) { 414 r.Response.Write("1") 415 r.Middleware.Next() 416 }) 417 s.BindMiddlewareDefault(func(r *ghttp.Request) { 418 r.Response.Write("2") 419 r.Middleware.Next() 420 }) 421 s.SetDumpRouterMap(false) 422 s.Start() 423 defer s.Shutdown() 424 425 time.Sleep(100 * time.Millisecond) 426 gtest.C(t, func(t *gtest.T) { 427 client := g.Client() 428 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 429 430 t.Assert(client.GetContent(ctx, "/"), "12") 431 t.Assert(client.GetContent(ctx, "/test/test"), "12test") 432 }) 433 } 434 435 func Test_BindMiddlewareDefault_Status(t *testing.T) { 436 s := g.Server(guid.S()) 437 s.BindHandler("/test/test", func(r *ghttp.Request) { 438 r.Response.Write("test") 439 }) 440 s.BindMiddlewareDefault(func(r *ghttp.Request) { 441 r.Middleware.Next() 442 }) 443 s.SetDumpRouterMap(false) 444 s.Start() 445 defer s.Shutdown() 446 447 time.Sleep(100 * time.Millisecond) 448 gtest.C(t, func(t *gtest.T) { 449 client := g.Client() 450 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 451 452 t.Assert(client.GetContent(ctx, "/"), "Not Found") 453 t.Assert(client.GetContent(ctx, "/test/test"), "test") 454 }) 455 } 456 457 type ObjectMiddleware struct{} 458 459 func (o *ObjectMiddleware) Init(r *ghttp.Request) { 460 r.Response.Write("100") 461 } 462 463 func (o *ObjectMiddleware) Shut(r *ghttp.Request) { 464 r.Response.Write("200") 465 } 466 467 func (o *ObjectMiddleware) Index(r *ghttp.Request) { 468 r.Response.Write("Object Index") 469 } 470 471 func (o *ObjectMiddleware) Show(r *ghttp.Request) { 472 r.Response.Write("Object Show") 473 } 474 475 func (o *ObjectMiddleware) Info(r *ghttp.Request) { 476 r.Response.Write("Object Info") 477 } 478 479 func Test_BindMiddlewareDefault_Basic6(t *testing.T) { 480 s := g.Server(guid.S()) 481 s.BindObject("/", new(ObjectMiddleware)) 482 s.BindMiddlewareDefault(func(r *ghttp.Request) { 483 r.Response.Write("1") 484 r.Middleware.Next() 485 r.Response.Write("2") 486 }) 487 s.BindMiddlewareDefault(func(r *ghttp.Request) { 488 r.Response.Write("3") 489 r.Middleware.Next() 490 r.Response.Write("4") 491 }) 492 s.SetDumpRouterMap(false) 493 s.Start() 494 defer s.Shutdown() 495 496 time.Sleep(100 * time.Millisecond) 497 gtest.C(t, func(t *gtest.T) { 498 client := g.Client() 499 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 500 501 t.Assert(client.GetContent(ctx, "/"), "13100Object Index20042") 502 t.Assert(client.GetContent(ctx, "/init"), "1342") 503 t.Assert(client.GetContent(ctx, "/shut"), "1342") 504 t.Assert(client.GetContent(ctx, "/index"), "13100Object Index20042") 505 t.Assert(client.GetContent(ctx, "/show"), "13100Object Show20042") 506 t.Assert(client.GetContent(ctx, "/none-exist"), "1342") 507 }) 508 } 509 510 func Test_Hook_Middleware_Basic1(t *testing.T) { 511 s := g.Server(guid.S()) 512 s.BindHandler("/test/test", func(r *ghttp.Request) { 513 r.Response.Write("test") 514 }) 515 s.BindHookHandler("/*", ghttp.HookBeforeServe, func(r *ghttp.Request) { 516 r.Response.Write("a") 517 }) 518 s.BindHookHandler("/*", ghttp.HookAfterServe, func(r *ghttp.Request) { 519 r.Response.Write("b") 520 }) 521 s.BindHookHandler("/*", ghttp.HookBeforeServe, func(r *ghttp.Request) { 522 r.Response.Write("c") 523 }) 524 s.BindHookHandler("/*", ghttp.HookAfterServe, func(r *ghttp.Request) { 525 r.Response.Write("d") 526 }) 527 s.BindMiddlewareDefault(func(r *ghttp.Request) { 528 r.Response.Write("1") 529 r.Middleware.Next() 530 r.Response.Write("2") 531 }) 532 s.BindMiddlewareDefault(func(r *ghttp.Request) { 533 r.Response.Write("3") 534 r.Middleware.Next() 535 r.Response.Write("4") 536 }) 537 s.SetDumpRouterMap(false) 538 s.Start() 539 defer s.Shutdown() 540 541 time.Sleep(100 * time.Millisecond) 542 gtest.C(t, func(t *gtest.T) { 543 client := g.Client() 544 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 545 546 t.Assert(client.GetContent(ctx, "/"), "ac1342bd") 547 t.Assert(client.GetContent(ctx, "/test/test"), "ac13test42bd") 548 }) 549 } 550 551 func MiddlewareAuth(r *ghttp.Request) { 552 token := r.Get("token").String() 553 if token == "123456" { 554 r.Middleware.Next() 555 } else { 556 r.Response.WriteStatus(http.StatusForbidden) 557 } 558 } 559 560 func MiddlewareCORS(r *ghttp.Request) { 561 r.Response.CORSDefault() 562 r.Middleware.Next() 563 } 564 565 func Test_Middleware_CORSAndAuth(t *testing.T) { 566 s := g.Server(guid.S()) 567 s.Use(MiddlewareCORS) 568 s.Group("/api.v2", func(group *ghttp.RouterGroup) { 569 group.Middleware(MiddlewareAuth) 570 group.POST("/user/list", func(r *ghttp.Request) { 571 r.Response.Write("list") 572 }) 573 }) 574 s.SetDumpRouterMap(false) 575 s.Start() 576 defer s.Shutdown() 577 time.Sleep(100 * time.Millisecond) 578 gtest.C(t, func(t *gtest.T) { 579 client := g.Client() 580 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 581 // Common Checks. 582 t.Assert(client.GetContent(ctx, "/"), "Not Found") 583 t.Assert(client.GetContent(ctx, "/api.v2"), "Not Found") 584 // Auth Checks. 585 t.Assert(client.PostContent(ctx, "/api.v2/user/list"), "Forbidden") 586 t.Assert(client.PostContent(ctx, "/api.v2/user/list", "token=123456"), "list") 587 // CORS Checks. 588 resp, err := client.Post(ctx, "/api.v2/user/list", "token=123456") 589 t.AssertNil(err) 590 t.Assert(len(resp.Header["Access-Control-Allow-Headers"]), 1) 591 t.Assert(resp.Header["Access-Control-Allow-Headers"][0], "Origin,Content-Type,Accept,User-Agent,Cookie,Authorization,X-Auth-Token,X-Requested-With") 592 t.Assert(resp.Header["Access-Control-Allow-Methods"][0], "GET,PUT,POST,DELETE,PATCH,HEAD,CONNECT,OPTIONS,TRACE") 593 t.Assert(resp.Header["Access-Control-Allow-Origin"][0], "*") 594 t.Assert(resp.Header["Access-Control-Max-Age"][0], "3628800") 595 resp.Close() 596 }) 597 gtest.C(t, func(t *gtest.T) { 598 client := g.Client() 599 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 600 t.Assert(client.SetHeader("Access-Control-Request-Headers", "GF,GoFrame").GetContent(ctx, "/"), "Not Found") 601 t.Assert(client.SetHeader("Origin", "GoFrame").GetContent(ctx, "/"), "Not Found") 602 }) 603 gtest.C(t, func(t *gtest.T) { 604 client := g.Client() 605 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 606 t.Assert(client.SetHeader("Referer", "Referer").PostContent(ctx, "/"), "Not Found") 607 }) 608 } 609 610 func MiddlewareScope1(r *ghttp.Request) { 611 r.Response.Write("a") 612 r.Middleware.Next() 613 r.Response.Write("b") 614 } 615 616 func MiddlewareScope2(r *ghttp.Request) { 617 r.Response.Write("c") 618 r.Middleware.Next() 619 r.Response.Write("d") 620 } 621 622 func MiddlewareScope3(r *ghttp.Request) { 623 r.Response.Write("e") 624 r.Middleware.Next() 625 r.Response.Write("f") 626 } 627 628 func Test_Middleware_Scope(t *testing.T) { 629 s := g.Server(guid.S()) 630 s.Group("/", func(group *ghttp.RouterGroup) { 631 group.Middleware(MiddlewareScope1) 632 group.ALL("/scope1", func(r *ghttp.Request) { 633 r.Response.Write("1") 634 }) 635 group.Group("/", func(group *ghttp.RouterGroup) { 636 group.Middleware(MiddlewareScope2) 637 group.ALL("/scope2", func(r *ghttp.Request) { 638 r.Response.Write("2") 639 }) 640 }) 641 group.Group("/", func(group *ghttp.RouterGroup) { 642 group.Middleware(MiddlewareScope3) 643 group.ALL("/scope3", func(r *ghttp.Request) { 644 r.Response.Write("3") 645 }) 646 }) 647 }) 648 s.SetDumpRouterMap(false) 649 s.Start() 650 defer s.Shutdown() 651 time.Sleep(100 * time.Millisecond) 652 gtest.C(t, func(t *gtest.T) { 653 client := g.Client() 654 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 655 656 t.Assert(client.GetContent(ctx, "/"), "Not Found") 657 t.Assert(client.GetContent(ctx, "/scope1"), "a1b") 658 t.Assert(client.GetContent(ctx, "/scope2"), "ac2db") 659 t.Assert(client.GetContent(ctx, "/scope3"), "ae3fb") 660 }) 661 } 662 663 func Test_Middleware_Panic(t *testing.T) { 664 s := g.Server(guid.S()) 665 i := 0 666 s.Group("/", func(group *ghttp.RouterGroup) { 667 group.Group("/", func(group *ghttp.RouterGroup) { 668 group.Middleware(func(r *ghttp.Request) { 669 i++ 670 panic("error") 671 // r.Middleware.Next() 672 }, func(r *ghttp.Request) { 673 i++ 674 r.Middleware.Next() 675 }) 676 group.ALL("/", func(r *ghttp.Request) { 677 r.Response.Write(i) 678 }) 679 }) 680 }) 681 s.SetDumpRouterMap(false) 682 s.Start() 683 defer s.Shutdown() 684 time.Sleep(100 * time.Millisecond) 685 gtest.C(t, func(t *gtest.T) { 686 client := g.Client() 687 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 688 689 t.Assert(client.GetContent(ctx, "/"), "exception recovered: error") 690 }) 691 } 692 693 func Test_Middleware_JsonBody(t *testing.T) { 694 s := g.Server(guid.S()) 695 s.Group("/", func(group *ghttp.RouterGroup) { 696 group.Middleware(ghttp.MiddlewareJsonBody) 697 group.ALL("/", func(r *ghttp.Request) { 698 r.Response.Write("hello") 699 }) 700 }) 701 s.SetDumpRouterMap(false) 702 s.Start() 703 defer s.Shutdown() 704 time.Sleep(100 * time.Millisecond) 705 gtest.C(t, func(t *gtest.T) { 706 client := g.Client() 707 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 708 709 t.Assert(client.GetContent(ctx, "/"), "hello") 710 t.Assert(client.PutContent(ctx, "/"), "hello") 711 t.Assert(client.PutContent(ctx, "/", `{"name":"john"}`), "hello") 712 t.Assert(client.PutContent(ctx, "/", `{"name":}`), "the request body content should be JSON format") 713 }) 714 } 715 716 func Test_MiddlewareHandlerResponse(t *testing.T) { 717 s := g.Server(guid.S()) 718 s.Group("/", func(group *ghttp.RouterGroup) { 719 group.Middleware(ghttp.MiddlewareHandlerResponse) 720 group.GET("/403", func(r *ghttp.Request) { 721 r.Response.WriteStatus(http.StatusForbidden, "") 722 }) 723 group.GET("/default", func(r *ghttp.Request) { 724 r.Response.WriteStatus(http.StatusInternalServerError, "") 725 }) 726 }) 727 s.SetDumpRouterMap(false) 728 s.Start() 729 defer s.Shutdown() 730 time.Sleep(100 * time.Millisecond) 731 gtest.C(t, func(t *gtest.T) { 732 client := g.Client() 733 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 734 735 rsp, err := client.Get(ctx, "/403") 736 t.AssertNil(err) 737 t.Assert(rsp.StatusCode, http.StatusForbidden) 738 rsp, err = client.Get(ctx, "/default") 739 t.AssertNil(err) 740 t.Assert(rsp.StatusCode, http.StatusInternalServerError) 741 }) 742 } 743 744 func Test_MiddlewareHandlerGzipResponse(t *testing.T) { 745 tp := testTracerProvider{} 746 otel.SetTracerProvider(&tp) 747 s := g.Server(guid.S()) 748 s.Group("/", func(group *ghttp.RouterGroup) { 749 group.GET("/default", func(r *ghttp.Request) { 750 var buffer strings.Builder 751 gzipWriter := gzip.NewWriter(&buffer) 752 defer gzipWriter.Close() 753 _, _ = gzipWriter.Write([]byte("hello")) 754 // 设置响应头,表明内容使用 gzip 压缩 755 r.Response.Header().Set("Content-Encoding", "gzip") 756 r.Response.Header().Set("Content-Type", "text/plain") 757 r.Response.Header().Set("Content-Length", fmt.Sprint(buffer.Len())) 758 // 写入压缩后的内容 759 r.Response.Write(buffer.String()) 760 }) 761 }) 762 s.SetDumpRouterMap(false) 763 s.Start() 764 defer s.Shutdown() 765 time.Sleep(100 * time.Millisecond) 766 gtest.C(t, func(t *gtest.T) { 767 client := g.Client() 768 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 769 rsp, err := client.Get(ctx, "/default") 770 t.AssertNil(err) 771 t.Assert(rsp.StatusCode, http.StatusOK) 772 }) 773 } 774 775 func Test_MiddlewareHandlerStreamResponse(t *testing.T) { 776 s := g.Server(guid.S()) 777 s.Group("/", func(group *ghttp.RouterGroup) { 778 group.Middleware(ghttp.MiddlewareHandlerResponse) 779 780 group.GET("/stream/event", func(r *ghttp.Request) { 781 r.Response.Header().Set("Content-Type", "text/event-stream") 782 }) 783 784 group.GET("/stream/octet", func(r *ghttp.Request) { 785 r.Response.Header().Set("Content-Type", "application/octet-stream") 786 }) 787 788 group.GET("/stream/mixed", func(r *ghttp.Request) { 789 r.Response.Header().Set("Content-Type", "multipart/x-mixed-replace") 790 }) 791 }) 792 s.SetDumpRouterMap(false) 793 s.Start() 794 defer s.Shutdown() 795 time.Sleep(100 * time.Millisecond) 796 797 gtest.C(t, func(t *gtest.T) { 798 client := g.Client() 799 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 800 801 rsp, err := client.Get(ctx, "/stream/event") 802 t.AssertNil(err) 803 t.Assert(rsp.StatusCode, http.StatusOK) 804 t.Assert(rsp.ReadAllString(), "") 805 806 rsp, err = client.Get(ctx, "/stream/octet") 807 t.AssertNil(err) 808 t.Assert(rsp.StatusCode, http.StatusOK) 809 t.Assert(rsp.ReadAllString(), "") 810 811 rsp, err = client.Get(ctx, "/stream/mixed") 812 t.AssertNil(err) 813 t.Assert(rsp.StatusCode, http.StatusOK) 814 t.Assert(rsp.ReadAllString(), "") 815 }) 816 } 817 818 type testTracerProvider struct{} 819 820 var _ trace.TracerProvider = &testTracerProvider{} 821 822 func (*testTracerProvider) Tracer(_ string, _ ...trace.TracerOption) trace.Tracer { 823 return trace.NewNoopTracerProvider().Tracer("") 824 }