1
0

SwifterTestsHttpRouter.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // SwifterTestsHttpRouter.swift
  3. // Swifter
  4. //
  5. // Copyright © 2016 Damian Kołakowski. All rights reserved.
  6. //
  7. import XCTest
  8. class SwifterTestsHttpRouter: XCTestCase {
  9. func testHttpRouterSlashRoot() {
  10. let router = HttpRouter()
  11. router.register(nil, path: "/", handler: { r in
  12. return .ok(.html("OK"))
  13. })
  14. XCTAssertNotNil(router.route(nil, path: "/"))
  15. }
  16. func testHttpRouterSimplePathSegments() {
  17. let router = HttpRouter()
  18. router.register(nil, path: "/a/b/c/d", handler: { r in
  19. return .ok(.html("OK"))
  20. })
  21. XCTAssertNil(router.route(nil, path: "/"))
  22. XCTAssertNil(router.route(nil, path: "/a"))
  23. XCTAssertNil(router.route(nil, path: "/a/b"))
  24. XCTAssertNil(router.route(nil, path: "/a/b/c"))
  25. XCTAssertNotNil(router.route(nil, path: "/a/b/c/d"))
  26. }
  27. func testHttpRouterSinglePathSegmentWildcard() {
  28. let router = HttpRouter()
  29. router.register(nil, path: "/a/*/c/d", handler: { r in
  30. return .ok(.html("OK"))
  31. })
  32. XCTAssertNil(router.route(nil, path: "/"))
  33. XCTAssertNil(router.route(nil, path: "/a"))
  34. XCTAssertNotNil(router.route(nil, path: "/a/foo/c/d"))
  35. XCTAssertNotNil(router.route(nil, path: "/a/b/c/d"))
  36. XCTAssertNil(router.route(nil, path: "/a/b"))
  37. XCTAssertNil(router.route(nil, path: "/a/b/foo/d"))
  38. }
  39. func testHttpRouterVariables() {
  40. let router = HttpRouter()
  41. router.register(nil, path: "/a/:arg1/:arg2/b/c/d/:arg3", handler: { r in
  42. return .ok(.html("OK"))
  43. })
  44. XCTAssertNil(router.route(nil, path: "/"))
  45. XCTAssertNil(router.route(nil, path: "/a"))
  46. XCTAssertNil(router.route(nil, path: "/a/b/c/d"))
  47. XCTAssertEqual(router.route(nil, path: "/a/value1/value2/b/c/d/value3")?.0[":arg1"], "value1")
  48. XCTAssertEqual(router.route(nil, path: "/a/value1/value2/b/c/d/value3")?.0[":arg2"], "value2")
  49. XCTAssertEqual(router.route(nil, path: "/a/value1/value2/b/c/d/value3")?.0[":arg3"], "value3")
  50. }
  51. func testHttpRouterMultiplePathSegmentWildcards() {
  52. let router = HttpRouter()
  53. router.register(nil, path: "/a/**/e/f/g", handler: { r in
  54. return .ok(.html("OK"))
  55. })
  56. XCTAssertNil(router.route(nil, path: "/"))
  57. XCTAssertNil(router.route(nil, path: "/a"))
  58. XCTAssertNotNil(router.route(nil, path: "/a/b/c/d/e/f/g"))
  59. XCTAssertNil(router.route(nil, path: "/a/e/f/g"))
  60. }
  61. func testHttpRouterEmptyTail() {
  62. let router = HttpRouter()
  63. router.register(nil, path: "/a/b/", handler: { r in
  64. return .ok(.html("OK"))
  65. })
  66. router.register(nil, path: "/a/b/:var", handler: { r in
  67. return .ok(.html("OK"))
  68. })
  69. XCTAssertNil(router.route(nil, path: "/"))
  70. XCTAssertNil(router.route(nil, path: "/a"))
  71. XCTAssertNotNil(router.route(nil, path: "/a/b/"))
  72. XCTAssertNil(router.route(nil, path: "/a/e/f/g"))
  73. XCTAssertEqual(router.route(nil, path: "/a/b/value1")?.0[":var"], "value1")
  74. XCTAssertEqual(router.route(nil, path: "/a/b/")?.0[":var"], "")
  75. }
  76. func testHttpRouterPercentEnocedPathSegments() {
  77. let router = HttpRouter()
  78. router.register(nil, path: "/a/<>/^", handler: { r in
  79. return .ok(.html("OK"))
  80. })
  81. XCTAssertNil(router.route(nil, path: "/"))
  82. XCTAssertNil(router.route(nil, path: "/a"))
  83. XCTAssertNotNil(router.route(nil, path: "/a/%3C%3E/%5E"))
  84. }
  85. }