router_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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. package beego
  15. import (
  16. "net/http"
  17. "net/http/httptest"
  18. "strings"
  19. "testing"
  20. "github.com/astaxie/beego/context"
  21. "github.com/astaxie/beego/logs"
  22. )
  23. type TestController struct {
  24. Controller
  25. }
  26. func (tc *TestController) Get() {
  27. tc.Data["Username"] = "astaxie"
  28. tc.Ctx.Output.Body([]byte("ok"))
  29. }
  30. func (tc *TestController) Post() {
  31. tc.Ctx.Output.Body([]byte(tc.Ctx.Input.Query(":name")))
  32. }
  33. func (tc *TestController) Param() {
  34. tc.Ctx.Output.Body([]byte(tc.Ctx.Input.Query(":name")))
  35. }
  36. func (tc *TestController) List() {
  37. tc.Ctx.Output.Body([]byte("i am list"))
  38. }
  39. func (tc *TestController) Params() {
  40. tc.Ctx.Output.Body([]byte(tc.Ctx.Input.Param("0") + tc.Ctx.Input.Param("1") + tc.Ctx.Input.Param("2")))
  41. }
  42. func (tc *TestController) Myext() {
  43. tc.Ctx.Output.Body([]byte(tc.Ctx.Input.Param(":ext")))
  44. }
  45. func (tc *TestController) GetURL() {
  46. tc.Ctx.Output.Body([]byte(tc.URLFor(".Myext")))
  47. }
  48. func (tc *TestController) GetParams() {
  49. tc.Ctx.WriteString(tc.Ctx.Input.Query(":last") + "+" +
  50. tc.Ctx.Input.Query(":first") + "+" + tc.Ctx.Input.Query("learn"))
  51. }
  52. func (tc *TestController) GetManyRouter() {
  53. tc.Ctx.WriteString(tc.Ctx.Input.Query(":id") + tc.Ctx.Input.Query(":page"))
  54. }
  55. func (tc *TestController) GetEmptyBody() {
  56. var res []byte
  57. tc.Ctx.Output.Body(res)
  58. }
  59. type ResStatus struct {
  60. Code int
  61. Msg string
  62. }
  63. type JSONController struct {
  64. Controller
  65. }
  66. func (jc *JSONController) Prepare() {
  67. jc.Data["json"] = "prepare"
  68. jc.ServeJSON(true)
  69. }
  70. func (jc *JSONController) Get() {
  71. jc.Data["Username"] = "astaxie"
  72. jc.Ctx.Output.Body([]byte("ok"))
  73. }
  74. func TestUrlFor(t *testing.T) {
  75. handler := NewControllerRegister()
  76. handler.Add("/api/list", &TestController{}, "*:List")
  77. handler.Add("/person/:last/:first", &TestController{}, "*:Param")
  78. if a := handler.URLFor("TestController.List"); a != "/api/list" {
  79. logs.Info(a)
  80. t.Errorf("TestController.List must equal to /api/list")
  81. }
  82. if a := handler.URLFor("TestController.Param", ":last", "xie", ":first", "asta"); a != "/person/xie/asta" {
  83. t.Errorf("TestController.Param must equal to /person/xie/asta, but get " + a)
  84. }
  85. }
  86. func TestUrlFor3(t *testing.T) {
  87. handler := NewControllerRegister()
  88. handler.AddAuto(&TestController{})
  89. if a := handler.URLFor("TestController.Myext"); a != "/test/myext" && a != "/Test/Myext" {
  90. t.Errorf("TestController.Myext must equal to /test/myext, but get " + a)
  91. }
  92. if a := handler.URLFor("TestController.GetURL"); a != "/test/geturl" && a != "/Test/GetURL" {
  93. t.Errorf("TestController.GetURL must equal to /test/geturl, but get " + a)
  94. }
  95. }
  96. func TestUrlFor2(t *testing.T) {
  97. handler := NewControllerRegister()
  98. handler.Add("/v1/:v/cms_:id(.+)_:page(.+).html", &TestController{}, "*:List")
  99. handler.Add("/v1/:username/edit", &TestController{}, "get:GetURL")
  100. handler.Add("/v1/:v(.+)_cms/ttt_:id(.+)_:page(.+).html", &TestController{}, "*:Param")
  101. handler.Add("/:year:int/:month:int/:title/:entid", &TestController{})
  102. if handler.URLFor("TestController.GetURL", ":username", "astaxie") != "/v1/astaxie/edit" {
  103. logs.Info(handler.URLFor("TestController.GetURL"))
  104. t.Errorf("TestController.List must equal to /v1/astaxie/edit")
  105. }
  106. if handler.URLFor("TestController.List", ":v", "za", ":id", "12", ":page", "123") !=
  107. "/v1/za/cms_12_123.html" {
  108. logs.Info(handler.URLFor("TestController.List"))
  109. t.Errorf("TestController.List must equal to /v1/za/cms_12_123.html")
  110. }
  111. if handler.URLFor("TestController.Param", ":v", "za", ":id", "12", ":page", "123") !=
  112. "/v1/za_cms/ttt_12_123.html" {
  113. logs.Info(handler.URLFor("TestController.Param"))
  114. t.Errorf("TestController.List must equal to /v1/za_cms/ttt_12_123.html")
  115. }
  116. if handler.URLFor("TestController.Get", ":year", "1111", ":month", "11",
  117. ":title", "aaaa", ":entid", "aaaa") !=
  118. "/1111/11/aaaa/aaaa" {
  119. logs.Info(handler.URLFor("TestController.Get"))
  120. t.Errorf("TestController.Get must equal to /1111/11/aaaa/aaaa")
  121. }
  122. }
  123. func TestUserFunc(t *testing.T) {
  124. r, _ := http.NewRequest("GET", "/api/list", nil)
  125. w := httptest.NewRecorder()
  126. handler := NewControllerRegister()
  127. handler.Add("/api/list", &TestController{}, "*:List")
  128. handler.ServeHTTP(w, r)
  129. if w.Body.String() != "i am list" {
  130. t.Errorf("user define func can't run")
  131. }
  132. }
  133. func TestPostFunc(t *testing.T) {
  134. r, _ := http.NewRequest("POST", "/astaxie", nil)
  135. w := httptest.NewRecorder()
  136. handler := NewControllerRegister()
  137. handler.Add("/:name", &TestController{})
  138. handler.ServeHTTP(w, r)
  139. if w.Body.String() != "astaxie" {
  140. t.Errorf("post func should astaxie")
  141. }
  142. }
  143. func TestAutoFunc(t *testing.T) {
  144. r, _ := http.NewRequest("GET", "/test/list", nil)
  145. w := httptest.NewRecorder()
  146. handler := NewControllerRegister()
  147. handler.AddAuto(&TestController{})
  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. func TestAutoFunc2(t *testing.T) {
  154. r, _ := http.NewRequest("GET", "/Test/List", nil)
  155. w := httptest.NewRecorder()
  156. handler := NewControllerRegister()
  157. handler.AddAuto(&TestController{})
  158. handler.ServeHTTP(w, r)
  159. if w.Body.String() != "i am list" {
  160. t.Errorf("user define func can't run")
  161. }
  162. }
  163. func TestAutoFuncParams(t *testing.T) {
  164. r, _ := http.NewRequest("GET", "/test/params/2009/11/12", nil)
  165. w := httptest.NewRecorder()
  166. handler := NewControllerRegister()
  167. handler.AddAuto(&TestController{})
  168. handler.ServeHTTP(w, r)
  169. if w.Body.String() != "20091112" {
  170. t.Errorf("user define func can't run")
  171. }
  172. }
  173. func TestAutoExtFunc(t *testing.T) {
  174. r, _ := http.NewRequest("GET", "/test/myext.json", nil)
  175. w := httptest.NewRecorder()
  176. handler := NewControllerRegister()
  177. handler.AddAuto(&TestController{})
  178. handler.ServeHTTP(w, r)
  179. if w.Body.String() != "json" {
  180. t.Errorf("user define func can't run")
  181. }
  182. }
  183. func TestRouteOk(t *testing.T) {
  184. r, _ := http.NewRequest("GET", "/person/anderson/thomas?learn=kungfu", nil)
  185. w := httptest.NewRecorder()
  186. handler := NewControllerRegister()
  187. handler.Add("/person/:last/:first", &TestController{}, "get:GetParams")
  188. handler.ServeHTTP(w, r)
  189. body := w.Body.String()
  190. if body != "anderson+thomas+kungfu" {
  191. t.Errorf("url param set to [%s];", body)
  192. }
  193. }
  194. func TestManyRoute(t *testing.T) {
  195. r, _ := http.NewRequest("GET", "/beego32-12.html", nil)
  196. w := httptest.NewRecorder()
  197. handler := NewControllerRegister()
  198. handler.Add("/beego:id([0-9]+)-:page([0-9]+).html", &TestController{}, "get:GetManyRouter")
  199. handler.ServeHTTP(w, r)
  200. body := w.Body.String()
  201. if body != "3212" {
  202. t.Errorf("url param set to [%s];", body)
  203. }
  204. }
  205. // Test for issue #1669
  206. func TestEmptyResponse(t *testing.T) {
  207. r, _ := http.NewRequest("GET", "/beego-empty.html", nil)
  208. w := httptest.NewRecorder()
  209. handler := NewControllerRegister()
  210. handler.Add("/beego-empty.html", &TestController{}, "get:GetEmptyBody")
  211. handler.ServeHTTP(w, r)
  212. if body := w.Body.String(); body != "" {
  213. t.Error("want empty body")
  214. }
  215. }
  216. func TestNotFound(t *testing.T) {
  217. r, _ := http.NewRequest("GET", "/", nil)
  218. w := httptest.NewRecorder()
  219. handler := NewControllerRegister()
  220. handler.ServeHTTP(w, r)
  221. if w.Code != http.StatusNotFound {
  222. t.Errorf("Code set to [%v]; want [%v]", w.Code, http.StatusNotFound)
  223. }
  224. }
  225. // TestStatic tests the ability to serve static
  226. // content from the filesystem
  227. func TestStatic(t *testing.T) {
  228. r, _ := http.NewRequest("GET", "/static/js/jquery.js", nil)
  229. w := httptest.NewRecorder()
  230. handler := NewControllerRegister()
  231. handler.ServeHTTP(w, r)
  232. if w.Code != 404 {
  233. t.Errorf("handler.Static failed to serve file")
  234. }
  235. }
  236. func TestPrepare(t *testing.T) {
  237. r, _ := http.NewRequest("GET", "/json/list", nil)
  238. w := httptest.NewRecorder()
  239. handler := NewControllerRegister()
  240. handler.Add("/json/list", &JSONController{})
  241. handler.ServeHTTP(w, r)
  242. if w.Body.String() != `"prepare"` {
  243. t.Errorf(w.Body.String() + "user define func can't run")
  244. }
  245. }
  246. func TestAutoPrefix(t *testing.T) {
  247. r, _ := http.NewRequest("GET", "/admin/test/list", nil)
  248. w := httptest.NewRecorder()
  249. handler := NewControllerRegister()
  250. handler.AddAutoPrefix("/admin", &TestController{})
  251. handler.ServeHTTP(w, r)
  252. if w.Body.String() != "i am list" {
  253. t.Errorf("TestAutoPrefix can't run")
  254. }
  255. }
  256. func TestRouterGet(t *testing.T) {
  257. r, _ := http.NewRequest("GET", "/user", nil)
  258. w := httptest.NewRecorder()
  259. handler := NewControllerRegister()
  260. handler.Get("/user", func(ctx *context.Context) {
  261. ctx.Output.Body([]byte("Get userlist"))
  262. })
  263. handler.ServeHTTP(w, r)
  264. if w.Body.String() != "Get userlist" {
  265. t.Errorf("TestRouterGet can't run")
  266. }
  267. }
  268. func TestRouterPost(t *testing.T) {
  269. r, _ := http.NewRequest("POST", "/user/123", nil)
  270. w := httptest.NewRecorder()
  271. handler := NewControllerRegister()
  272. handler.Post("/user/:id", func(ctx *context.Context) {
  273. ctx.Output.Body([]byte(ctx.Input.Param(":id")))
  274. })
  275. handler.ServeHTTP(w, r)
  276. if w.Body.String() != "123" {
  277. t.Errorf("TestRouterPost can't run")
  278. }
  279. }
  280. func sayhello(w http.ResponseWriter, r *http.Request) {
  281. w.Write([]byte("sayhello"))
  282. }
  283. func TestRouterHandler(t *testing.T) {
  284. r, _ := http.NewRequest("POST", "/sayhi", nil)
  285. w := httptest.NewRecorder()
  286. handler := NewControllerRegister()
  287. handler.Handler("/sayhi", http.HandlerFunc(sayhello))
  288. handler.ServeHTTP(w, r)
  289. if w.Body.String() != "sayhello" {
  290. t.Errorf("TestRouterHandler can't run")
  291. }
  292. }
  293. func TestRouterHandlerAll(t *testing.T) {
  294. r, _ := http.NewRequest("POST", "/sayhi/a/b/c", nil)
  295. w := httptest.NewRecorder()
  296. handler := NewControllerRegister()
  297. handler.Handler("/sayhi", http.HandlerFunc(sayhello), true)
  298. handler.ServeHTTP(w, r)
  299. if w.Body.String() != "sayhello" {
  300. t.Errorf("TestRouterHandler can't run")
  301. }
  302. }
  303. //
  304. // Benchmarks NewApp:
  305. //
  306. func beegoFilterFunc(ctx *context.Context) {
  307. ctx.WriteString("hello")
  308. }
  309. type AdminController struct {
  310. Controller
  311. }
  312. func (a *AdminController) Get() {
  313. a.Ctx.WriteString("hello")
  314. }
  315. func TestRouterFunc(t *testing.T) {
  316. mux := NewControllerRegister()
  317. mux.Get("/action", beegoFilterFunc)
  318. mux.Post("/action", beegoFilterFunc)
  319. rw, r := testRequest("GET", "/action")
  320. mux.ServeHTTP(rw, r)
  321. if rw.Body.String() != "hello" {
  322. t.Errorf("TestRouterFunc can't run")
  323. }
  324. }
  325. func BenchmarkFunc(b *testing.B) {
  326. mux := NewControllerRegister()
  327. mux.Get("/action", beegoFilterFunc)
  328. rw, r := testRequest("GET", "/action")
  329. b.ResetTimer()
  330. for i := 0; i < b.N; i++ {
  331. mux.ServeHTTP(rw, r)
  332. }
  333. }
  334. func BenchmarkController(b *testing.B) {
  335. mux := NewControllerRegister()
  336. mux.Add("/action", &AdminController{})
  337. rw, r := testRequest("GET", "/action")
  338. b.ResetTimer()
  339. for i := 0; i < b.N; i++ {
  340. mux.ServeHTTP(rw, r)
  341. }
  342. }
  343. func testRequest(method, path string) (*httptest.ResponseRecorder, *http.Request) {
  344. request, _ := http.NewRequest(method, path, nil)
  345. recorder := httptest.NewRecorder()
  346. return recorder, request
  347. }
  348. // Expectation: A Filter with the correct configuration should be created given
  349. // specific parameters.
  350. func TestInsertFilter(t *testing.T) {
  351. testName := "TestInsertFilter"
  352. mux := NewControllerRegister()
  353. mux.InsertFilter("*", BeforeRouter, func(*context.Context) {})
  354. if !mux.filters[BeforeRouter][0].returnOnOutput {
  355. t.Errorf(
  356. "%s: passing no variadic params should set returnOnOutput to true",
  357. testName)
  358. }
  359. if mux.filters[BeforeRouter][0].resetParams {
  360. t.Errorf(
  361. "%s: passing no variadic params should set resetParams to false",
  362. testName)
  363. }
  364. mux = NewControllerRegister()
  365. mux.InsertFilter("*", BeforeRouter, func(*context.Context) {}, false)
  366. if mux.filters[BeforeRouter][0].returnOnOutput {
  367. t.Errorf(
  368. "%s: passing false as 1st variadic param should set returnOnOutput to false",
  369. testName)
  370. }
  371. mux = NewControllerRegister()
  372. mux.InsertFilter("*", BeforeRouter, func(*context.Context) {}, true, true)
  373. if !mux.filters[BeforeRouter][0].resetParams {
  374. t.Errorf(
  375. "%s: passing true as 2nd variadic param should set resetParams to true",
  376. testName)
  377. }
  378. }
  379. // Expectation: the second variadic arg should cause the execution of the filter
  380. // to preserve the parameters from before its execution.
  381. func TestParamResetFilter(t *testing.T) {
  382. testName := "TestParamResetFilter"
  383. route := "/beego/*" // splat
  384. path := "/beego/routes/routes"
  385. mux := NewControllerRegister()
  386. mux.InsertFilter("*", BeforeExec, beegoResetParams, true, true)
  387. mux.Get(route, beegoHandleResetParams)
  388. rw, r := testRequest("GET", path)
  389. mux.ServeHTTP(rw, r)
  390. // The two functions, `beegoResetParams` and `beegoHandleResetParams` add
  391. // a response header of `Splat`. The expectation here is that that Header
  392. // value should match what the _request's_ router set, not the filter's.
  393. headers := rw.HeaderMap
  394. if len(headers["Splat"]) != 1 {
  395. t.Errorf(
  396. "%s: There was an error in the test. Splat param not set in Header",
  397. testName)
  398. }
  399. if headers["Splat"][0] != "routes/routes" {
  400. t.Errorf(
  401. "%s: expected `:splat` param to be [routes/routes] but it was [%s]",
  402. testName, headers["Splat"][0])
  403. }
  404. }
  405. // Execution point: BeforeRouter
  406. // expectation: only BeforeRouter function is executed, notmatch output as router doesn't handle
  407. func TestFilterBeforeRouter(t *testing.T) {
  408. testName := "TestFilterBeforeRouter"
  409. url := "/beforeRouter"
  410. mux := NewControllerRegister()
  411. mux.InsertFilter(url, BeforeRouter, beegoBeforeRouter1)
  412. mux.Get(url, beegoFilterFunc)
  413. rw, r := testRequest("GET", url)
  414. mux.ServeHTTP(rw, r)
  415. if strings.Contains(rw.Body.String(), "BeforeRouter1") == false {
  416. t.Errorf(testName + " BeforeRouter did not run")
  417. }
  418. if strings.Contains(rw.Body.String(), "hello") == true {
  419. t.Errorf(testName + " BeforeRouter did not return properly")
  420. }
  421. }
  422. // Execution point: BeforeExec
  423. // expectation: only BeforeExec function is executed, match as router determines route only
  424. func TestFilterBeforeExec(t *testing.T) {
  425. testName := "TestFilterBeforeExec"
  426. url := "/beforeExec"
  427. mux := NewControllerRegister()
  428. mux.InsertFilter(url, BeforeRouter, beegoFilterNoOutput)
  429. mux.InsertFilter(url, BeforeExec, beegoBeforeExec1)
  430. mux.Get(url, beegoFilterFunc)
  431. rw, r := testRequest("GET", url)
  432. mux.ServeHTTP(rw, r)
  433. if strings.Contains(rw.Body.String(), "BeforeExec1") == false {
  434. t.Errorf(testName + " BeforeExec did not run")
  435. }
  436. if strings.Contains(rw.Body.String(), "hello") == true {
  437. t.Errorf(testName + " BeforeExec did not return properly")
  438. }
  439. if strings.Contains(rw.Body.String(), "BeforeRouter") == true {
  440. t.Errorf(testName + " BeforeRouter ran in error")
  441. }
  442. }
  443. // Execution point: AfterExec
  444. // expectation: only AfterExec function is executed, match as router handles
  445. func TestFilterAfterExec(t *testing.T) {
  446. testName := "TestFilterAfterExec"
  447. url := "/afterExec"
  448. mux := NewControllerRegister()
  449. mux.InsertFilter(url, BeforeRouter, beegoFilterNoOutput)
  450. mux.InsertFilter(url, BeforeExec, beegoFilterNoOutput)
  451. mux.InsertFilter(url, AfterExec, beegoAfterExec1, false)
  452. mux.Get(url, beegoFilterFunc)
  453. rw, r := testRequest("GET", url)
  454. mux.ServeHTTP(rw, r)
  455. if strings.Contains(rw.Body.String(), "AfterExec1") == false {
  456. t.Errorf(testName + " AfterExec did not run")
  457. }
  458. if strings.Contains(rw.Body.String(), "hello") == false {
  459. t.Errorf(testName + " handler did not run properly")
  460. }
  461. if strings.Contains(rw.Body.String(), "BeforeRouter") == true {
  462. t.Errorf(testName + " BeforeRouter ran in error")
  463. }
  464. if strings.Contains(rw.Body.String(), "BeforeExec") == true {
  465. t.Errorf(testName + " BeforeExec ran in error")
  466. }
  467. }
  468. // Execution point: FinishRouter
  469. // expectation: only FinishRouter function is executed, match as router handles
  470. func TestFilterFinishRouter(t *testing.T) {
  471. testName := "TestFilterFinishRouter"
  472. url := "/finishRouter"
  473. mux := NewControllerRegister()
  474. mux.InsertFilter(url, BeforeRouter, beegoFilterNoOutput)
  475. mux.InsertFilter(url, BeforeExec, beegoFilterNoOutput)
  476. mux.InsertFilter(url, AfterExec, beegoFilterNoOutput)
  477. mux.InsertFilter(url, FinishRouter, beegoFinishRouter1)
  478. mux.Get(url, beegoFilterFunc)
  479. rw, r := testRequest("GET", url)
  480. mux.ServeHTTP(rw, r)
  481. if strings.Contains(rw.Body.String(), "FinishRouter1") == true {
  482. t.Errorf(testName + " FinishRouter did not run")
  483. }
  484. if strings.Contains(rw.Body.String(), "hello") == false {
  485. t.Errorf(testName + " handler did not run properly")
  486. }
  487. if strings.Contains(rw.Body.String(), "AfterExec1") == true {
  488. t.Errorf(testName + " AfterExec ran in error")
  489. }
  490. if strings.Contains(rw.Body.String(), "BeforeRouter") == true {
  491. t.Errorf(testName + " BeforeRouter ran in error")
  492. }
  493. if strings.Contains(rw.Body.String(), "BeforeExec") == true {
  494. t.Errorf(testName + " BeforeExec ran in error")
  495. }
  496. }
  497. // Execution point: FinishRouter
  498. // expectation: only first FinishRouter function is executed, match as router handles
  499. func TestFilterFinishRouterMultiFirstOnly(t *testing.T) {
  500. testName := "TestFilterFinishRouterMultiFirstOnly"
  501. url := "/finishRouterMultiFirstOnly"
  502. mux := NewControllerRegister()
  503. mux.InsertFilter(url, FinishRouter, beegoFinishRouter1, false)
  504. mux.InsertFilter(url, FinishRouter, beegoFinishRouter2)
  505. mux.Get(url, beegoFilterFunc)
  506. rw, r := testRequest("GET", url)
  507. mux.ServeHTTP(rw, r)
  508. if strings.Contains(rw.Body.String(), "FinishRouter1") == false {
  509. t.Errorf(testName + " FinishRouter1 did not run")
  510. }
  511. if strings.Contains(rw.Body.String(), "hello") == false {
  512. t.Errorf(testName + " handler did not run properly")
  513. }
  514. // not expected in body
  515. if strings.Contains(rw.Body.String(), "FinishRouter2") == true {
  516. t.Errorf(testName + " FinishRouter2 did run")
  517. }
  518. }
  519. // Execution point: FinishRouter
  520. // expectation: both FinishRouter functions execute, match as router handles
  521. func TestFilterFinishRouterMulti(t *testing.T) {
  522. testName := "TestFilterFinishRouterMulti"
  523. url := "/finishRouterMulti"
  524. mux := NewControllerRegister()
  525. mux.InsertFilter(url, FinishRouter, beegoFinishRouter1, false)
  526. mux.InsertFilter(url, FinishRouter, beegoFinishRouter2, false)
  527. mux.Get(url, beegoFilterFunc)
  528. rw, r := testRequest("GET", url)
  529. mux.ServeHTTP(rw, r)
  530. if strings.Contains(rw.Body.String(), "FinishRouter1") == false {
  531. t.Errorf(testName + " FinishRouter1 did not run")
  532. }
  533. if strings.Contains(rw.Body.String(), "hello") == false {
  534. t.Errorf(testName + " handler did not run properly")
  535. }
  536. if strings.Contains(rw.Body.String(), "FinishRouter2") == false {
  537. t.Errorf(testName + " FinishRouter2 did not run properly")
  538. }
  539. }
  540. func beegoFilterNoOutput(ctx *context.Context) {
  541. return
  542. }
  543. func beegoBeforeRouter1(ctx *context.Context) {
  544. ctx.WriteString("|BeforeRouter1")
  545. }
  546. func beegoBeforeRouter2(ctx *context.Context) {
  547. ctx.WriteString("|BeforeRouter2")
  548. }
  549. func beegoBeforeExec1(ctx *context.Context) {
  550. ctx.WriteString("|BeforeExec1")
  551. }
  552. func beegoBeforeExec2(ctx *context.Context) {
  553. ctx.WriteString("|BeforeExec2")
  554. }
  555. func beegoAfterExec1(ctx *context.Context) {
  556. ctx.WriteString("|AfterExec1")
  557. }
  558. func beegoAfterExec2(ctx *context.Context) {
  559. ctx.WriteString("|AfterExec2")
  560. }
  561. func beegoFinishRouter1(ctx *context.Context) {
  562. ctx.WriteString("|FinishRouter1")
  563. }
  564. func beegoFinishRouter2(ctx *context.Context) {
  565. ctx.WriteString("|FinishRouter2")
  566. }
  567. func beegoResetParams(ctx *context.Context) {
  568. ctx.ResponseWriter.Header().Set("splat", ctx.Input.Param(":splat"))
  569. }
  570. func beegoHandleResetParams(ctx *context.Context) {
  571. ctx.ResponseWriter.Header().Set("splat", ctx.Input.Param(":splat"))
  572. }