/src/test/RouterTest.fan

https://bitbucket.org/afrankvt/draft/ · Unknown · 98 lines · 85 code · 13 blank · 0 comment · 0 complexity · df40ec1bf0cd810d9af0a0ca4d0ee096 MD5 · raw file

  1. //
  2. // Copyright (c) 2011, Andy Frank
  3. // Licensed under the MIT License
  4. //
  5. // History:
  6. // 14 May 2011 Andy Frank Creation
  7. //
  8. using web
  9. **
  10. ** Test code for Router class.
  11. **
  12. internal class RouterTest : Test
  13. {
  14. Void testRoutes()
  15. {
  16. // TODO FIXIT - {index} arguments
  17. r := Route("/foo/bar", "GET", #foo)
  18. verifyEq(r.match(`/foo/bar`, "GET"), Str:Str[:])
  19. verifyEq(r.match(`/foo/bar`, "POST"), null)
  20. verifyEq(r.match(`/foo/bar`, "HEAD"), null)
  21. verifyEq(r.match(`/yo`, "GET"), null)
  22. verifyEq(r.match(`/foo`, "GET"), null)
  23. verifyEq(r.match(`/foo/`, "GET"), null)
  24. verifyEq(r.match(`/foo/ba`, "GET"), null)
  25. // TODO: should this match?
  26. // verifyEq(r.match(`/foo/bar/`, "GET"), null)
  27. r = Route("/foo/{arg}", "GET", #foo)
  28. verifyEq(r.match(`/foo/123`, "GET"), Str:Str["arg":"123"])
  29. verifyEq(r.match(`/foo/abc`, "GET"), Str:Str["arg":"abc"])
  30. verifyEq(r.match(`/foo/ax9`, "GET"), Str:Str["arg":"ax9"])
  31. verifyEq(r.match(`/foo/_4b`, "GET"), Str:Str["arg":"_4b"])
  32. r = Route("/foo/{a}/bar/{b}/list", "GET", #foo)
  33. verifyEq(r.match(`/foo/123/bar/abc/list`, "GET"), Str:Str["a":"123", "b":"abc"])
  34. verifyEq(r.match(`/foo/123/bax/abc/list`, "GET"), null)
  35. // vararg matches
  36. r = Route("/foo/*", "GET", #foo)
  37. verifyEq(r.match(`/foo/x/y/z`, "GET"), Str:Str[:])
  38. verifyEq(r.match(`/fox/x/y/z`, "GET"), null)
  39. r = Route("/foo/{bar}/*", "GET", #foo)
  40. verifyEq(r.match(`/foo/x/y/z`, "GET"), Str:Str["bar":"x"])
  41. verifyEq(r.match(`/fox/x/y/z`, "GET"), null)
  42. r = Route("/x/y/z/*", "GET", #foo)
  43. verifyEq(r.match(`/x`, "GET"), null)
  44. verifyEq(r.match(`/x/y`, "GET"), null)
  45. verifyEq(r.match(`/x/y/z`, "GET"), null)
  46. verifyEq(r.match(`/x/y/z/foo`, "GET"), Str:Str[:])
  47. verifyEq(r.match(`/x/y/z/foo/a/b/c`, "GET"), Str:Str[:])
  48. // errs
  49. // should err?
  50. // verifyErr(ArgErr#) { x := Route("foo", "GET", #foo) }
  51. verifyErr(ArgErr#) { x := Route("/foo/*/*", "GET", #foo) }
  52. verifyErr(ArgErr#) { x := Route("/*/foo", "GET", #foo) }
  53. // allowed?
  54. // verifyErr(ArgErr#) { x := Route("/bar/", "GET", #foo) }
  55. }
  56. Void testRouter()
  57. {
  58. test := Router {
  59. routes = [
  60. Route("/", "GET", #index),
  61. Route("/foo", "GET", #foo),
  62. Route("/foo/bar", "GET", #bar),
  63. Route("/foo/{arg}", "GET", #fooArg),
  64. ]
  65. }
  66. verifyRouter(test, `/foo`, #foo, Str:Str[:])
  67. // TODO FIXIT: this needs to work
  68. // maybe this is illegal - can't end in / - expect for "/"
  69. // verifyRouter(test, `/foo/`, #fooIndex, Str:Str[:])
  70. verifyRouter(test, `/foo/bar`, #bar, Str:Str[:])
  71. verifyRouter(test, `/foo/xyz`, #fooArg, Str:Str["arg":"xyz"])
  72. }
  73. private Void verifyRouter(Router r, Uri uri, Method? handler, [Str:Str]? args)
  74. {
  75. m := r.match(uri, "GET")
  76. verifyEq(m?.route?.handler, handler)
  77. verifyEq(m?.args, args)
  78. }
  79. // dummy handlers
  80. private Void index() {}
  81. private Void foo() {}
  82. private Void bar() {}
  83. // private Void fooIndex() {}
  84. private Void fooArg() {}
  85. }