From 2c0c9037b1e472237d8b1b2a13a746f64c933cd9 Mon Sep 17 00:00:00 2001 From: Erick Jung Date: Wed, 23 Dec 2020 11:33:33 +0000 Subject: [PATCH] Fixes routing match for HTTP method (#5) Fixes routing match for HTTP method --- src/Mockingbird.xcodeproj/project.pbxproj | 8 ++-- .../Store/Server/Service/ServerManager.swift | 2 +- src/Mockingbird/Utils/Router.swift | 4 +- src/MockingbirdTests/Test/RouterTests.swift | 47 ++++++++++++++++--- 4 files changed, 48 insertions(+), 13 deletions(-) diff --git a/src/Mockingbird.xcodeproj/project.pbxproj b/src/Mockingbird.xcodeproj/project.pbxproj index 718bfa4..af38a88 100644 --- a/src/Mockingbird.xcodeproj/project.pbxproj +++ b/src/Mockingbird.xcodeproj/project.pbxproj @@ -1284,7 +1284,7 @@ CODE_SIGN_ENTITLEMENTS = Mockingbird/Resources/mockingbird.entitlements; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 3; + CURRENT_PROJECT_VERSION = 4; ENABLE_HARDENED_RUNTIME = YES; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", @@ -1297,7 +1297,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 10.13; - MARKETING_VERSION = 1.1.0; + MARKETING_VERSION = 1.1.1; OTHER_CODE_SIGN_FLAGS = ""; PRODUCT_BUNDLE_IDENTIFIER = com.farfetch.mockingbird; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -1313,7 +1313,7 @@ CODE_SIGN_ENTITLEMENTS = Mockingbird/Resources/mockingbird.entitlements; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 3; + CURRENT_PROJECT_VERSION = 4; ENABLE_HARDENED_RUNTIME = YES; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", @@ -1326,7 +1326,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 10.13; - MARKETING_VERSION = 1.1.0; + MARKETING_VERSION = 1.1.1; OTHER_CODE_SIGN_FLAGS = ""; PRODUCT_BUNDLE_IDENTIFIER = com.farfetch.mockingbird; PRODUCT_NAME = "$(TARGET_NAME)"; diff --git a/src/Mockingbird/Store/Server/Service/ServerManager.swift b/src/Mockingbird/Store/Server/Service/ServerManager.swift index fe20b64..27b1efd 100644 --- a/src/Mockingbird/Store/Server/Service/ServerManager.swift +++ b/src/Mockingbird/Store/Server/Service/ServerManager.swift @@ -76,7 +76,7 @@ private extension ServerManager { let pattern = Router.shared.convertUrlToPattern(transaction.request.url) - let (code, ret) = Router.shared.handler(for: transaction.request.url) + let (code, ret) = Router.shared.handler(for: transaction.request.url, type: transaction.request.method) if let code = code, let ret = ret { diff --git a/src/Mockingbird/Utils/Router.swift b/src/Mockingbird/Utils/Router.swift index d8f3bac..6f13a74 100644 --- a/src/Mockingbird/Utils/Router.swift +++ b/src/Mockingbird/Utils/Router.swift @@ -72,7 +72,7 @@ class Router { } } - func handler(for url: URLConvertible) -> (Int?, String?) { + func handler(for url: URLConvertible, type: String) -> (Int?, String?) { let query = retrieveQueryFrom(pattern: url.urlStringValue).removingPercentEncoding ?? "" let path = URL(string: url.urlStringValue)?.path ?? "" @@ -100,7 +100,7 @@ class Router { } else { guard let match = self.matcher.match(path, from: self.handlersKeys) else { return (nil, nil) } - guard let handler = self.handlers.first(where: { $0.pattern == match.pattern }) else { return (nil, nil) } + guard let handler = self.handlers.first(where: { $0.pattern == match.pattern && $0.type == type }) else { return (nil, nil) } return handler.factory(url, path, match.values) } diff --git a/src/MockingbirdTests/Test/RouterTests.swift b/src/MockingbirdTests/Test/RouterTests.swift index 5247cec..6bab44d 100644 --- a/src/MockingbirdTests/Test/RouterTests.swift +++ b/src/MockingbirdTests/Test/RouterTests.swift @@ -29,7 +29,7 @@ class RouterTests: XCTestCase { return (200, "") } - _ = Router.shared.handler(for: "http://www.test.com/path1") + _ = Router.shared.handler(for: "http://www.test.com/path1", type: "GET") self.wait(for: [expectation], timeout: 1) } @@ -48,7 +48,7 @@ class RouterTests: XCTestCase { return (200, "") } - _ = Router.shared.handler(for: "http://www.test.com/path2?query1=test1&query2=test2") + _ = Router.shared.handler(for: "http://www.test.com/path2?query1=test1&query2=test2", type: "GET") self.wait(for: [expectation], timeout: 1) } @@ -74,7 +74,7 @@ class RouterTests: XCTestCase { return (200, "") } - _ = Router.shared.handler(for: "http://www.test.com/dyn1/1234/path4?query1=test1&query2=test2") + _ = Router.shared.handler(for: "http://www.test.com/dyn1/1234/path4?query1=test1&query2=test2", type: "GET") self.wait(for: [expectation], timeout: 1) } @@ -100,7 +100,7 @@ class RouterTests: XCTestCase { return (200, "") } - _ = Router.shared.handler(for: "http://www.test.com/dyn2/1234/path4?query1=test1&query2=test2") + _ = Router.shared.handler(for: "http://www.test.com/dyn2/1234/path4?query1=test1&query2=test2", type: "GET") self.wait(for: [expectation], timeout: 1) } @@ -143,7 +143,7 @@ class RouterTests: XCTestCase { return (200, "") } - _ = Router.shared.handler(for: "http://www.test.com/path4?status=test1,test2,test3&query1=test1&query2=test2") + _ = Router.shared.handler(for: "http://www.test.com/path4?status=test1,test2,test3&query1=test1&query2=test2", type: "GET") self.wait(for: [expectation], timeout: 1) } @@ -180,7 +180,42 @@ class RouterTests: XCTestCase { return (200, "") } - _ = Router.shared.handler(for: "http://www.test.com/path5?status=test1,test2,test3&query1=test1&query2=test2") + _ = Router.shared.handler(for: "http://www.test.com/path5?status=test1,test2,test3&query1=test1&query2=test2", type: "GET") + + self.wait(for: [expectation], timeout: 1) + } + + func testRouterWithSamePath() { + + let expectation = XCTestExpectation(description:"testing router with same path and different type") + + Router.shared.register(type: "GET", code: 200, pattern: "/path6") { _, _, _ in + + XCTAssertFalse(true) + return (200, "") + } + + Router.shared.register(type: "PUT", code: 200, pattern: "/path6") { _, _, _ in + + XCTAssertFalse(true) + return (200, "") + } + + Router.shared.register(type: "POST", code: 200, pattern: "/path6") { _, _, _ in + + XCTAssertFalse(true) + return (200, "") + } + + Router.shared.register(type: "PATCH", code: 200, pattern: "/path6") { _, path, _ in + + XCTAssertEqual(path, "/path6") + expectation.fulfill() + + return (200, "") + } + + _ = Router.shared.handler(for: "http://www.test.com/path6", type: "PATCH") self.wait(for: [expectation], timeout: 1) }