SwifterTestsHttpRouter.swift 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. var router: HttpRouter!
  11. override func setUp() {
  12. super.setUp()
  13. router = HttpRouter()
  14. }
  15. override func tearDown() {
  16. router = nil
  17. super.tearDown()
  18. }
  19. func testHttpRouterSlashRoot() {
  20. router.register(nil, path: "/", handler: { _ in
  21. return .ok(.htmlBody("OK"), [:])
  22. })
  23. XCTAssertNotNil(router.route(nil, path: "/"))
  24. }
  25. func testHttpRouterSimplePathSegments() {
  26. router.register(nil, path: "/a/b/c/d", handler: { _ in
  27. return .ok(.htmlBody("OK"), [:])
  28. })
  29. XCTAssertNil(router.route(nil, path: "/"))
  30. XCTAssertNil(router.route(nil, path: "/a"))
  31. XCTAssertNil(router.route(nil, path: "/a/b"))
  32. XCTAssertNil(router.route(nil, path: "/a/b/c"))
  33. XCTAssertNotNil(router.route(nil, path: "/a/b/c/d"))
  34. }
  35. func testHttpRouterSinglePathSegmentWildcard() {
  36. router.register(nil, path: "/a/*/c/d", handler: { _ in
  37. return .ok(.htmlBody("OK"), [:])
  38. })
  39. XCTAssertNil(router.route(nil, path: "/"))
  40. XCTAssertNil(router.route(nil, path: "/a"))
  41. XCTAssertNotNil(router.route(nil, path: "/a/foo/c/d"))
  42. XCTAssertNotNil(router.route(nil, path: "/a/b/c/d"))
  43. XCTAssertNil(router.route(nil, path: "/a/b"))
  44. XCTAssertNil(router.route(nil, path: "/a/b/foo/d"))
  45. }
  46. func testHttpRouterVariables() {
  47. router.register(nil, path: "/a/:arg1/:arg2/b/c/d/:arg3", handler: { _ in
  48. return .ok(.htmlBody("OK"), [:])
  49. })
  50. XCTAssertNil(router.route(nil, path: "/"))
  51. XCTAssertNil(router.route(nil, path: "/a"))
  52. XCTAssertNil(router.route(nil, path: "/a/b/c/d"))
  53. XCTAssertEqual(router.route(nil, path: "/a/value1/value2/b/c/d/value3")?.0[":arg1"], "value1")
  54. XCTAssertEqual(router.route(nil, path: "/a/value1/value2/b/c/d/value3")?.0[":arg2"], "value2")
  55. XCTAssertEqual(router.route(nil, path: "/a/value1/value2/b/c/d/value3")?.0[":arg3"], "value3")
  56. }
  57. func testHttpRouterMultiplePathSegmentWildcards() {
  58. router.register(nil, path: "/a/**/e/f/g", handler: { _ in
  59. return .ok(.htmlBody("OK"), [:])
  60. })
  61. XCTAssertNil(router.route(nil, path: "/"))
  62. XCTAssertNil(router.route(nil, path: "/a"))
  63. XCTAssertNotNil(router.route(nil, path: "/a/b/c/d/e/f/g"))
  64. XCTAssertNil(router.route(nil, path: "/a/e/f/g"))
  65. }
  66. func testHttpRouterMultiplePathSegmentWildcardTail() {
  67. router.register(nil, path: "/a/b/**", handler: { _ in
  68. return .ok(.htmlBody("OK"), [:])
  69. })
  70. XCTAssertNil(router.route(nil, path: "/"))
  71. XCTAssertNil(router.route(nil, path: "/a"))
  72. XCTAssertNotNil(router.route(nil, path: "/a/b/c/d/e/f/g"))
  73. XCTAssertNil(router.route(nil, path: "/a/e/f/g"))
  74. }
  75. func testHttpRouterEmptyTail() {
  76. router.register(nil, path: "/a/b/", handler: { _ in
  77. return .ok(.htmlBody("OK"), [:])
  78. })
  79. router.register(nil, path: "/a/b/:var", handler: { _ in
  80. return .ok(.htmlBody("OK"), [:])
  81. })
  82. XCTAssertNil(router.route(nil, path: "/"))
  83. XCTAssertNil(router.route(nil, path: "/a"))
  84. XCTAssertNotNil(router.route(nil, path: "/a/b/"))
  85. XCTAssertNil(router.route(nil, path: "/a/e/f/g"))
  86. XCTAssertEqual(router.route(nil, path: "/a/b/value1")?.0[":var"], "value1")
  87. XCTAssertEqual(router.route(nil, path: "/a/b/")?.0[":var"], nil)
  88. }
  89. func testHttpRouterPercentEncodedPathSegments() {
  90. router.register(nil, path: "/a/<>/^", handler: { _ in
  91. return .ok(.htmlBody("OK"), [:])
  92. })
  93. XCTAssertNil(router.route(nil, path: "/"))
  94. XCTAssertNil(router.route(nil, path: "/a"))
  95. XCTAssertNotNil(router.route(nil, path: "/a/%3C%3E/%5E"))
  96. }
  97. func testHttpRouterHandlesOverlappingPaths() {
  98. let request = HttpRequest()
  99. let staticRouteExpectation = expectation(description: "Static Route")
  100. var foundStaticRoute = false
  101. router.register("GET", path: "a/b") { _ in
  102. foundStaticRoute = true
  103. staticRouteExpectation.fulfill()
  104. return HttpResponse.accepted
  105. }
  106. let variableRouteExpectation = expectation(description: "Variable Route")
  107. var foundVariableRoute = false
  108. router.register("GET", path: "a/:id/c") { _ in
  109. foundVariableRoute = true
  110. variableRouteExpectation.fulfill()
  111. return HttpResponse.accepted
  112. }
  113. let staticRouteResult = router.route("GET", path: "a/b")
  114. let staticRouterHandler = staticRouteResult?.1
  115. XCTAssertNotNil(staticRouteResult)
  116. _ = staticRouterHandler?(request)
  117. let variableRouteResult = router.route("GET", path: "a/b/c")
  118. let variableRouterHandler = variableRouteResult?.1
  119. XCTAssertNotNil(variableRouteResult)
  120. _ = variableRouterHandler?(request)
  121. waitForExpectations(timeout: 10, handler: nil)
  122. XCTAssertTrue(foundStaticRoute)
  123. XCTAssertTrue(foundVariableRoute)
  124. }
  125. func testHttpRouterHandlesOverlappingPathsInDynamicRoutes() {
  126. let request = HttpRequest()
  127. let firstVariableRouteExpectation = expectation(description: "First Variable Route")
  128. var foundFirstVariableRoute = false
  129. router.register("GET", path: "a/:id") { _ in
  130. foundFirstVariableRoute = true
  131. firstVariableRouteExpectation.fulfill()
  132. return HttpResponse.accepted
  133. }
  134. let secondVariableRouteExpectation = expectation(description: "Second Variable Route")
  135. var foundSecondVariableRoute = false
  136. router.register("GET", path: "a/:id/c") { _ in
  137. foundSecondVariableRoute = true
  138. secondVariableRouteExpectation.fulfill()
  139. return HttpResponse.accepted
  140. }
  141. let firstRouteResult = router.route("GET", path: "a/b")
  142. let firstRouterHandler = firstRouteResult?.1
  143. XCTAssertNotNil(firstRouteResult)
  144. _ = firstRouterHandler?(request)
  145. let secondRouteResult = router.route("GET", path: "a/b/c")
  146. let secondRouterHandler = secondRouteResult?.1
  147. XCTAssertNotNil(secondRouteResult)
  148. _ = secondRouterHandler?(request)
  149. waitForExpectations(timeout: 10, handler: nil)
  150. XCTAssertTrue(foundFirstVariableRoute)
  151. XCTAssertTrue(foundSecondVariableRoute)
  152. }
  153. func testHttpRouterShouldHandleOverlappingRoutesInTrail() {
  154. let request = HttpRequest()
  155. let firstVariableRouteExpectation = expectation(description: "First Variable Route")
  156. var foundFirstVariableRoute = false
  157. router.register("GET", path: "/a/:id") { _ in
  158. foundFirstVariableRoute = true
  159. firstVariableRouteExpectation.fulfill()
  160. return HttpResponse.accepted
  161. }
  162. let secondVariableRouteExpectation = expectation(description: "Second Variable Route")
  163. var foundSecondVariableRoute = false
  164. router.register("GET", path: "/a") { _ in
  165. foundSecondVariableRoute = true
  166. secondVariableRouteExpectation.fulfill()
  167. return HttpResponse.accepted
  168. }
  169. let thirdVariableRouteExpectation = expectation(description: "Third Variable Route")
  170. var foundThirdVariableRoute = false
  171. router.register("GET", path: "/a/:id/b") { _ in
  172. foundThirdVariableRoute = true
  173. thirdVariableRouteExpectation.fulfill()
  174. return HttpResponse.accepted
  175. }
  176. let firstRouteResult = router.route("GET", path: "/a")
  177. let firstRouterHandler = firstRouteResult?.1
  178. XCTAssertNotNil(firstRouteResult)
  179. _ = firstRouterHandler?(request)
  180. let secondRouteResult = router.route("GET", path: "/a/b")
  181. let secondRouterHandler = secondRouteResult?.1
  182. XCTAssertNotNil(secondRouteResult)
  183. _ = secondRouterHandler?(request)
  184. let thirdRouteResult = router.route("GET", path: "/a/b/b")
  185. let thirdRouterHandler = thirdRouteResult?.1
  186. XCTAssertNotNil(thirdRouteResult)
  187. _ = thirdRouterHandler?(request)
  188. waitForExpectations(timeout: 10, handler: nil)
  189. XCTAssertTrue(foundFirstVariableRoute)
  190. XCTAssertTrue(foundSecondVariableRoute)
  191. XCTAssertTrue(foundThirdVariableRoute)
  192. }
  193. func testHttpRouterHandlesOverlappingPathsInDynamicRoutesInTheMiddle() {
  194. let request = HttpRequest()
  195. let firstVariableRouteExpectation = expectation(description: "First Variable Route")
  196. var foundFirstVariableRoute = false
  197. router.register("GET", path: "/a/b/c/d/e") { _ in
  198. foundFirstVariableRoute = true
  199. firstVariableRouteExpectation.fulfill()
  200. return HttpResponse.accepted
  201. }
  202. let secondVariableRouteExpectation = expectation(description: "Second Variable Route")
  203. var foundSecondVariableRoute = false
  204. router.register("GET", path: "/a/:id/f/g") { _ in
  205. foundSecondVariableRoute = true
  206. secondVariableRouteExpectation.fulfill()
  207. return HttpResponse.accepted
  208. }
  209. let firstRouteResult = router.route("GET", path: "/a/b/c/d/e")
  210. let firstRouterHandler = firstRouteResult?.1
  211. XCTAssertNotNil(firstRouteResult)
  212. _ = firstRouterHandler?(request)
  213. let secondRouteResult = router.route("GET", path: "/a/b/f/g")
  214. let secondRouterHandler = secondRouteResult?.1
  215. XCTAssertNotNil(secondRouteResult)
  216. _ = secondRouterHandler?(request)
  217. waitForExpectations(timeout: 10, handler: nil)
  218. XCTAssertTrue(foundFirstVariableRoute)
  219. XCTAssertTrue(foundSecondVariableRoute)
  220. }
  221. }