| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- //
- // SwifterTestsHttpRouter.swift
- // Swifter
- //
- // Copyright © 2016 Damian Kołakowski. All rights reserved.
- //
- import XCTest
- @testable import Swifter
- class SwifterTestsHttpRouter: XCTestCase {
-
- func testHttpRouterSlashRoot() {
-
- let router = HttpRouter()
-
- router.register(nil, path: "/", handler: { _ in
- return .ok(.html("OK"))
- })
-
- XCTAssertNotNil(router.route(nil, path: "/"))
- }
-
- func testHttpRouterSimplePathSegments() {
-
- let router = HttpRouter()
-
- router.register(nil, path: "/a/b/c/d", handler: { _ in
- return .ok(.html("OK"))
- })
-
- XCTAssertNil(router.route(nil, path: "/"))
- XCTAssertNil(router.route(nil, path: "/a"))
- XCTAssertNil(router.route(nil, path: "/a/b"))
- XCTAssertNil(router.route(nil, path: "/a/b/c"))
- XCTAssertNotNil(router.route(nil, path: "/a/b/c/d"))
- }
-
- func testHttpRouterSinglePathSegmentWildcard() {
-
- let router = HttpRouter()
-
- router.register(nil, path: "/a/*/c/d", handler: { _ in
- return .ok(.html("OK"))
- })
-
- XCTAssertNil(router.route(nil, path: "/"))
- XCTAssertNil(router.route(nil, path: "/a"))
- XCTAssertNotNil(router.route(nil, path: "/a/foo/c/d"))
- XCTAssertNotNil(router.route(nil, path: "/a/b/c/d"))
- XCTAssertNil(router.route(nil, path: "/a/b"))
- XCTAssertNil(router.route(nil, path: "/a/b/foo/d"))
- }
-
- func testHttpRouterVariables() {
-
- let router = HttpRouter()
-
- router.register(nil, path: "/a/:arg1/:arg2/b/c/d/:arg3", handler: { _ in
- return .ok(.html("OK"))
- })
-
- XCTAssertNil(router.route(nil, path: "/"))
- XCTAssertNil(router.route(nil, path: "/a"))
- XCTAssertNil(router.route(nil, path: "/a/b/c/d"))
- XCTAssertEqual(router.route(nil, path: "/a/value1/value2/b/c/d/value3")?.0[":arg1"], "value1")
- XCTAssertEqual(router.route(nil, path: "/a/value1/value2/b/c/d/value3")?.0[":arg2"], "value2")
- XCTAssertEqual(router.route(nil, path: "/a/value1/value2/b/c/d/value3")?.0[":arg3"], "value3")
- }
-
- func testHttpRouterMultiplePathSegmentWildcards() {
-
- let router = HttpRouter()
-
- router.register(nil, path: "/a/**/e/f/g", handler: { _ in
- return .ok(.html("OK"))
- })
-
- XCTAssertNil(router.route(nil, path: "/"))
- XCTAssertNil(router.route(nil, path: "/a"))
- XCTAssertNotNil(router.route(nil, path: "/a/b/c/d/e/f/g"))
- XCTAssertNil(router.route(nil, path: "/a/e/f/g"))
- }
-
- func testHttpRouterEmptyTail() {
-
- let router = HttpRouter()
-
- router.register(nil, path: "/a/b/", handler: { _ in
- return .ok(.html("OK"))
- })
-
- router.register(nil, path: "/a/b/:var", handler: { _ in
- return .ok(.html("OK"))
- })
-
- XCTAssertNil(router.route(nil, path: "/"))
- XCTAssertNil(router.route(nil, path: "/a"))
- XCTAssertNotNil(router.route(nil, path: "/a/b/"))
- XCTAssertNil(router.route(nil, path: "/a/e/f/g"))
-
- XCTAssertEqual(router.route(nil, path: "/a/b/value1")?.0[":var"], "value1")
-
- XCTAssertEqual(router.route(nil, path: "/a/b/")?.0[":var"], nil)
- }
-
- func testHttpRouterPercentEncodedPathSegments() {
-
- let router = HttpRouter()
-
- router.register(nil, path: "/a/<>/^", handler: { _ in
- return .ok(.html("OK"))
- })
- XCTAssertNil(router.route(nil, path: "/"))
- XCTAssertNil(router.route(nil, path: "/a"))
- XCTAssertNotNil(router.route(nil, path: "/a/%3C%3E/%5E"))
- }
-
- func testHttpRouterHandlesOverlappingPaths() {
-
- let router = HttpRouter()
- let request = HttpRequest()
-
- let staticRouteExpectation = expectation(description: "Static Route")
- var foundStaticRoute = false
- router.register("GET", path: "a/b") { _ in
- foundStaticRoute = true
- staticRouteExpectation.fulfill()
- return HttpResponse.accepted
- }
-
- let variableRouteExpectation = expectation(description: "Variable Route")
- var foundVariableRoute = false
- router.register("GET", path: "a/:id/c") { _ in
- foundVariableRoute = true
- variableRouteExpectation.fulfill()
- return HttpResponse.accepted
- }
-
- let staticRouteResult = router.route("GET", path: "a/b")
- let staticRouterHandler = staticRouteResult?.1
- XCTAssertNotNil(staticRouteResult)
- _ = staticRouterHandler?(request)
-
- let variableRouteResult = router.route("GET", path: "a/b/c")
- let variableRouterHandler = variableRouteResult?.1
- XCTAssertNotNil(variableRouteResult)
- _ = variableRouterHandler?(request)
-
- waitForExpectations(timeout: 10, handler: nil)
- XCTAssertTrue(foundStaticRoute)
- XCTAssertTrue(foundVariableRoute)
- }
-
- func testHttpRouterHandlesOverlappingPathsInDynamicRoutes() {
- let router = HttpRouter()
- let request = HttpRequest()
-
- let firstVariableRouteExpectation = expectation(description: "First Variable Route")
- var foundFirstVariableRoute = false
- router.register("GET", path: "a/:id") { _ in
- foundFirstVariableRoute = true
- firstVariableRouteExpectation.fulfill()
- return HttpResponse.accepted
- }
-
- let secondVariableRouteExpectation = expectation(description: "Second Variable Route")
- var foundSecondVariableRoute = false
- router.register("GET", path: "a/:id/c") { _ in
- foundSecondVariableRoute = true
- secondVariableRouteExpectation.fulfill()
- return HttpResponse.accepted
- }
-
- let firstRouteResult = router.route("GET", path: "a/b")
- let firstRouterHandler = firstRouteResult?.1
- XCTAssertNotNil(firstRouteResult)
- _ = firstRouterHandler?(request)
-
- let secondRouteResult = router.route("GET", path: "a/b/c")
- let secondRouterHandler = secondRouteResult?.1
- XCTAssertNotNil(secondRouteResult)
- _ = secondRouterHandler?(request)
-
- waitForExpectations(timeout: 10, handler: nil)
- XCTAssertTrue(foundFirstVariableRoute)
- XCTAssertTrue(foundSecondVariableRoute)
- }
-
- func testHttpRouterShouldHandleOverlappingRoutesInTrail() {
- let router = HttpRouter()
- let request = HttpRequest()
-
- let firstVariableRouteExpectation = expectation(description: "First Variable Route")
- var foundFirstVariableRoute = false
- router.register("GET", path: "/a/:id") { _ in
- foundFirstVariableRoute = true
- firstVariableRouteExpectation.fulfill()
- return HttpResponse.accepted
- }
-
- let secondVariableRouteExpectation = expectation(description: "Second Variable Route")
- var foundSecondVariableRoute = false
- router.register("GET", path: "/a") { _ in
- foundSecondVariableRoute = true
- secondVariableRouteExpectation.fulfill()
- return HttpResponse.accepted
- }
-
- let thirdVariableRouteExpectation = expectation(description: "Third Variable Route")
- var foundThirdVariableRoute = false
- router.register("GET", path: "/a/:id/b") { _ in
- foundThirdVariableRoute = true
- thirdVariableRouteExpectation.fulfill()
- return HttpResponse.accepted
- }
-
- let firstRouteResult = router.route("GET", path: "/a")
- let firstRouterHandler = firstRouteResult?.1
- XCTAssertNotNil(firstRouteResult)
- _ = firstRouterHandler?(request)
-
- let secondRouteResult = router.route("GET", path: "/a/b")
- let secondRouterHandler = secondRouteResult?.1
- XCTAssertNotNil(secondRouteResult)
- _ = secondRouterHandler?(request)
-
- let thirdRouteResult = router.route("GET", path: "/a/b/b")
- let thirdRouterHandler = thirdRouteResult?.1
- XCTAssertNotNil(thirdRouteResult)
- _ = thirdRouterHandler?(request)
-
- waitForExpectations(timeout: 10, handler: nil)
- XCTAssertTrue(foundFirstVariableRoute)
- XCTAssertTrue(foundSecondVariableRoute)
- XCTAssertTrue(foundThirdVariableRoute)
- }
-
- func testHttpRouterHandlesOverlappingPathsInDynamicRoutesInTheMiddle() {
- let router = HttpRouter()
- let request = HttpRequest()
-
- let firstVariableRouteExpectation = expectation(description: "First Variable Route")
- var foundFirstVariableRoute = false
- router.register("GET", path: "/a/b/c/d/e") { _ in
- foundFirstVariableRoute = true
- firstVariableRouteExpectation.fulfill()
- return HttpResponse.accepted
- }
-
- let secondVariableRouteExpectation = expectation(description: "Second Variable Route")
- var foundSecondVariableRoute = false
- router.register("GET", path: "/a/:id/f/g") { _ in
- foundSecondVariableRoute = true
- secondVariableRouteExpectation.fulfill()
- return HttpResponse.accepted
- }
-
- let firstRouteResult = router.route("GET", path: "/a/b/c/d/e")
- let firstRouterHandler = firstRouteResult?.1
- XCTAssertNotNil(firstRouteResult)
- _ = firstRouterHandler?(request)
-
- let secondRouteResult = router.route("GET", path: "/a/b/f/g")
- let secondRouterHandler = secondRouteResult?.1
- XCTAssertNotNil(secondRouteResult)
- _ = secondRouterHandler?(request)
-
- waitForExpectations(timeout: 10, handler: nil)
- XCTAssertTrue(foundFirstVariableRoute)
- XCTAssertTrue(foundSecondVariableRoute)
- }
- }
|