github.com/astaxie/beego@v1.12.3/router_test.go (about) 1 // Copyright 2014 beego Author. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package beego 16 17 import ( 18 "bytes" 19 "net/http" 20 "net/http/httptest" 21 "strings" 22 "testing" 23 24 "github.com/astaxie/beego/context" 25 "github.com/astaxie/beego/logs" 26 ) 27 28 type TestController struct { 29 Controller 30 } 31 32 func (tc *TestController) Get() { 33 tc.Data["Username"] = "astaxie" 34 tc.Ctx.Output.Body([]byte("ok")) 35 } 36 37 func (tc *TestController) Post() { 38 tc.Ctx.Output.Body([]byte(tc.Ctx.Input.Query(":name"))) 39 } 40 41 func (tc *TestController) Param() { 42 tc.Ctx.Output.Body([]byte(tc.Ctx.Input.Query(":name"))) 43 } 44 45 func (tc *TestController) List() { 46 tc.Ctx.Output.Body([]byte("i am list")) 47 } 48 49 func (tc *TestController) Params() { 50 tc.Ctx.Output.Body([]byte(tc.Ctx.Input.Param("0") + tc.Ctx.Input.Param("1") + tc.Ctx.Input.Param("2"))) 51 } 52 53 func (tc *TestController) Myext() { 54 tc.Ctx.Output.Body([]byte(tc.Ctx.Input.Param(":ext"))) 55 } 56 57 func (tc *TestController) GetURL() { 58 tc.Ctx.Output.Body([]byte(tc.URLFor(".Myext"))) 59 } 60 61 func (tc *TestController) GetParams() { 62 tc.Ctx.WriteString(tc.Ctx.Input.Query(":last") + "+" + 63 tc.Ctx.Input.Query(":first") + "+" + tc.Ctx.Input.Query("learn")) 64 } 65 66 func (tc *TestController) GetManyRouter() { 67 tc.Ctx.WriteString(tc.Ctx.Input.Query(":id") + tc.Ctx.Input.Query(":page")) 68 } 69 70 func (tc *TestController) GetEmptyBody() { 71 var res []byte 72 tc.Ctx.Output.Body(res) 73 } 74 75 type JSONController struct { 76 Controller 77 } 78 79 func (jc *JSONController) Prepare() { 80 jc.Data["json"] = "prepare" 81 jc.ServeJSON(true) 82 } 83 84 func (jc *JSONController) Get() { 85 jc.Data["Username"] = "astaxie" 86 jc.Ctx.Output.Body([]byte("ok")) 87 } 88 89 func TestUrlFor(t *testing.T) { 90 handler := NewControllerRegister() 91 handler.Add("/api/list", &TestController{}, "*:List") 92 handler.Add("/person/:last/:first", &TestController{}, "*:Param") 93 if a := handler.URLFor("TestController.List"); a != "/api/list" { 94 logs.Info(a) 95 t.Errorf("TestController.List must equal to /api/list") 96 } 97 if a := handler.URLFor("TestController.Param", ":last", "xie", ":first", "asta"); a != "/person/xie/asta" { 98 t.Errorf("TestController.Param must equal to /person/xie/asta, but get " + a) 99 } 100 } 101 102 func TestUrlFor3(t *testing.T) { 103 handler := NewControllerRegister() 104 handler.AddAuto(&TestController{}) 105 if a := handler.URLFor("TestController.Myext"); a != "/test/myext" && a != "/Test/Myext" { 106 t.Errorf("TestController.Myext must equal to /test/myext, but get " + a) 107 } 108 if a := handler.URLFor("TestController.GetURL"); a != "/test/geturl" && a != "/Test/GetURL" { 109 t.Errorf("TestController.GetURL must equal to /test/geturl, but get " + a) 110 } 111 } 112 113 func TestUrlFor2(t *testing.T) { 114 handler := NewControllerRegister() 115 handler.Add("/v1/:v/cms_:id(.+)_:page(.+).html", &TestController{}, "*:List") 116 handler.Add("/v1/:username/edit", &TestController{}, "get:GetURL") 117 handler.Add("/v1/:v(.+)_cms/ttt_:id(.+)_:page(.+).html", &TestController{}, "*:Param") 118 handler.Add("/:year:int/:month:int/:title/:entid", &TestController{}) 119 if handler.URLFor("TestController.GetURL", ":username", "astaxie") != "/v1/astaxie/edit" { 120 logs.Info(handler.URLFor("TestController.GetURL")) 121 t.Errorf("TestController.List must equal to /v1/astaxie/edit") 122 } 123 124 if handler.URLFor("TestController.List", ":v", "za", ":id", "12", ":page", "123") != 125 "/v1/za/cms_12_123.html" { 126 logs.Info(handler.URLFor("TestController.List")) 127 t.Errorf("TestController.List must equal to /v1/za/cms_12_123.html") 128 } 129 if handler.URLFor("TestController.Param", ":v", "za", ":id", "12", ":page", "123") != 130 "/v1/za_cms/ttt_12_123.html" { 131 logs.Info(handler.URLFor("TestController.Param")) 132 t.Errorf("TestController.List must equal to /v1/za_cms/ttt_12_123.html") 133 } 134 if handler.URLFor("TestController.Get", ":year", "1111", ":month", "11", 135 ":title", "aaaa", ":entid", "aaaa") != 136 "/1111/11/aaaa/aaaa" { 137 logs.Info(handler.URLFor("TestController.Get")) 138 t.Errorf("TestController.Get must equal to /1111/11/aaaa/aaaa") 139 } 140 } 141 142 func TestUserFunc(t *testing.T) { 143 r, _ := http.NewRequest("GET", "/api/list", nil) 144 w := httptest.NewRecorder() 145 146 handler := NewControllerRegister() 147 handler.Add("/api/list", &TestController{}, "*:List") 148 handler.ServeHTTP(w, r) 149 if w.Body.String() != "i am list" { 150 t.Errorf("user define func can't run") 151 } 152 } 153 154 func TestPostFunc(t *testing.T) { 155 r, _ := http.NewRequest("POST", "/astaxie", nil) 156 w := httptest.NewRecorder() 157 158 handler := NewControllerRegister() 159 handler.Add("/:name", &TestController{}) 160 handler.ServeHTTP(w, r) 161 if w.Body.String() != "astaxie" { 162 t.Errorf("post func should astaxie") 163 } 164 } 165 166 func TestAutoFunc(t *testing.T) { 167 r, _ := http.NewRequest("GET", "/test/list", nil) 168 w := httptest.NewRecorder() 169 170 handler := NewControllerRegister() 171 handler.AddAuto(&TestController{}) 172 handler.ServeHTTP(w, r) 173 if w.Body.String() != "i am list" { 174 t.Errorf("user define func can't run") 175 } 176 } 177 178 func TestAutoFunc2(t *testing.T) { 179 r, _ := http.NewRequest("GET", "/Test/List", nil) 180 w := httptest.NewRecorder() 181 182 handler := NewControllerRegister() 183 handler.AddAuto(&TestController{}) 184 handler.ServeHTTP(w, r) 185 if w.Body.String() != "i am list" { 186 t.Errorf("user define func can't run") 187 } 188 } 189 190 func TestAutoFuncParams(t *testing.T) { 191 r, _ := http.NewRequest("GET", "/test/params/2009/11/12", nil) 192 w := httptest.NewRecorder() 193 194 handler := NewControllerRegister() 195 handler.AddAuto(&TestController{}) 196 handler.ServeHTTP(w, r) 197 if w.Body.String() != "20091112" { 198 t.Errorf("user define func can't run") 199 } 200 } 201 202 func TestAutoExtFunc(t *testing.T) { 203 r, _ := http.NewRequest("GET", "/test/myext.json", nil) 204 w := httptest.NewRecorder() 205 206 handler := NewControllerRegister() 207 handler.AddAuto(&TestController{}) 208 handler.ServeHTTP(w, r) 209 if w.Body.String() != "json" { 210 t.Errorf("user define func can't run") 211 } 212 } 213 214 func TestRouteOk(t *testing.T) { 215 216 r, _ := http.NewRequest("GET", "/person/anderson/thomas?learn=kungfu", nil) 217 w := httptest.NewRecorder() 218 219 handler := NewControllerRegister() 220 handler.Add("/person/:last/:first", &TestController{}, "get:GetParams") 221 handler.ServeHTTP(w, r) 222 body := w.Body.String() 223 if body != "anderson+thomas+kungfu" { 224 t.Errorf("url param set to [%s];", body) 225 } 226 } 227 228 func TestManyRoute(t *testing.T) { 229 230 r, _ := http.NewRequest("GET", "/beego32-12.html", nil) 231 w := httptest.NewRecorder() 232 233 handler := NewControllerRegister() 234 handler.Add("/beego:id([0-9]+)-:page([0-9]+).html", &TestController{}, "get:GetManyRouter") 235 handler.ServeHTTP(w, r) 236 237 body := w.Body.String() 238 239 if body != "3212" { 240 t.Errorf("url param set to [%s];", body) 241 } 242 } 243 244 // Test for issue #1669 245 func TestEmptyResponse(t *testing.T) { 246 247 r, _ := http.NewRequest("GET", "/beego-empty.html", nil) 248 w := httptest.NewRecorder() 249 250 handler := NewControllerRegister() 251 handler.Add("/beego-empty.html", &TestController{}, "get:GetEmptyBody") 252 handler.ServeHTTP(w, r) 253 254 if body := w.Body.String(); body != "" { 255 t.Error("want empty body") 256 } 257 } 258 259 func TestNotFound(t *testing.T) { 260 r, _ := http.NewRequest("GET", "/", nil) 261 w := httptest.NewRecorder() 262 263 handler := NewControllerRegister() 264 handler.ServeHTTP(w, r) 265 266 if w.Code != http.StatusNotFound { 267 t.Errorf("Code set to [%v]; want [%v]", w.Code, http.StatusNotFound) 268 } 269 } 270 271 // TestStatic tests the ability to serve static 272 // content from the filesystem 273 func TestStatic(t *testing.T) { 274 r, _ := http.NewRequest("GET", "/static/js/jquery.js", nil) 275 w := httptest.NewRecorder() 276 277 handler := NewControllerRegister() 278 handler.ServeHTTP(w, r) 279 280 if w.Code != 404 { 281 t.Errorf("handler.Static failed to serve file") 282 } 283 } 284 285 func TestPrepare(t *testing.T) { 286 r, _ := http.NewRequest("GET", "/json/list", nil) 287 w := httptest.NewRecorder() 288 289 handler := NewControllerRegister() 290 handler.Add("/json/list", &JSONController{}) 291 handler.ServeHTTP(w, r) 292 if w.Body.String() != `"prepare"` { 293 t.Errorf(w.Body.String() + "user define func can't run") 294 } 295 } 296 297 func TestAutoPrefix(t *testing.T) { 298 r, _ := http.NewRequest("GET", "/admin/test/list", nil) 299 w := httptest.NewRecorder() 300 301 handler := NewControllerRegister() 302 handler.AddAutoPrefix("/admin", &TestController{}) 303 handler.ServeHTTP(w, r) 304 if w.Body.String() != "i am list" { 305 t.Errorf("TestAutoPrefix can't run") 306 } 307 } 308 309 func TestRouterGet(t *testing.T) { 310 r, _ := http.NewRequest("GET", "/user", nil) 311 w := httptest.NewRecorder() 312 313 handler := NewControllerRegister() 314 handler.Get("/user", func(ctx *context.Context) { 315 ctx.Output.Body([]byte("Get userlist")) 316 }) 317 handler.ServeHTTP(w, r) 318 if w.Body.String() != "Get userlist" { 319 t.Errorf("TestRouterGet can't run") 320 } 321 } 322 323 func TestRouterPost(t *testing.T) { 324 r, _ := http.NewRequest("POST", "/user/123", nil) 325 w := httptest.NewRecorder() 326 327 handler := NewControllerRegister() 328 handler.Post("/user/:id", func(ctx *context.Context) { 329 ctx.Output.Body([]byte(ctx.Input.Param(":id"))) 330 }) 331 handler.ServeHTTP(w, r) 332 if w.Body.String() != "123" { 333 t.Errorf("TestRouterPost can't run") 334 } 335 } 336 337 func sayhello(w http.ResponseWriter, r *http.Request) { 338 w.Write([]byte("sayhello")) 339 } 340 341 func TestRouterHandler(t *testing.T) { 342 r, _ := http.NewRequest("POST", "/sayhi", nil) 343 w := httptest.NewRecorder() 344 345 handler := NewControllerRegister() 346 handler.Handler("/sayhi", http.HandlerFunc(sayhello)) 347 handler.ServeHTTP(w, r) 348 if w.Body.String() != "sayhello" { 349 t.Errorf("TestRouterHandler can't run") 350 } 351 } 352 353 func TestRouterHandlerAll(t *testing.T) { 354 r, _ := http.NewRequest("POST", "/sayhi/a/b/c", nil) 355 w := httptest.NewRecorder() 356 357 handler := NewControllerRegister() 358 handler.Handler("/sayhi", http.HandlerFunc(sayhello), true) 359 handler.ServeHTTP(w, r) 360 if w.Body.String() != "sayhello" { 361 t.Errorf("TestRouterHandler can't run") 362 } 363 } 364 365 // 366 // Benchmarks NewApp: 367 // 368 369 func beegoFilterFunc(ctx *context.Context) { 370 ctx.WriteString("hello") 371 } 372 373 type AdminController struct { 374 Controller 375 } 376 377 func (a *AdminController) Get() { 378 a.Ctx.WriteString("hello") 379 } 380 381 func TestRouterFunc(t *testing.T) { 382 mux := NewControllerRegister() 383 mux.Get("/action", beegoFilterFunc) 384 mux.Post("/action", beegoFilterFunc) 385 rw, r := testRequest("GET", "/action") 386 mux.ServeHTTP(rw, r) 387 if rw.Body.String() != "hello" { 388 t.Errorf("TestRouterFunc can't run") 389 } 390 } 391 392 func BenchmarkFunc(b *testing.B) { 393 mux := NewControllerRegister() 394 mux.Get("/action", beegoFilterFunc) 395 rw, r := testRequest("GET", "/action") 396 b.ResetTimer() 397 for i := 0; i < b.N; i++ { 398 mux.ServeHTTP(rw, r) 399 } 400 } 401 402 func BenchmarkController(b *testing.B) { 403 mux := NewControllerRegister() 404 mux.Add("/action", &AdminController{}) 405 rw, r := testRequest("GET", "/action") 406 b.ResetTimer() 407 for i := 0; i < b.N; i++ { 408 mux.ServeHTTP(rw, r) 409 } 410 } 411 412 func testRequest(method, path string) (*httptest.ResponseRecorder, *http.Request) { 413 request, _ := http.NewRequest(method, path, nil) 414 recorder := httptest.NewRecorder() 415 416 return recorder, request 417 } 418 419 // Expectation: A Filter with the correct configuration should be created given 420 // specific parameters. 421 func TestInsertFilter(t *testing.T) { 422 testName := "TestInsertFilter" 423 424 mux := NewControllerRegister() 425 mux.InsertFilter("*", BeforeRouter, func(*context.Context) {}) 426 if !mux.filters[BeforeRouter][0].returnOnOutput { 427 t.Errorf( 428 "%s: passing no variadic params should set returnOnOutput to true", 429 testName) 430 } 431 if mux.filters[BeforeRouter][0].resetParams { 432 t.Errorf( 433 "%s: passing no variadic params should set resetParams to false", 434 testName) 435 } 436 437 mux = NewControllerRegister() 438 mux.InsertFilter("*", BeforeRouter, func(*context.Context) {}, false) 439 if mux.filters[BeforeRouter][0].returnOnOutput { 440 t.Errorf( 441 "%s: passing false as 1st variadic param should set returnOnOutput to false", 442 testName) 443 } 444 445 mux = NewControllerRegister() 446 mux.InsertFilter("*", BeforeRouter, func(*context.Context) {}, true, true) 447 if !mux.filters[BeforeRouter][0].resetParams { 448 t.Errorf( 449 "%s: passing true as 2nd variadic param should set resetParams to true", 450 testName) 451 } 452 } 453 454 // Expectation: the second variadic arg should cause the execution of the filter 455 // to preserve the parameters from before its execution. 456 func TestParamResetFilter(t *testing.T) { 457 testName := "TestParamResetFilter" 458 route := "/beego/*" // splat 459 path := "/beego/routes/routes" 460 461 mux := NewControllerRegister() 462 463 mux.InsertFilter("*", BeforeExec, beegoResetParams, true, true) 464 465 mux.Get(route, beegoHandleResetParams) 466 467 rw, r := testRequest("GET", path) 468 mux.ServeHTTP(rw, r) 469 470 // The two functions, `beegoResetParams` and `beegoHandleResetParams` add 471 // a response header of `Splat`. The expectation here is that that Header 472 // value should match what the _request's_ router set, not the filter's. 473 474 headers := rw.Result().Header 475 if len(headers["Splat"]) != 1 { 476 t.Errorf( 477 "%s: There was an error in the test. Splat param not set in Header", 478 testName) 479 } 480 if headers["Splat"][0] != "routes/routes" { 481 t.Errorf( 482 "%s: expected `:splat` param to be [routes/routes] but it was [%s]", 483 testName, headers["Splat"][0]) 484 } 485 } 486 487 // Execution point: BeforeRouter 488 // expectation: only BeforeRouter function is executed, notmatch output as router doesn't handle 489 func TestFilterBeforeRouter(t *testing.T) { 490 testName := "TestFilterBeforeRouter" 491 url := "/beforeRouter" 492 493 mux := NewControllerRegister() 494 mux.InsertFilter(url, BeforeRouter, beegoBeforeRouter1) 495 496 mux.Get(url, beegoFilterFunc) 497 498 rw, r := testRequest("GET", url) 499 mux.ServeHTTP(rw, r) 500 501 if !strings.Contains(rw.Body.String(), "BeforeRouter1") { 502 t.Errorf(testName + " BeforeRouter did not run") 503 } 504 if strings.Contains(rw.Body.String(), "hello") { 505 t.Errorf(testName + " BeforeRouter did not return properly") 506 } 507 } 508 509 // Execution point: BeforeExec 510 // expectation: only BeforeExec function is executed, match as router determines route only 511 func TestFilterBeforeExec(t *testing.T) { 512 testName := "TestFilterBeforeExec" 513 url := "/beforeExec" 514 515 mux := NewControllerRegister() 516 mux.InsertFilter(url, BeforeRouter, beegoFilterNoOutput) 517 mux.InsertFilter(url, BeforeExec, beegoBeforeExec1) 518 519 mux.Get(url, beegoFilterFunc) 520 521 rw, r := testRequest("GET", url) 522 mux.ServeHTTP(rw, r) 523 524 if !strings.Contains(rw.Body.String(), "BeforeExec1") { 525 t.Errorf(testName + " BeforeExec did not run") 526 } 527 if strings.Contains(rw.Body.String(), "hello") { 528 t.Errorf(testName + " BeforeExec did not return properly") 529 } 530 if strings.Contains(rw.Body.String(), "BeforeRouter") { 531 t.Errorf(testName + " BeforeRouter ran in error") 532 } 533 } 534 535 // Execution point: AfterExec 536 // expectation: only AfterExec function is executed, match as router handles 537 func TestFilterAfterExec(t *testing.T) { 538 testName := "TestFilterAfterExec" 539 url := "/afterExec" 540 541 mux := NewControllerRegister() 542 mux.InsertFilter(url, BeforeRouter, beegoFilterNoOutput) 543 mux.InsertFilter(url, BeforeExec, beegoFilterNoOutput) 544 mux.InsertFilter(url, AfterExec, beegoAfterExec1, false) 545 546 mux.Get(url, beegoFilterFunc) 547 548 rw, r := testRequest("GET", url) 549 mux.ServeHTTP(rw, r) 550 551 if !strings.Contains(rw.Body.String(), "AfterExec1") { 552 t.Errorf(testName + " AfterExec did not run") 553 } 554 if !strings.Contains(rw.Body.String(), "hello") { 555 t.Errorf(testName + " handler did not run properly") 556 } 557 if strings.Contains(rw.Body.String(), "BeforeRouter") { 558 t.Errorf(testName + " BeforeRouter ran in error") 559 } 560 if strings.Contains(rw.Body.String(), "BeforeExec") { 561 t.Errorf(testName + " BeforeExec ran in error") 562 } 563 } 564 565 // Execution point: FinishRouter 566 // expectation: only FinishRouter function is executed, match as router handles 567 func TestFilterFinishRouter(t *testing.T) { 568 testName := "TestFilterFinishRouter" 569 url := "/finishRouter" 570 571 mux := NewControllerRegister() 572 mux.InsertFilter(url, BeforeRouter, beegoFilterNoOutput) 573 mux.InsertFilter(url, BeforeExec, beegoFilterNoOutput) 574 mux.InsertFilter(url, AfterExec, beegoFilterNoOutput) 575 mux.InsertFilter(url, FinishRouter, beegoFinishRouter1) 576 577 mux.Get(url, beegoFilterFunc) 578 579 rw, r := testRequest("GET", url) 580 mux.ServeHTTP(rw, r) 581 582 if strings.Contains(rw.Body.String(), "FinishRouter1") { 583 t.Errorf(testName + " FinishRouter did not run") 584 } 585 if !strings.Contains(rw.Body.String(), "hello") { 586 t.Errorf(testName + " handler did not run properly") 587 } 588 if strings.Contains(rw.Body.String(), "AfterExec1") { 589 t.Errorf(testName + " AfterExec ran in error") 590 } 591 if strings.Contains(rw.Body.String(), "BeforeRouter") { 592 t.Errorf(testName + " BeforeRouter ran in error") 593 } 594 if strings.Contains(rw.Body.String(), "BeforeExec") { 595 t.Errorf(testName + " BeforeExec ran in error") 596 } 597 } 598 599 // Execution point: FinishRouter 600 // expectation: only first FinishRouter function is executed, match as router handles 601 func TestFilterFinishRouterMultiFirstOnly(t *testing.T) { 602 testName := "TestFilterFinishRouterMultiFirstOnly" 603 url := "/finishRouterMultiFirstOnly" 604 605 mux := NewControllerRegister() 606 mux.InsertFilter(url, FinishRouter, beegoFinishRouter1, false) 607 mux.InsertFilter(url, FinishRouter, beegoFinishRouter2) 608 609 mux.Get(url, beegoFilterFunc) 610 611 rw, r := testRequest("GET", url) 612 mux.ServeHTTP(rw, r) 613 614 if !strings.Contains(rw.Body.String(), "FinishRouter1") { 615 t.Errorf(testName + " FinishRouter1 did not run") 616 } 617 if !strings.Contains(rw.Body.String(), "hello") { 618 t.Errorf(testName + " handler did not run properly") 619 } 620 // not expected in body 621 if strings.Contains(rw.Body.String(), "FinishRouter2") { 622 t.Errorf(testName + " FinishRouter2 did run") 623 } 624 } 625 626 // Execution point: FinishRouter 627 // expectation: both FinishRouter functions execute, match as router handles 628 func TestFilterFinishRouterMulti(t *testing.T) { 629 testName := "TestFilterFinishRouterMulti" 630 url := "/finishRouterMulti" 631 632 mux := NewControllerRegister() 633 mux.InsertFilter(url, FinishRouter, beegoFinishRouter1, false) 634 mux.InsertFilter(url, FinishRouter, beegoFinishRouter2, false) 635 636 mux.Get(url, beegoFilterFunc) 637 638 rw, r := testRequest("GET", url) 639 mux.ServeHTTP(rw, r) 640 641 if !strings.Contains(rw.Body.String(), "FinishRouter1") { 642 t.Errorf(testName + " FinishRouter1 did not run") 643 } 644 if !strings.Contains(rw.Body.String(), "hello") { 645 t.Errorf(testName + " handler did not run properly") 646 } 647 if !strings.Contains(rw.Body.String(), "FinishRouter2") { 648 t.Errorf(testName + " FinishRouter2 did not run properly") 649 } 650 } 651 652 func beegoFilterNoOutput(ctx *context.Context) { 653 } 654 655 func beegoBeforeRouter1(ctx *context.Context) { 656 ctx.WriteString("|BeforeRouter1") 657 } 658 659 func beegoBeforeExec1(ctx *context.Context) { 660 ctx.WriteString("|BeforeExec1") 661 } 662 663 func beegoAfterExec1(ctx *context.Context) { 664 ctx.WriteString("|AfterExec1") 665 } 666 667 func beegoFinishRouter1(ctx *context.Context) { 668 ctx.WriteString("|FinishRouter1") 669 } 670 671 func beegoFinishRouter2(ctx *context.Context) { 672 ctx.WriteString("|FinishRouter2") 673 } 674 675 func beegoResetParams(ctx *context.Context) { 676 ctx.ResponseWriter.Header().Set("splat", ctx.Input.Param(":splat")) 677 } 678 679 func beegoHandleResetParams(ctx *context.Context) { 680 ctx.ResponseWriter.Header().Set("splat", ctx.Input.Param(":splat")) 681 } 682 683 // YAML 684 type YAMLController struct { 685 Controller 686 } 687 688 func (jc *YAMLController) Prepare() { 689 jc.Data["yaml"] = "prepare" 690 jc.ServeYAML() 691 } 692 693 func (jc *YAMLController) Get() { 694 jc.Data["Username"] = "astaxie" 695 jc.Ctx.Output.Body([]byte("ok")) 696 } 697 698 func TestYAMLPrepare(t *testing.T) { 699 r, _ := http.NewRequest("GET", "/yaml/list", nil) 700 w := httptest.NewRecorder() 701 702 handler := NewControllerRegister() 703 handler.Add("/yaml/list", &YAMLController{}) 704 handler.ServeHTTP(w, r) 705 if strings.TrimSpace(w.Body.String()) != "prepare" { 706 t.Errorf(w.Body.String()) 707 } 708 } 709 710 func TestRouterEntityTooLargeCopyBody(t *testing.T) { 711 _MaxMemory := BConfig.MaxMemory 712 _CopyRequestBody := BConfig.CopyRequestBody 713 BConfig.CopyRequestBody = true 714 BConfig.MaxMemory = 20 715 716 b := bytes.NewBuffer([]byte("barbarbarbarbarbarbarbarbarbar")) 717 r, _ := http.NewRequest("POST", "/user/123", b) 718 w := httptest.NewRecorder() 719 720 handler := NewControllerRegister() 721 handler.Post("/user/:id", func(ctx *context.Context) { 722 ctx.Output.Body([]byte(ctx.Input.Param(":id"))) 723 }) 724 handler.ServeHTTP(w, r) 725 726 BConfig.CopyRequestBody = _CopyRequestBody 727 BConfig.MaxMemory = _MaxMemory 728 729 if w.Code != http.StatusRequestEntityTooLarge { 730 t.Errorf("TestRouterRequestEntityTooLarge can't run") 731 } 732 }