SwifterTestsHttpRouter.swift 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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"], nil)
  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. func testHttpRouterShouldHandleOverlappingRoutesInTrail() {
  145. let router = HttpRouter()
  146. let request = HttpRequest()
  147. let firstVariableRouteExpectation = expectation(description: "First Variable Route")
  148. var foundFirstVariableRoute = false
  149. router.register("GET", path: "/a/:id") { request in
  150. foundFirstVariableRoute = true
  151. firstVariableRouteExpectation.fulfill()
  152. return HttpResponse.accepted
  153. }
  154. let secondVariableRouteExpectation = expectation(description: "Second Variable Route")
  155. var foundSecondVariableRoute = false
  156. router.register("GET", path: "/a") { _ in
  157. foundSecondVariableRoute = true
  158. secondVariableRouteExpectation.fulfill()
  159. return HttpResponse.accepted
  160. }
  161. let thirdVariableRouteExpectation = expectation(description: "Third Variable Route")
  162. var foundThirdVariableRoute = false
  163. router.register("GET", path: "/a/:id/b") { request in
  164. foundThirdVariableRoute = true
  165. thirdVariableRouteExpectation.fulfill()
  166. return HttpResponse.accepted
  167. }
  168. let firstRouteResult = router.route("GET", path: "/a")
  169. let firstRouterHandler = firstRouteResult?.1
  170. XCTAssertNotNil(firstRouteResult)
  171. _ = firstRouterHandler?(request)
  172. let secondRouteResult = router.route("GET", path: "/a/b")
  173. let secondRouterHandler = secondRouteResult?.1
  174. XCTAssertNotNil(secondRouteResult)
  175. _ = secondRouterHandler?(request)
  176. let thirdRouteResult = router.route("GET", path: "/a/b/b")
  177. let thirdRouterHandler = thirdRouteResult?.1
  178. XCTAssertNotNil(thirdRouteResult)
  179. _ = thirdRouterHandler?(request)
  180. waitForExpectations(timeout: 10, handler: nil)
  181. XCTAssertTrue(foundFirstVariableRoute)
  182. XCTAssertTrue(foundSecondVariableRoute)
  183. XCTAssertTrue(foundThirdVariableRoute)
  184. }
  185. func testHttpRouterHandlesOverlappingPathsInDynamicRoutesInTheMiddle() {
  186. let router = HttpRouter()
  187. let request = HttpRequest()
  188. let firstVariableRouteExpectation = expectation(description: "First Variable Route")
  189. var foundFirstVariableRoute = false
  190. router.register("GET", path: "/a/b/c/d/e") { request in
  191. foundFirstVariableRoute = true
  192. firstVariableRouteExpectation.fulfill()
  193. return HttpResponse.accepted
  194. }
  195. let secondVariableRouteExpectation = expectation(description: "Second Variable Route")
  196. var foundSecondVariableRoute = false
  197. router.register("GET", path: "/a/:id/f/g") { _ in
  198. foundSecondVariableRoute = true
  199. secondVariableRouteExpectation.fulfill()
  200. return HttpResponse.accepted
  201. }
  202. let firstRouteResult = router.route("GET", path: "/a/b/c/d/e")
  203. let firstRouterHandler = firstRouteResult?.1
  204. XCTAssertNotNil(firstRouteResult)
  205. _ = firstRouterHandler?(request)
  206. let secondRouteResult = router.route("GET", path: "/a/b/f/g")
  207. let secondRouterHandler = secondRouteResult?.1
  208. XCTAssertNotNil(secondRouteResult)
  209. _ = secondRouterHandler?(request)
  210. waitForExpectations(timeout: 10, handler: nil)
  211. XCTAssertTrue(foundFirstVariableRoute)
  212. XCTAssertTrue(foundSecondVariableRoute)
  213. }
  214. }