1
0

SwifterTestsHttpRouter.swift 9.9 KB

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