tree_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. // Copyright 2013 Julien Schmidt. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be found
  3. // in the LICENSE file.
  4. package gin
  5. import (
  6. "fmt"
  7. "reflect"
  8. "strings"
  9. "testing"
  10. )
  11. func printChildren(n *node, prefix string) {
  12. fmt.Printf(" %02d:%02d %s%s[%d] %v %t %d \r\n", n.priority, n.maxParams, prefix, n.path, len(n.children), n.handlers, n.wildChild, n.nType)
  13. for l := len(n.path); l > 0; l-- {
  14. prefix += " "
  15. }
  16. for _, child := range n.children {
  17. printChildren(child, prefix)
  18. }
  19. }
  20. // Used as a workaround since we can't compare functions or their adresses
  21. var fakeHandlerValue string
  22. func fakeHandler(val string) []HandlerFunc {
  23. return []HandlerFunc{func(c *Context) {
  24. fakeHandlerValue = val
  25. }}
  26. }
  27. type testRequests []struct {
  28. path string
  29. nilHandler bool
  30. route string
  31. ps Params
  32. }
  33. func checkRequests(t *testing.T, tree *node, requests testRequests) {
  34. for _, request := range requests {
  35. handler, ps, _ := tree.getValue(request.path, nil)
  36. if handler == nil {
  37. if !request.nilHandler {
  38. t.Errorf("handle mismatch for route '%s': Expected non-nil handle", request.path)
  39. }
  40. } else if request.nilHandler {
  41. t.Errorf("handle mismatch for route '%s': Expected nil handle", request.path)
  42. } else {
  43. handler[0](nil)
  44. if fakeHandlerValue != request.route {
  45. t.Errorf("handle mismatch for route '%s': Wrong handle (%s != %s)", request.path, fakeHandlerValue, request.route)
  46. }
  47. }
  48. if !reflect.DeepEqual(ps, request.ps) {
  49. t.Errorf("Params mismatch for route '%s'", request.path)
  50. }
  51. }
  52. }
  53. func checkPriorities(t *testing.T, n *node) uint32 {
  54. var prio uint32
  55. for i := range n.children {
  56. prio += checkPriorities(t, n.children[i])
  57. }
  58. if n.handlers != nil {
  59. prio++
  60. }
  61. if n.priority != prio {
  62. t.Errorf(
  63. "priority mismatch for node '%s': is %d, should be %d",
  64. n.path, n.priority, prio,
  65. )
  66. }
  67. return prio
  68. }
  69. func checkMaxParams(t *testing.T, n *node) uint8 {
  70. var maxParams uint8
  71. for i := range n.children {
  72. params := checkMaxParams(t, n.children[i])
  73. if params > maxParams {
  74. maxParams = params
  75. }
  76. }
  77. if n.nType != static && !n.wildChild {
  78. maxParams++
  79. }
  80. if n.maxParams != maxParams {
  81. t.Errorf(
  82. "maxParams mismatch for node '%s': is %d, should be %d",
  83. n.path, n.maxParams, maxParams,
  84. )
  85. }
  86. return maxParams
  87. }
  88. func TestCountParams(t *testing.T) {
  89. if countParams("/path/:param1/static/*catch-all") != 2 {
  90. t.Fail()
  91. }
  92. if countParams(strings.Repeat("/:param", 256)) != 255 {
  93. t.Fail()
  94. }
  95. }
  96. func TestTreeAddAndGet(t *testing.T) {
  97. tree := &node{}
  98. routes := [...]string{
  99. "/hi",
  100. "/contact",
  101. "/co",
  102. "/c",
  103. "/a",
  104. "/ab",
  105. "/doc/",
  106. "/doc/go_faq.html",
  107. "/doc/go1.html",
  108. "/α",
  109. "/β",
  110. }
  111. for _, route := range routes {
  112. tree.addRoute(route, fakeHandler(route))
  113. }
  114. //printChildren(tree, "")
  115. checkRequests(t, tree, testRequests{
  116. {"/a", false, "/a", nil},
  117. {"/", true, "", nil},
  118. {"/hi", false, "/hi", nil},
  119. {"/contact", false, "/contact", nil},
  120. {"/co", false, "/co", nil},
  121. {"/con", true, "", nil}, // key mismatch
  122. {"/cona", true, "", nil}, // key mismatch
  123. {"/no", true, "", nil}, // no matching child
  124. {"/ab", false, "/ab", nil},
  125. {"/α", false, "/α", nil},
  126. {"/β", false, "/β", nil},
  127. })
  128. checkPriorities(t, tree)
  129. checkMaxParams(t, tree)
  130. }
  131. func TestTreeWildcard(t *testing.T) {
  132. tree := &node{}
  133. routes := [...]string{
  134. "/",
  135. "/cmd/:tool/:sub",
  136. "/cmd/:tool/",
  137. "/src/*filepath",
  138. "/search/",
  139. "/search/:query",
  140. "/user_:name",
  141. "/user_:name/about",
  142. "/files/:dir/*filepath",
  143. "/doc/",
  144. "/doc/go_faq.html",
  145. "/doc/go1.html",
  146. "/info/:user/public",
  147. "/info/:user/project/:project",
  148. }
  149. for _, route := range routes {
  150. tree.addRoute(route, fakeHandler(route))
  151. }
  152. //printChildren(tree, "")
  153. checkRequests(t, tree, testRequests{
  154. {"/", false, "/", nil},
  155. {"/cmd/test/", false, "/cmd/:tool/", Params{Param{"tool", "test"}}},
  156. {"/cmd/test", true, "", Params{Param{"tool", "test"}}},
  157. {"/cmd/test/3", false, "/cmd/:tool/:sub", Params{Param{"tool", "test"}, Param{"sub", "3"}}},
  158. {"/src/", false, "/src/*filepath", Params{Param{"filepath", "/"}}},
  159. {"/src/some/file.png", false, "/src/*filepath", Params{Param{"filepath", "/some/file.png"}}},
  160. {"/search/", false, "/search/", nil},
  161. {"/search/someth!ng+in+ünìcodé", false, "/search/:query", Params{Param{"query", "someth!ng+in+ünìcodé"}}},
  162. {"/search/someth!ng+in+ünìcodé/", true, "", Params{Param{"query", "someth!ng+in+ünìcodé"}}},
  163. {"/user_gopher", false, "/user_:name", Params{Param{"name", "gopher"}}},
  164. {"/user_gopher/about", false, "/user_:name/about", Params{Param{"name", "gopher"}}},
  165. {"/files/js/inc/framework.js", false, "/files/:dir/*filepath", Params{Param{"dir", "js"}, Param{"filepath", "/inc/framework.js"}}},
  166. {"/info/gordon/public", false, "/info/:user/public", Params{Param{"user", "gordon"}}},
  167. {"/info/gordon/project/go", false, "/info/:user/project/:project", Params{Param{"user", "gordon"}, Param{"project", "go"}}},
  168. })
  169. checkPriorities(t, tree)
  170. checkMaxParams(t, tree)
  171. }
  172. func catchPanic(testFunc func()) (recv interface{}) {
  173. defer func() {
  174. recv = recover()
  175. }()
  176. testFunc()
  177. return
  178. }
  179. type testRoute struct {
  180. path string
  181. conflict bool
  182. }
  183. func testRoutes(t *testing.T, routes []testRoute) {
  184. tree := &node{}
  185. for _, route := range routes {
  186. recv := catchPanic(func() {
  187. tree.addRoute(route.path, nil)
  188. })
  189. if route.conflict {
  190. if recv == nil {
  191. t.Errorf("no panic for conflicting route '%s'", route.path)
  192. }
  193. } else if recv != nil {
  194. t.Errorf("unexpected panic for route '%s': %v", route.path, recv)
  195. }
  196. }
  197. //printChildren(tree, "")
  198. }
  199. func TestTreeWildcardConflict(t *testing.T) {
  200. routes := []testRoute{
  201. {"/cmd/:tool/:sub", false},
  202. {"/cmd/vet", true},
  203. {"/src/*filepath", false},
  204. {"/src/*filepathx", true},
  205. {"/src/", true},
  206. {"/src1/", false},
  207. {"/src1/*filepath", true},
  208. {"/src2*filepath", true},
  209. {"/search/:query", false},
  210. {"/search/invalid", true},
  211. {"/user_:name", false},
  212. {"/user_x", true},
  213. {"/user_:name", false},
  214. {"/id:id", false},
  215. {"/id/:id", true},
  216. }
  217. testRoutes(t, routes)
  218. }
  219. func TestTreeChildConflict(t *testing.T) {
  220. routes := []testRoute{
  221. {"/cmd/vet", false},
  222. {"/cmd/:tool/:sub", true},
  223. {"/src/AUTHORS", false},
  224. {"/src/*filepath", true},
  225. {"/user_x", false},
  226. {"/user_:name", true},
  227. {"/id/:id", false},
  228. {"/id:id", true},
  229. {"/:id", true},
  230. {"/*filepath", true},
  231. }
  232. testRoutes(t, routes)
  233. }
  234. func TestTreeDupliatePath(t *testing.T) {
  235. tree := &node{}
  236. routes := [...]string{
  237. "/",
  238. "/doc/",
  239. "/src/*filepath",
  240. "/search/:query",
  241. "/user_:name",
  242. }
  243. for _, route := range routes {
  244. recv := catchPanic(func() {
  245. tree.addRoute(route, fakeHandler(route))
  246. })
  247. if recv != nil {
  248. t.Fatalf("panic inserting route '%s': %v", route, recv)
  249. }
  250. // Add again
  251. recv = catchPanic(func() {
  252. tree.addRoute(route, nil)
  253. })
  254. if recv == nil {
  255. t.Fatalf("no panic while inserting duplicate route '%s", route)
  256. }
  257. }
  258. //printChildren(tree, "")
  259. checkRequests(t, tree, testRequests{
  260. {"/", false, "/", nil},
  261. {"/doc/", false, "/doc/", nil},
  262. {"/src/some/file.png", false, "/src/*filepath", Params{Param{"filepath", "/some/file.png"}}},
  263. {"/search/someth!ng+in+ünìcodé", false, "/search/:query", Params{Param{"query", "someth!ng+in+ünìcodé"}}},
  264. {"/user_gopher", false, "/user_:name", Params{Param{"name", "gopher"}}},
  265. })
  266. }
  267. func TestEmptyWildcardName(t *testing.T) {
  268. tree := &node{}
  269. routes := [...]string{
  270. "/user:",
  271. "/user:/",
  272. "/cmd/:/",
  273. "/src/*",
  274. }
  275. for _, route := range routes {
  276. recv := catchPanic(func() {
  277. tree.addRoute(route, nil)
  278. })
  279. if recv == nil {
  280. t.Fatalf("no panic while inserting route with empty wildcard name '%s", route)
  281. }
  282. }
  283. }
  284. func TestTreeCatchAllConflict(t *testing.T) {
  285. routes := []testRoute{
  286. {"/src/*filepath/x", true},
  287. {"/src2/", false},
  288. {"/src2/*filepath/x", true},
  289. }
  290. testRoutes(t, routes)
  291. }
  292. func TestTreeCatchAllConflictRoot(t *testing.T) {
  293. routes := []testRoute{
  294. {"/", false},
  295. {"/*filepath", true},
  296. }
  297. testRoutes(t, routes)
  298. }
  299. func TestTreeDoubleWildcard(t *testing.T) {
  300. const panicMsg = "only one wildcard per path segment is allowed"
  301. routes := [...]string{
  302. "/:foo:bar",
  303. "/:foo:bar/",
  304. "/:foo*bar",
  305. }
  306. for _, route := range routes {
  307. tree := &node{}
  308. recv := catchPanic(func() {
  309. tree.addRoute(route, nil)
  310. })
  311. if rs, ok := recv.(string); !ok || rs != panicMsg {
  312. t.Fatalf(`"Expected panic "%s" for route '%s', got "%v"`, panicMsg, route, recv)
  313. }
  314. }
  315. }
  316. /*func TestTreeDuplicateWildcard(t *testing.T) {
  317. tree := &node{}
  318. routes := [...]string{
  319. "/:id/:name/:id",
  320. }
  321. for _, route := range routes {
  322. ...
  323. }
  324. }*/
  325. func TestTreeTrailingSlashRedirect(t *testing.T) {
  326. tree := &node{}
  327. routes := [...]string{
  328. "/hi",
  329. "/b/",
  330. "/search/:query",
  331. "/cmd/:tool/",
  332. "/src/*filepath",
  333. "/x",
  334. "/x/y",
  335. "/y/",
  336. "/y/z",
  337. "/0/:id",
  338. "/0/:id/1",
  339. "/1/:id/",
  340. "/1/:id/2",
  341. "/aa",
  342. "/a/",
  343. "/doc",
  344. "/doc/go_faq.html",
  345. "/doc/go1.html",
  346. "/no/a",
  347. "/no/b",
  348. "/api/hello/:name",
  349. }
  350. for _, route := range routes {
  351. recv := catchPanic(func() {
  352. tree.addRoute(route, fakeHandler(route))
  353. })
  354. if recv != nil {
  355. t.Fatalf("panic inserting route '%s': %v", route, recv)
  356. }
  357. }
  358. //printChildren(tree, "")
  359. tsrRoutes := [...]string{
  360. "/hi/",
  361. "/b",
  362. "/search/gopher/",
  363. "/cmd/vet",
  364. "/src",
  365. "/x/",
  366. "/y",
  367. "/0/go/",
  368. "/1/go",
  369. "/a",
  370. "/doc/",
  371. }
  372. for _, route := range tsrRoutes {
  373. handler, _, tsr := tree.getValue(route, nil)
  374. if handler != nil {
  375. t.Fatalf("non-nil handler for TSR route '%s", route)
  376. } else if !tsr {
  377. t.Errorf("expected TSR recommendation for route '%s'", route)
  378. }
  379. }
  380. noTsrRoutes := [...]string{
  381. "/",
  382. "/no",
  383. "/no/",
  384. "/_",
  385. "/_/",
  386. "/api/world/abc",
  387. }
  388. for _, route := range noTsrRoutes {
  389. handler, _, tsr := tree.getValue(route, nil)
  390. if handler != nil {
  391. t.Fatalf("non-nil handler for No-TSR route '%s", route)
  392. } else if tsr {
  393. t.Errorf("expected no TSR recommendation for route '%s'", route)
  394. }
  395. }
  396. }
  397. func TestTreeFindCaseInsensitivePath(t *testing.T) {
  398. tree := &node{}
  399. routes := [...]string{
  400. "/hi",
  401. "/b/",
  402. "/ABC/",
  403. "/search/:query",
  404. "/cmd/:tool/",
  405. "/src/*filepath",
  406. "/x",
  407. "/x/y",
  408. "/y/",
  409. "/y/z",
  410. "/0/:id",
  411. "/0/:id/1",
  412. "/1/:id/",
  413. "/1/:id/2",
  414. "/aa",
  415. "/a/",
  416. "/doc",
  417. "/doc/go_faq.html",
  418. "/doc/go1.html",
  419. "/doc/go/away",
  420. "/no/a",
  421. "/no/b",
  422. }
  423. for _, route := range routes {
  424. recv := catchPanic(func() {
  425. tree.addRoute(route, fakeHandler(route))
  426. })
  427. if recv != nil {
  428. t.Fatalf("panic inserting route '%s': %v", route, recv)
  429. }
  430. }
  431. // Check out == in for all registered routes
  432. // With fixTrailingSlash = true
  433. for _, route := range routes {
  434. out, found := tree.findCaseInsensitivePath(route, true)
  435. if !found {
  436. t.Errorf("Route '%s' not found!", route)
  437. } else if string(out) != route {
  438. t.Errorf("Wrong result for route '%s': %s", route, string(out))
  439. }
  440. }
  441. // With fixTrailingSlash = false
  442. for _, route := range routes {
  443. out, found := tree.findCaseInsensitivePath(route, false)
  444. if !found {
  445. t.Errorf("Route '%s' not found!", route)
  446. } else if string(out) != route {
  447. t.Errorf("Wrong result for route '%s': %s", route, string(out))
  448. }
  449. }
  450. tests := []struct {
  451. in string
  452. out string
  453. found bool
  454. slash bool
  455. }{
  456. {"/HI", "/hi", true, false},
  457. {"/HI/", "/hi", true, true},
  458. {"/B", "/b/", true, true},
  459. {"/B/", "/b/", true, false},
  460. {"/abc", "/ABC/", true, true},
  461. {"/abc/", "/ABC/", true, false},
  462. {"/aBc", "/ABC/", true, true},
  463. {"/aBc/", "/ABC/", true, false},
  464. {"/abC", "/ABC/", true, true},
  465. {"/abC/", "/ABC/", true, false},
  466. {"/SEARCH/QUERY", "/search/QUERY", true, false},
  467. {"/SEARCH/QUERY/", "/search/QUERY", true, true},
  468. {"/CMD/TOOL/", "/cmd/TOOL/", true, false},
  469. {"/CMD/TOOL", "/cmd/TOOL/", true, true},
  470. {"/SRC/FILE/PATH", "/src/FILE/PATH", true, false},
  471. {"/x/Y", "/x/y", true, false},
  472. {"/x/Y/", "/x/y", true, true},
  473. {"/X/y", "/x/y", true, false},
  474. {"/X/y/", "/x/y", true, true},
  475. {"/X/Y", "/x/y", true, false},
  476. {"/X/Y/", "/x/y", true, true},
  477. {"/Y/", "/y/", true, false},
  478. {"/Y", "/y/", true, true},
  479. {"/Y/z", "/y/z", true, false},
  480. {"/Y/z/", "/y/z", true, true},
  481. {"/Y/Z", "/y/z", true, false},
  482. {"/Y/Z/", "/y/z", true, true},
  483. {"/y/Z", "/y/z", true, false},
  484. {"/y/Z/", "/y/z", true, true},
  485. {"/Aa", "/aa", true, false},
  486. {"/Aa/", "/aa", true, true},
  487. {"/AA", "/aa", true, false},
  488. {"/AA/", "/aa", true, true},
  489. {"/aA", "/aa", true, false},
  490. {"/aA/", "/aa", true, true},
  491. {"/A/", "/a/", true, false},
  492. {"/A", "/a/", true, true},
  493. {"/DOC", "/doc", true, false},
  494. {"/DOC/", "/doc", true, true},
  495. {"/NO", "", false, true},
  496. {"/DOC/GO", "", false, true},
  497. }
  498. // With fixTrailingSlash = true
  499. for _, test := range tests {
  500. out, found := tree.findCaseInsensitivePath(test.in, true)
  501. if found != test.found || (found && (string(out) != test.out)) {
  502. t.Errorf("Wrong result for '%s': got %s, %t; want %s, %t",
  503. test.in, string(out), found, test.out, test.found)
  504. return
  505. }
  506. }
  507. // With fixTrailingSlash = false
  508. for _, test := range tests {
  509. out, found := tree.findCaseInsensitivePath(test.in, false)
  510. if test.slash {
  511. if found { // test needs a trailingSlash fix. It must not be found!
  512. t.Errorf("Found without fixTrailingSlash: %s; got %s", test.in, string(out))
  513. }
  514. } else {
  515. if found != test.found || (found && (string(out) != test.out)) {
  516. t.Errorf("Wrong result for '%s': got %s, %t; want %s, %t",
  517. test.in, string(out), found, test.out, test.found)
  518. return
  519. }
  520. }
  521. }
  522. }
  523. func TestTreeInvalidNodeType(t *testing.T) {
  524. tree := &node{}
  525. tree.addRoute("/", fakeHandler("/"))
  526. tree.addRoute("/:page", fakeHandler("/:page"))
  527. // set invalid node type
  528. tree.children[0].nType = 42
  529. // normal lookup
  530. recv := catchPanic(func() {
  531. tree.getValue("/test", nil)
  532. })
  533. if rs, ok := recv.(string); !ok || rs != "Invalid node type" {
  534. t.Fatalf(`Expected panic "Invalid node type", got "%v"`, recv)
  535. }
  536. // case-insensitive lookup
  537. recv = catchPanic(func() {
  538. tree.findCaseInsensitivePath("/test", true)
  539. })
  540. if rs, ok := recv.(string); !ok || rs != "Invalid node type" {
  541. t.Fatalf(`Expected panic "Invalid node type", got "%v"`, recv)
  542. }
  543. }