SwifterTestsHttpRouter.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // SwifterTestsHttpRouter.swift
  3. // Swifter
  4. //
  5. // Copyright © 2016 Damian Kołakowski. All rights reserved.
  6. //
  7. import XCTest
  8. @testable import Swifter
  9. class SwifterTestsHttpRouter: XCTestCase {
  10. func testHttpRouterSlashRoot() {
  11. let router = HttpRouter()
  12. router.register(nil, path: "/", handler: { r in
  13. return .ok(.html("OK"))
  14. })
  15. XCTAssertNotNil(router.route(nil, path: "/"))
  16. }
  17. func testHttpRouterSimplePathSegments() {
  18. let router = HttpRouter()
  19. router.register(nil, path: "/a/b/c/d", handler: { r in
  20. return .ok(.html("OK"))
  21. })
  22. XCTAssertNil(router.route(nil, path: "/"))
  23. XCTAssertNil(router.route(nil, path: "/a"))
  24. XCTAssertNil(router.route(nil, path: "/a/b"))
  25. XCTAssertNil(router.route(nil, path: "/a/b/c"))
  26. XCTAssertNotNil(router.route(nil, path: "/a/b/c/d"))
  27. }
  28. func testHttpRouterSinglePathSegmentWildcard() {
  29. let router = HttpRouter()
  30. router.register(nil, path: "/a/*/c/d", handler: { r in
  31. return .ok(.html("OK"))
  32. })
  33. XCTAssertNil(router.route(nil, path: "/"))
  34. XCTAssertNil(router.route(nil, path: "/a"))
  35. XCTAssertNotNil(router.route(nil, path: "/a/foo/c/d"))
  36. XCTAssertNotNil(router.route(nil, path: "/a/b/c/d"))
  37. XCTAssertNil(router.route(nil, path: "/a/b"))
  38. XCTAssertNil(router.route(nil, path: "/a/b/foo/d"))
  39. }
  40. func testHttpRouterVariables() {
  41. let router = HttpRouter()
  42. router.register(nil, path: "/a/:arg1/:arg2/b/c/d/:arg3", handler: { r in
  43. return .ok(.html("OK"))
  44. })
  45. XCTAssertNil(router.route(nil, path: "/"))
  46. XCTAssertNil(router.route(nil, path: "/a"))
  47. XCTAssertNil(router.route(nil, path: "/a/b/c/d"))
  48. XCTAssertEqual(router.route(nil, path: "/a/value1/value2/b/c/d/value3")?.0[":arg1"], "value1")
  49. XCTAssertEqual(router.route(nil, path: "/a/value1/value2/b/c/d/value3")?.0[":arg2"], "value2")
  50. XCTAssertEqual(router.route(nil, path: "/a/value1/value2/b/c/d/value3")?.0[":arg3"], "value3")
  51. }
  52. func testHttpRouterMultiplePathSegmentWildcards() {
  53. let router = HttpRouter()
  54. router.register(nil, path: "/a/**/e/f/g", handler: { r in
  55. return .ok(.html("OK"))
  56. })
  57. XCTAssertNil(router.route(nil, path: "/"))
  58. XCTAssertNil(router.route(nil, path: "/a"))
  59. XCTAssertNotNil(router.route(nil, path: "/a/b/c/d/e/f/g"))
  60. XCTAssertNil(router.route(nil, path: "/a/e/f/g"))
  61. }
  62. func testHttpRouterEmptyTail() {
  63. let router = HttpRouter()
  64. router.register(nil, path: "/a/b/", handler: { r in
  65. return .ok(.html("OK"))
  66. })
  67. router.register(nil, path: "/a/b/:var", handler: { r in
  68. return .ok(.html("OK"))
  69. })
  70. XCTAssertNil(router.route(nil, path: "/"))
  71. XCTAssertNil(router.route(nil, path: "/a"))
  72. XCTAssertNotNil(router.route(nil, path: "/a/b/"))
  73. XCTAssertNil(router.route(nil, path: "/a/e/f/g"))
  74. XCTAssertEqual(router.route(nil, path: "/a/b/value1")?.0[":var"], "value1")
  75. XCTAssertEqual(router.route(nil, path: "/a/b/")?.0[":var"], "")
  76. }
  77. func testHttpRouterPercentEncodedPathSegments() {
  78. let router = HttpRouter()
  79. router.register(nil, path: "/a/<>/^", handler: { r in
  80. return .ok(.html("OK"))
  81. })
  82. XCTAssertNil(router.route(nil, path: "/"))
  83. XCTAssertNil(router.route(nil, path: "/a"))
  84. XCTAssertNotNil(router.route(nil, path: "/a/%3C%3E/%5E"))
  85. }
  86. func testHttpRouterHandlesOverlappingPaths() {
  87. let router = HttpRouter()
  88. let request = HttpRequest()
  89. let staticRouteExpectation = expectation(description: "Static Route")
  90. var foundStaticRoute = false
  91. router.register("GET", path: "a/b") { _ in
  92. foundStaticRoute = true
  93. staticRouteExpectation.fulfill()
  94. return HttpResponse.accepted
  95. }
  96. let variableRouteExpectation = expectation(description: "Variable Route")
  97. var foundVariableRoute = false
  98. router.register("GET", path: "a/:id/c") { _ in
  99. foundVariableRoute = true
  100. variableRouteExpectation.fulfill()
  101. return HttpResponse.accepted
  102. }
  103. let staticRouteResult = router.route("GET", path: "a/b")
  104. let staticRouterHandler = staticRouteResult?.1
  105. XCTAssertNotNil(staticRouteResult)
  106. _ = staticRouterHandler?(request)
  107. let variableRouteResult = router.route("GET", path: "a/b/c")
  108. let variableRouterHandler = variableRouteResult?.1
  109. XCTAssertNotNil(variableRouteResult)
  110. _ = variableRouterHandler?(request)
  111. waitForExpectations(timeout: 10, handler: nil)
  112. XCTAssertTrue(foundStaticRoute)
  113. XCTAssertTrue(foundVariableRoute)
  114. }
  115. func testHttpRouterHandlesOverlappingPathsInDynamicRoutes() {
  116. let router = HttpRouter()
  117. let request = HttpRequest()
  118. let firstVariableRouteExpectation = expectation(description: "First Variable Route")
  119. var foundFirstVariableRoute = false
  120. router.register("GET", path: "a/:id") { request in
  121. foundFirstVariableRoute = true
  122. firstVariableRouteExpectation.fulfill()
  123. return HttpResponse.accepted
  124. }
  125. let secondVariableRouteExpectation = expectation(description: "Second Variable Route")
  126. var foundSecondVariableRoute = false
  127. router.register("GET", path: "a/:id/c") { _ in
  128. foundSecondVariableRoute = true
  129. secondVariableRouteExpectation.fulfill()
  130. return HttpResponse.accepted
  131. }
  132. let firstRouteResult = router.route("GET", path: "a/b")
  133. let firstRouterHandler = firstRouteResult?.1
  134. XCTAssertNotNil(firstRouteResult)
  135. _ = firstRouterHandler?(request)
  136. let secondRouteResult = router.route("GET", path: "a/b/c")
  137. let secondRouterHandler = secondRouteResult?.1
  138. XCTAssertNotNil(secondRouteResult)
  139. _ = secondRouterHandler?(request)
  140. waitForExpectations(timeout: 10, handler: nil)
  141. XCTAssertTrue(foundFirstVariableRoute)
  142. XCTAssertTrue(foundSecondVariableRoute)
  143. }
  144. }