SwifterTestsHttpRouter.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 testHttpRouterEmptyTail() {
  67. router.register(nil, path: "/a/b/", handler: { _ in
  68. return .ok(.htmlBody("OK"))
  69. })
  70. router.register(nil, path: "/a/b/:var", handler: { _ in
  71. return .ok(.htmlBody("OK"))
  72. })
  73. XCTAssertNil(router.route(nil, path: "/"))
  74. XCTAssertNil(router.route(nil, path: "/a"))
  75. XCTAssertNotNil(router.route(nil, path: "/a/b/"))
  76. XCTAssertNil(router.route(nil, path: "/a/e/f/g"))
  77. XCTAssertEqual(router.route(nil, path: "/a/b/value1")?.0[":var"], "value1")
  78. XCTAssertEqual(router.route(nil, path: "/a/b/")?.0[":var"], nil)
  79. }
  80. func testHttpRouterPercentEncodedPathSegments() {
  81. router.register(nil, path: "/a/<>/^", handler: { _ in
  82. return .ok(.htmlBody("OK"))
  83. })
  84. XCTAssertNil(router.route(nil, path: "/"))
  85. XCTAssertNil(router.route(nil, path: "/a"))
  86. XCTAssertNotNil(router.route(nil, path: "/a/%3C%3E/%5E"))
  87. }
  88. func testHttpRouterHandlesOverlappingPaths() {
  89. let request = HttpRequest()
  90. let staticRouteExpectation = expectation(description: "Static Route")
  91. var foundStaticRoute = false
  92. router.register("GET", path: "a/b") { _ in
  93. foundStaticRoute = true
  94. staticRouteExpectation.fulfill()
  95. return HttpResponse.accepted
  96. }
  97. let variableRouteExpectation = expectation(description: "Variable Route")
  98. var foundVariableRoute = false
  99. router.register("GET", path: "a/:id/c") { _ in
  100. foundVariableRoute = true
  101. variableRouteExpectation.fulfill()
  102. return HttpResponse.accepted
  103. }
  104. let staticRouteResult = router.route("GET", path: "a/b")
  105. let staticRouterHandler = staticRouteResult?.1
  106. XCTAssertNotNil(staticRouteResult)
  107. _ = staticRouterHandler?(request)
  108. let variableRouteResult = router.route("GET", path: "a/b/c")
  109. let variableRouterHandler = variableRouteResult?.1
  110. XCTAssertNotNil(variableRouteResult)
  111. _ = variableRouterHandler?(request)
  112. waitForExpectations(timeout: 10, handler: nil)
  113. XCTAssertTrue(foundStaticRoute)
  114. XCTAssertTrue(foundVariableRoute)
  115. }
  116. func testHttpRouterHandlesOverlappingPathsInDynamicRoutes() {
  117. let request = HttpRequest()
  118. let firstVariableRouteExpectation = expectation(description: "First Variable Route")
  119. var foundFirstVariableRoute = false
  120. router.register("GET", path: "a/:id") { _ 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 request = HttpRequest()
  146. let firstVariableRouteExpectation = expectation(description: "First Variable Route")
  147. var foundFirstVariableRoute = false
  148. router.register("GET", path: "/a/:id") { _ in
  149. foundFirstVariableRoute = true
  150. firstVariableRouteExpectation.fulfill()
  151. return HttpResponse.accepted
  152. }
  153. let secondVariableRouteExpectation = expectation(description: "Second Variable Route")
  154. var foundSecondVariableRoute = false
  155. router.register("GET", path: "/a") { _ in
  156. foundSecondVariableRoute = true
  157. secondVariableRouteExpectation.fulfill()
  158. return HttpResponse.accepted
  159. }
  160. let thirdVariableRouteExpectation = expectation(description: "Third Variable Route")
  161. var foundThirdVariableRoute = false
  162. router.register("GET", path: "/a/:id/b") { _ in
  163. foundThirdVariableRoute = true
  164. thirdVariableRouteExpectation.fulfill()
  165. return HttpResponse.accepted
  166. }
  167. let firstRouteResult = router.route("GET", path: "/a")
  168. let firstRouterHandler = firstRouteResult?.1
  169. XCTAssertNotNil(firstRouteResult)
  170. _ = firstRouterHandler?(request)
  171. let secondRouteResult = router.route("GET", path: "/a/b")
  172. let secondRouterHandler = secondRouteResult?.1
  173. XCTAssertNotNil(secondRouteResult)
  174. _ = secondRouterHandler?(request)
  175. let thirdRouteResult = router.route("GET", path: "/a/b/b")
  176. let thirdRouterHandler = thirdRouteResult?.1
  177. XCTAssertNotNil(thirdRouteResult)
  178. _ = thirdRouterHandler?(request)
  179. waitForExpectations(timeout: 10, handler: nil)
  180. XCTAssertTrue(foundFirstVariableRoute)
  181. XCTAssertTrue(foundSecondVariableRoute)
  182. XCTAssertTrue(foundThirdVariableRoute)
  183. }
  184. func testHttpRouterHandlesOverlappingPathsInDynamicRoutesInTheMiddle() {
  185. let request = HttpRequest()
  186. let firstVariableRouteExpectation = expectation(description: "First Variable Route")
  187. var foundFirstVariableRoute = false
  188. router.register("GET", path: "/a/b/c/d/e") { _ in
  189. foundFirstVariableRoute = true
  190. firstVariableRouteExpectation.fulfill()
  191. return HttpResponse.accepted
  192. }
  193. let secondVariableRouteExpectation = expectation(description: "Second Variable Route")
  194. var foundSecondVariableRoute = false
  195. router.register("GET", path: "/a/:id/f/g") { _ in
  196. foundSecondVariableRoute = true
  197. secondVariableRouteExpectation.fulfill()
  198. return HttpResponse.accepted
  199. }
  200. let firstRouteResult = router.route("GET", path: "/a/b/c/d/e")
  201. let firstRouterHandler = firstRouteResult?.1
  202. XCTAssertNotNil(firstRouteResult)
  203. _ = firstRouterHandler?(request)
  204. let secondRouteResult = router.route("GET", path: "/a/b/f/g")
  205. let secondRouterHandler = secondRouteResult?.1
  206. XCTAssertNotNil(secondRouteResult)
  207. _ = secondRouterHandler?(request)
  208. waitForExpectations(timeout: 10, handler: nil)
  209. XCTAssertTrue(foundFirstVariableRoute)
  210. XCTAssertTrue(foundSecondVariableRoute)
  211. }
  212. }