Skip to content

Commit

Permalink
Fixes routing match for HTTP method (#5)
Browse files Browse the repository at this point in the history
Fixes routing match for HTTP method
  • Loading branch information
erickjung authored Dec 23, 2020
1 parent 8451095 commit 2c0c903
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/Mockingbird.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand All @@ -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)";
Expand All @@ -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)",
Expand All @@ -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)";
Expand Down
2 changes: 1 addition & 1 deletion src/Mockingbird/Store/Server/Service/ServerManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/Mockingbird/Utils/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? ""
Expand Down Expand Up @@ -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)
}
Expand Down
47 changes: 41 additions & 6 deletions src/MockingbirdTests/Test/RouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 2c0c903

Please sign in to comment.