1
0

SwifterTestsHttpRouter.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // SwifterTestsHttpRouter.swift
  3. // Swifter
  4. //
  5. // Copyright © 2016 Damian Kołakowski. All rights reserved.
  6. //
  7. import XCTest
  8. 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"], "")
  76. }
  77. }