Micro & pluggable web framework for Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

844 lines
19KB

  1. // Copyright 2015 The Tango Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package tango
  5. import (
  6. "bytes"
  7. "net/http"
  8. "net/http/httptest"
  9. "regexp"
  10. "testing"
  11. )
  12. type RouterNoMethodAction struct {
  13. }
  14. func TestRouter1(t *testing.T) {
  15. buff := bytes.NewBufferString("")
  16. recorder := httptest.NewRecorder()
  17. recorder.Body = buff
  18. o := Classic()
  19. o.Get("/:name", new(RouterNoMethodAction))
  20. req, err := http.NewRequest("GET", "http://localhost:8000/foobar", nil)
  21. if err != nil {
  22. t.Error(err)
  23. }
  24. o.ServeHTTP(recorder, req)
  25. expect(t, recorder.Code, http.StatusNotFound)
  26. refute(t, len(buff.String()), 0)
  27. }
  28. type RouterGetAction struct {
  29. }
  30. func (a *RouterGetAction) Get() string {
  31. return "get"
  32. }
  33. func (a *RouterGetAction) Post() string {
  34. return "post"
  35. }
  36. func TestRouter2(t *testing.T) {
  37. buff := bytes.NewBufferString("")
  38. recorder := httptest.NewRecorder()
  39. recorder.Body = buff
  40. o := Classic()
  41. o.Get("/:name", new(RouterGetAction))
  42. o.Post("/:name", new(RouterGetAction))
  43. req, err := http.NewRequest("GET", "http://localhost:8000/foobar", nil)
  44. if err != nil {
  45. t.Error(err)
  46. }
  47. o.ServeHTTP(recorder, req)
  48. expect(t, recorder.Code, http.StatusOK)
  49. refute(t, len(buff.String()), 0)
  50. expect(t, buff.String(), "get")
  51. buff.Reset()
  52. req, err = http.NewRequest("POST", "http://localhost:8000/foobar", nil)
  53. if err != nil {
  54. t.Error(err)
  55. }
  56. o.ServeHTTP(recorder, req)
  57. expect(t, recorder.Code, http.StatusOK)
  58. refute(t, len(buff.String()), 0)
  59. expect(t, buff.String(), "post")
  60. }
  61. type RouterAnyAction struct {
  62. Ctx
  63. }
  64. func (a *RouterAnyAction) Any() string {
  65. return a.Req().Method
  66. }
  67. func TestRouter3(t *testing.T) {
  68. buff := bytes.NewBufferString("")
  69. recorder := httptest.NewRecorder()
  70. recorder.Body = buff
  71. o := Classic()
  72. o.Any("/", new(RouterAnyAction))
  73. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  74. if err != nil {
  75. t.Error(err)
  76. }
  77. o.ServeHTTP(recorder, req)
  78. expect(t, recorder.Code, http.StatusOK)
  79. refute(t, len(buff.String()), 0)
  80. expect(t, buff.String(), "GET")
  81. buff.Reset()
  82. req, err = http.NewRequest("POST", "http://localhost:8000/", nil)
  83. if err != nil {
  84. t.Error(err)
  85. }
  86. o.ServeHTTP(recorder, req)
  87. expect(t, recorder.Code, http.StatusOK)
  88. refute(t, len(buff.String()), 0)
  89. expect(t, buff.String(), "POST")
  90. }
  91. type RouterSpecAction struct {
  92. a string
  93. }
  94. func (RouterSpecAction) Method1() string {
  95. return "1"
  96. }
  97. func (r *RouterSpecAction) Method2() string {
  98. return r.a
  99. }
  100. func TestRouterFunc(t *testing.T) {
  101. buff := bytes.NewBufferString("")
  102. recorder := httptest.NewRecorder()
  103. recorder.Body = buff
  104. o := Classic()
  105. o.Get("/", func() string {
  106. return "func"
  107. })
  108. o.Post("/", func(ctx *Context) {
  109. ctx.Write([]byte("func(*Context)"))
  110. })
  111. o.Put("/", func(resp http.ResponseWriter, req *http.Request) {
  112. resp.Write([]byte("func(http.ResponseWriter, *http.Request)"))
  113. })
  114. o.Options("/", func(resp http.ResponseWriter) {
  115. resp.Write([]byte("func(http.ResponseWriter)"))
  116. })
  117. o.Delete("/", func(req *http.Request) string {
  118. return "func(*http.Request)"
  119. })
  120. // plain
  121. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  122. if err != nil {
  123. t.Error(err)
  124. }
  125. o.ServeHTTP(recorder, req)
  126. expect(t, recorder.Code, http.StatusOK)
  127. refute(t, len(buff.String()), 0)
  128. expect(t, buff.String(), "func")
  129. // context
  130. buff.Reset()
  131. req, err = http.NewRequest("POST", "http://localhost:8000/", nil)
  132. if err != nil {
  133. t.Error(err)
  134. }
  135. o.ServeHTTP(recorder, req)
  136. expect(t, recorder.Code, http.StatusOK)
  137. refute(t, len(buff.String()), 0)
  138. expect(t, buff.String(), "func(*Context)")
  139. // http
  140. buff.Reset()
  141. req, err = http.NewRequest("PUT", "http://localhost:8000/", nil)
  142. if err != nil {
  143. t.Error(err)
  144. }
  145. o.ServeHTTP(recorder, req)
  146. expect(t, recorder.Code, http.StatusOK)
  147. refute(t, len(buff.String()), 0)
  148. expect(t, buff.String(), "func(http.ResponseWriter, *http.Request)")
  149. // response
  150. buff.Reset()
  151. req, err = http.NewRequest("OPTIONS", "http://localhost:8000/", nil)
  152. if err != nil {
  153. t.Error(err)
  154. }
  155. o.ServeHTTP(recorder, req)
  156. expect(t, recorder.Code, http.StatusOK)
  157. refute(t, len(buff.String()), 0)
  158. expect(t, buff.String(), "func(http.ResponseWriter)")
  159. // req
  160. buff.Reset()
  161. req, err = http.NewRequest("DELETE", "http://localhost:8000/", nil)
  162. if err != nil {
  163. t.Error(err)
  164. }
  165. o.ServeHTTP(recorder, req)
  166. expect(t, recorder.Code, http.StatusOK)
  167. refute(t, len(buff.String()), 0)
  168. expect(t, buff.String(), "func(*http.Request)")
  169. }
  170. type Router4Action struct {
  171. Params
  172. }
  173. func (r *Router4Action) Get() string {
  174. return r.Params.Get(":name1") + "-" + r.Params.Get(":name2")
  175. }
  176. func TestRouter4(t *testing.T) {
  177. buff := bytes.NewBufferString("")
  178. recorder := httptest.NewRecorder()
  179. recorder.Body = buff
  180. o := Classic()
  181. o.Get("/:name1-:name2", new(Router4Action))
  182. req, err := http.NewRequest("GET", "http://localhost:8000/foobar-foobar2", nil)
  183. if err != nil {
  184. t.Error(err)
  185. }
  186. o.ServeHTTP(recorder, req)
  187. expect(t, recorder.Code, http.StatusOK)
  188. refute(t, len(buff.String()), 0)
  189. expect(t, buff.String(), "foobar-foobar2")
  190. }
  191. type Router5Action struct {
  192. }
  193. func (r *Router5Action) Get() string {
  194. return "router5"
  195. }
  196. func TestRouter5(t *testing.T) {
  197. buff := bytes.NewBufferString("")
  198. recorder := httptest.NewRecorder()
  199. recorder.Body = buff
  200. o := Classic()
  201. o.Route("GET", "/", new(Router5Action))
  202. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  203. if err != nil {
  204. t.Error(err)
  205. }
  206. o.ServeHTTP(recorder, req)
  207. expect(t, recorder.Code, http.StatusOK)
  208. refute(t, len(buff.String()), 0)
  209. expect(t, buff.String(), "router5")
  210. }
  211. type Router6Action struct {
  212. }
  213. func (r *Router6Action) MyMethod() string {
  214. return "router6"
  215. }
  216. func TestRouter6(t *testing.T) {
  217. buff := bytes.NewBufferString("")
  218. recorder := httptest.NewRecorder()
  219. recorder.Body = buff
  220. o := Classic()
  221. o.Route("GET:MyMethod", "/", new(Router6Action))
  222. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  223. if err != nil {
  224. t.Error(err)
  225. }
  226. o.ServeHTTP(recorder, req)
  227. expect(t, recorder.Code, http.StatusOK)
  228. refute(t, len(buff.String()), 0)
  229. expect(t, buff.String(), "router6")
  230. }
  231. type Router7Action struct {
  232. }
  233. func (r *Router7Action) MyGet() string {
  234. return "router7-get"
  235. }
  236. func (r *Router7Action) Post() string {
  237. return "router7-post"
  238. }
  239. func TestRouter7(t *testing.T) {
  240. buff := bytes.NewBufferString("")
  241. recorder := httptest.NewRecorder()
  242. recorder.Body = buff
  243. o := Classic()
  244. o.Route([]string{"GET:MyGet", "POST"}, "/", new(Router7Action))
  245. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  246. if err != nil {
  247. t.Error(err)
  248. }
  249. o.ServeHTTP(recorder, req)
  250. expect(t, recorder.Code, http.StatusOK)
  251. refute(t, len(buff.String()), 0)
  252. expect(t, buff.String(), "router7-get")
  253. buff.Reset()
  254. req, err = http.NewRequest("POST", "http://localhost:8000/", nil)
  255. if err != nil {
  256. t.Error(err)
  257. }
  258. o.ServeHTTP(recorder, req)
  259. expect(t, recorder.Code, http.StatusOK)
  260. refute(t, len(buff.String()), 0)
  261. expect(t, buff.String(), "router7-post")
  262. }
  263. type Router8Action struct {
  264. }
  265. func (r *Router8Action) MyGet() string {
  266. return "router8-get"
  267. }
  268. func (r *Router8Action) Post() string {
  269. return "router8-post"
  270. }
  271. func TestRouter8(t *testing.T) {
  272. buff := bytes.NewBufferString("")
  273. recorder := httptest.NewRecorder()
  274. recorder.Body = buff
  275. o := Classic()
  276. o.Route(map[string]string{"GET": "MyGet", "POST": "Post"}, "/", new(Router8Action))
  277. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  278. if err != nil {
  279. t.Error(err)
  280. }
  281. o.ServeHTTP(recorder, req)
  282. expect(t, recorder.Code, http.StatusOK)
  283. refute(t, len(buff.String()), 0)
  284. expect(t, buff.String(), "router8-get")
  285. buff.Reset()
  286. req, err = http.NewRequest("POST", "http://localhost:8000/", nil)
  287. if err != nil {
  288. t.Error(err)
  289. }
  290. o.ServeHTTP(recorder, req)
  291. expect(t, recorder.Code, http.StatusOK)
  292. refute(t, len(buff.String()), 0)
  293. expect(t, buff.String(), "router8-post")
  294. }
  295. type Regex1Action struct {
  296. Params
  297. }
  298. func (r *Regex1Action) Get() string {
  299. return r.Params.Get(":name")
  300. }
  301. func TestRouter9(t *testing.T) {
  302. buff := bytes.NewBufferString("")
  303. recorder := httptest.NewRecorder()
  304. recorder.Body = buff
  305. o := Classic()
  306. o.Get("/(:name[a-zA-Z]+)", new(Regex1Action))
  307. req, err := http.NewRequest("GET", "http://localhost:8000/foobar", nil)
  308. if err != nil {
  309. t.Error(err)
  310. }
  311. o.ServeHTTP(recorder, req)
  312. expect(t, recorder.Code, http.StatusOK)
  313. expect(t, buff.String(), "foobar")
  314. refute(t, len(buff.String()), 0)
  315. }
  316. var (
  317. parsedResult = map[string][]*node{
  318. "/": []*node{
  319. {content: "/", tp: snode},
  320. },
  321. "/static/css/bootstrap.css": []*node{
  322. {content: "/static", tp: snode},
  323. {content: "/css", tp: snode},
  324. {content: "/bootstrap.css", tp: snode},
  325. },
  326. "/:name": []*node{
  327. {content: "/", tp: snode},
  328. {content: ":name", tp: nnode},
  329. },
  330. "/sss:name": []*node{
  331. {content: "/sss", tp: snode},
  332. {content: ":name", tp: nnode},
  333. },
  334. "/(:name)": []*node{
  335. {content: "/", tp: snode},
  336. {content: ":name", tp: nnode},
  337. },
  338. "/(:name)/sss": []*node{
  339. {content: "/", tp: snode},
  340. {content: ":name", tp: nnode},
  341. {content: "/sss", tp: snode},
  342. },
  343. "/:name-:value": []*node{
  344. {content: "/", tp: snode},
  345. {content: ":name", tp: nnode},
  346. {content: "-", tp: snode},
  347. {content: ":value", tp: nnode},
  348. },
  349. "/(:name)ssss(:value)": []*node{
  350. {content: "/", tp: snode},
  351. {content: ":name", tp: nnode},
  352. {content: "ssss", tp: snode},
  353. {content: ":value", tp: nnode},
  354. },
  355. "/(:name[0-9]+)": []*node{
  356. {content: "/", tp: snode},
  357. {content: ":name", tp: rnode, regexp: regexp.MustCompile("([0-9]+)")},
  358. },
  359. "/*name": []*node{
  360. {content: "/", tp: snode},
  361. {content: "*name", tp: anode},
  362. },
  363. "/*name/ssss": []*node{
  364. {content: "/", tp: snode},
  365. {content: "*name", tp: anode},
  366. {content: "/ssss", tp: snode},
  367. },
  368. "/(*name)ssss": []*node{
  369. {content: "/", tp: snode},
  370. {content: "*name", tp: anode},
  371. {content: "ssss", tp: snode},
  372. },
  373. "/:name-(:name2[a-z]+)": []*node{
  374. {content: "/", tp: snode},
  375. {content: ":name", tp: nnode},
  376. {content: "-", tp: snode},
  377. {content: ":name2", tp: rnode, regexp: regexp.MustCompile("([a-z]+)")},
  378. },
  379. "/web/content/(:id3)-(:unique3)/(:filename)": []*node{
  380. {content: "/web", tp: snode},
  381. {content: "/content", tp: snode},
  382. {content: "/", tp: snode},
  383. {content: ":id3", tp: nnode},
  384. {content: "-", tp: snode},
  385. {content: ":unique3", tp: nnode},
  386. {content: "/", tp: snode},
  387. {content: ":filename", tp: nnode},
  388. },
  389. }
  390. )
  391. func TestParseNode(t *testing.T) {
  392. for p, r := range parsedResult {
  393. res := parseNodes(p)
  394. if len(r) != len(res) {
  395. t.Fatalf("%v 's result %v is not equal %v", p, r, res)
  396. }
  397. for i := 0; i < len(r); i++ {
  398. if r[i].content != res[i].content ||
  399. r[i].tp != res[i].tp {
  400. t.Fatalf("%v 's %d result %v is not equal %v", p, i, r[i], res[i])
  401. }
  402. if r[i].tp != rnode {
  403. if r[i].regexp != nil {
  404. t.Fatalf("%v 's %d result %v is not equal %v", p, i, r[i], res[i])
  405. }
  406. } else {
  407. if r[i].regexp == nil {
  408. t.Fatalf("%v 's %d result %v is not equal %v", p, i, r[i], res[i])
  409. }
  410. }
  411. }
  412. }
  413. }
  414. type result struct {
  415. url string
  416. match bool
  417. params Params
  418. }
  419. var (
  420. matchResult = map[string][]result{
  421. "/": []result{
  422. {"/", true, Params{}},
  423. {"/s", false, Params{}},
  424. {"/123", false, Params{}},
  425. },
  426. "/ss/tt": []result{
  427. {"/ss/tt", true, Params{}},
  428. {"/s", false, Params{}},
  429. {"/ss", false, Params{}},
  430. },
  431. "/:name": []result{
  432. {"/s", true, Params{{":name", "s"}}},
  433. {"/", false, Params{}},
  434. {"/123/s", false, Params{}},
  435. },
  436. "/:name1/:name2/:name3": []result{
  437. {"/1/2/3", true, Params{{":name1", "1"}, {":name2", "2"}, {":name3", "3"}}},
  438. {"/1/2", false, Params{}},
  439. {"/1/2/3/", false, Params{}},
  440. },
  441. "/*name": []result{
  442. {"/s", true, Params{{"*name", "s"}}},
  443. {"/123/s", true, Params{{"*name", "123/s"}}},
  444. {"/", false, Params{}},
  445. },
  446. "/(*name)ssss": []result{
  447. {"/sssss", true, Params{{"*name", "s"}}},
  448. {"/123/ssss", true, Params{{"*name", "123/"}}},
  449. {"/", false, Params{}},
  450. {"/ss", false, Params{}},
  451. },
  452. "/111(*name)ssss": []result{
  453. {"/111sssss", true, Params{{"*name", "s"}}},
  454. {"/111/123/ssss", true, Params{{"*name", "/123/"}}},
  455. {"/", false, Params{}},
  456. {"/ss", false, Params{}},
  457. },
  458. "/(:name[0-9]+)": []result{
  459. {"/123", true, Params{{":name", "123"}}},
  460. {"/sss", false, Params{}},
  461. },
  462. "/ss(:name[0-9]+)": []result{
  463. {"/ss123", true, Params{{":name", "123"}}},
  464. {"/sss", false, Params{}},
  465. },
  466. "/ss(:name[0-9]+)tt": []result{
  467. {"/ss123tt", true, Params{{":name", "123"}}},
  468. {"/sss", false, Params{}},
  469. },
  470. "/:name1-(:name2[0-9]+)": []result{
  471. {"/ss-123", true, Params{{":name1", "ss"}, {":name2", "123"}}},
  472. {"/sss", false, Params{}},
  473. },
  474. "/(:name1)00(:name2[0-9]+)": []result{
  475. {"/ss00123", true, Params{{":name1", "ss"}, {":name2", "123"}}},
  476. {"/sss", false, Params{}},
  477. },
  478. "/(:name1)!(:name2[0-9]+)!(:name3.*)": []result{
  479. {"/ss!123!456", true, Params{{":name1", "ss"}, {":name2", "123"}, {":name3", "456"}}},
  480. {"/sss", false, Params{}},
  481. },
  482. "/web/content/(:id3)-(:unique3)/(:filename)": []result{
  483. {"/web/content/36-0420888/website.assets_frontend.0.css", true, Params{{":id3", "36"}, {":unique3", "0420888"}, {":filename", "website.assets_frontend.0.css"}}},
  484. },
  485. }
  486. )
  487. type Action struct {
  488. }
  489. func (Action) Get() string {
  490. return "get"
  491. }
  492. func TestRouterSingle(t *testing.T) {
  493. for k, m := range matchResult {
  494. r := New()
  495. r.Route("GET", k, new(Action))
  496. for _, res := range m {
  497. handler, params := r.Match(res.url, "GET")
  498. if res.match {
  499. if handler == nil {
  500. t.Fatal(k, res, "handler", handler, "should not be nil")
  501. }
  502. for i, v := range params {
  503. if res.params[i].Name != v.Name {
  504. t.Fatal(k, res, "params name", v, "not equal", res.params[i])
  505. }
  506. if res.params[i].Value != v.Value {
  507. t.Fatal(k, res, "params value", v, "not equal", res.params[i])
  508. }
  509. }
  510. } else {
  511. if handler != nil {
  512. t.Fatal(k, res, "handler", handler, "should be nil")
  513. }
  514. }
  515. }
  516. }
  517. }
  518. type testCase struct {
  519. routers []string
  520. results []result
  521. }
  522. var (
  523. matchResult2 = []testCase{
  524. {
  525. []string{"/"},
  526. []result{
  527. {"/", true, Params{}},
  528. {"/s", false, Params{}},
  529. {"/123", false, Params{}},
  530. },
  531. },
  532. {
  533. []string{"/admin", "/:name"},
  534. []result{
  535. {"/", false, Params{}},
  536. {"/admin", true, Params{}},
  537. {"/s", true, Params{param{":name", "s"}}},
  538. {"/123", true, Params{param{":name", "123"}}},
  539. },
  540. },
  541. {
  542. []string{"/:name", "/admin"},
  543. []result{
  544. {"/", false, Params{}},
  545. {"/admin", true, Params{}},
  546. {"/s", true, Params{param{":name", "s"}}},
  547. {"/123", true, Params{param{":name", "123"}}},
  548. },
  549. },
  550. {
  551. []string{"/admin", "/*name"},
  552. []result{
  553. {"/", false, Params{}},
  554. {"/admin", true, Params{}},
  555. {"/s", true, Params{param{"*name", "s"}}},
  556. {"/123", true, Params{param{"*name", "123"}}},
  557. },
  558. },
  559. {
  560. []string{"/*name", "/admin"},
  561. []result{
  562. {"/", false, Params{}},
  563. {"/admin", true, Params{}},
  564. {"/s", true, Params{param{"*name", "s"}}},
  565. {"/123", true, Params{param{"*name", "123"}}},
  566. },
  567. },
  568. {
  569. []string{"/*name", "/:name"},
  570. []result{
  571. {"/", false, Params{}},
  572. {"/s", true, Params{param{"*name", "s"}}},
  573. {"/123", true, Params{param{"*name", "123"}}},
  574. },
  575. },
  576. {
  577. []string{"/:name", "/*name"},
  578. []result{
  579. {"/", false, Params{}},
  580. {"/s", true, Params{param{":name", "s"}}},
  581. {"/123", true, Params{param{":name", "123"}}},
  582. {"/123/1", true, Params{param{"*name", "123/1"}}},
  583. },
  584. },
  585. {
  586. []string{"/*name", "/*name/123"},
  587. []result{
  588. {"/", false, Params{}},
  589. {"/123", true, Params{param{"*name", "123"}}},
  590. {"/s", true, Params{param{"*name", "s"}}},
  591. {"/abc/123", true, Params{param{"*name", "abc"}}},
  592. {"/name1/name2/123", true, Params{param{"*name", "name1/name2"}}},
  593. },
  594. },
  595. {
  596. []string{"/admin/ui", "/*name", "/:name"},
  597. []result{
  598. {"/", false, Params{}},
  599. {"/admin/ui", true, Params{}},
  600. {"/s", true, Params{param{"*name", "s"}}},
  601. {"/123", true, Params{param{"*name", "123"}}},
  602. },
  603. },
  604. {
  605. []string{"/(:id[0-9]+)", "/(:id[0-9]+)/edit", "/(:id[0-9]+)/del"},
  606. []result{
  607. {"/1", true, Params{param{":id", "1"}}},
  608. {"/admin/ui", false, Params{}},
  609. {"/2/edit", true, Params{param{":id", "2"}}},
  610. {"/3/del", true, Params{param{":id", "3"}}},
  611. },
  612. },
  613. {
  614. []string{"/admin/ui", "/:name1/:name2"},
  615. []result{
  616. {"/", false, Params{}},
  617. {"/admin/ui", true, Params{}},
  618. {"/s", false, Params{}},
  619. {"/admin/ui2", true, Params{param{":name1", "admin"}, param{":name2", "ui2"}}},
  620. {"/123/s", true, Params{param{":name1", "123"}, param{":name2", "s"}}},
  621. },
  622. },
  623. {
  624. []string{"/(:name1)/(:name1)abc/",
  625. "/(:name1)/(:name1)abc(:name2)abc/",
  626. "/(:name1)/abc(:name1)abc(:name2)abc/",
  627. "/(:name1)/(:name1)/"},
  628. []result{
  629. {"/abc/abc123abc123abc", true, Params{
  630. param{":name1", "abc"},
  631. param{":name1", "123"},
  632. param{":name2", "123"},
  633. },
  634. },
  635. },
  636. },
  637. }
  638. )
  639. func TestRouterMultiple(t *testing.T) {
  640. for _, kase := range matchResult2 {
  641. r := New()
  642. for _, k := range kase.routers {
  643. r.Route("GET", k, new(Action))
  644. }
  645. for _, res := range kase.results {
  646. handler, params := r.Match(res.url, "GET")
  647. if res.match {
  648. if handler == nil {
  649. t.Fatal(kase.routers, res, "handler", handler, "should not be nil")
  650. }
  651. if len(res.params) != len(params) {
  652. t.Fatal(kase.routers, res, "params", params, "not equal", res.params)
  653. }
  654. for i, v := range params {
  655. if res.params[i].Name != v.Name {
  656. t.Fatal(kase.routers, res, "params name", v, "not equal", res.params[i])
  657. }
  658. if res.params[i].Value != v.Value {
  659. t.Fatal(kase.routers, res, "params value", v, "not equal", res.params[i])
  660. }
  661. }
  662. } else {
  663. if handler != nil {
  664. t.Fatal(kase.routers, res, "handler", handler, "should be nil")
  665. }
  666. }
  667. }
  668. }
  669. }
  670. func TestRouter10(t *testing.T) {
  671. buff := bytes.NewBufferString("")
  672. recorder := httptest.NewRecorder()
  673. recorder.Body = buff
  674. r := New()
  675. r.Get("/", func(ctx *Context) {
  676. ctx.Write([]byte("test"))
  677. })
  678. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  679. if err != nil {
  680. t.Error(err)
  681. }
  682. r.ServeHTTP(recorder, req)
  683. expect(t, recorder.Code, http.StatusOK)
  684. expect(t, buff.String(), "test")
  685. refute(t, len(buff.String()), 0)
  686. }
  687. type Route11Action struct {
  688. }
  689. func (Route11Action) Any() string {
  690. return "any"
  691. }
  692. func TestRouter11(t *testing.T) {
  693. buff := bytes.NewBufferString("")
  694. recorder := httptest.NewRecorder()
  695. recorder.Body = buff
  696. r := Classic()
  697. r.Get("/", new(Route11Action))
  698. r.Post("/2", new(Route11Action))
  699. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  700. if err != nil {
  701. t.Error(err)
  702. }
  703. r.ServeHTTP(recorder, req)
  704. expect(t, recorder.Code, http.StatusOK)
  705. expect(t, buff.String(), "any")
  706. refute(t, len(buff.String()), 0)
  707. buff.Reset()
  708. req, err = http.NewRequest("POST", "http://localhost:8000/2", nil)
  709. if err != nil {
  710. t.Error(err)
  711. }
  712. r.ServeHTTP(recorder, req)
  713. expect(t, recorder.Code, http.StatusOK)
  714. expect(t, buff.String(), "any")
  715. refute(t, len(buff.String()), 0)
  716. }