From 7311d21136eab486b1bdcdcb460ce8b63827027e Mon Sep 17 00:00:00 2001 From: "Hernan G. Gonzalez" Date: Wed, 12 Oct 2016 20:49:47 -0300 Subject: [PATCH 1/6] Add PATCH support --- Source/Router.swift | 28 +++++++++++++++++++++++++--- Tests/RouterTests.swift | 18 +++++++++++++++--- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/Source/Router.swift b/Source/Router.swift index ed36724..770484c 100644 --- a/Source/Router.swift +++ b/Source/Router.swift @@ -91,7 +91,7 @@ public struct Response: ResponseFieldsProvider { /** A Router object is an object in charge of intercepting outgoing network calls in order to return custom objects. You register new Router objects by using the `register` class methods. - After that, the router can be used to register different HTTP methods (GET, POST, DEL, PUT) with custom `RouteHandlers` + After that, the router can be used to register different HTTP methods (GET, POST, PATCH, DEL, PUT) with custom `RouteHandlers` */ public final class Router { @@ -118,6 +118,7 @@ public final class Router { case post = "POST" case put = "PUT" case delete = "DELETE" + case patch = "PATCH" } private var routes: [Route : RouteHandler] = [:] @@ -283,10 +284,31 @@ public final class Router { public func post(_ path: String, handler: @escaping RouteHandler) { addRoute(with: path, method: .post, handler: handler) } - + + /** + Registers a PATCH request with the given path + + The path is used together with the `Router.baseURL` to match requests. It can contain wildcard components prefixed by ":" that are later used to retrieve the components of the request: + + - "/users/:userid" and "/users/1234" will produce [userid: 1234] + + Other than wildcards the components must be matched by the request. + The path should not contain paths that are already contained in the baseURL: + + - base: "http://kakapo.com/api" -> path "/users/1234" ✅ + - base: "http://kakapo.com/api" -> path "api/users/1234" ❌ + + Trailing and leading slashes are not important for the route matching. + + - parameter path: The path used to match URL requests. + - parameter handler: A `RouteHandler` handler that will be used when the route is matched for a GET request + */ + public func patch(_ path: String, handler: @escaping RouteHandler) { + addRoute(with: path, method: .patch, handler: handler) + } /** Registers a DEL request with the given path - + The path is used together with the `Router.baseURL` to match requests. It can contain wildcard components prefixed by ":" that are later used to retrieve the components of the request: - "/users/:userid" and "/users/1234" will produce [userid: 1234] diff --git a/Tests/RouterTests.swift b/Tests/RouterTests.swift index 24cb434..0439f40 100644 --- a/Tests/RouterTests.swift +++ b/Tests/RouterTests.swift @@ -215,6 +215,7 @@ class RouterTests: QuickSpec { var calledPost = false var calledPut = false var calledDel = false + var calledPatch = false router.post("/users/:user_id") { (request) -> Serializable? in calledPost = true @@ -230,7 +231,12 @@ class RouterTests: QuickSpec { calledDel = true return nil } - + + router.patch("/users/:user_id") { (request) -> Serializable? in + calledPatch = true + return nil + } + var request = URLRequest(url: URL(string: "http://www.test.com/users/1")!) request.httpMethod = "POST" URLSession.shared.dataTask(with: request) { (_, _, _) in }.resume() @@ -248,8 +254,14 @@ class RouterTests: QuickSpec { URLSession.shared.dataTask(with: request) { (_, _, _) in }.resume() expect(calledDel).toEventually(beTrue()) - } - + + request = URLRequest(url: URL(string: "http://www.test.com/users/1")!) + request.httpMethod = "PATCH" + URLSession.shared.dataTask(with: request) { (_, _, _) in }.resume() + + expect(calledPatch).toEventually(beTrue()) + } + it("should replace handlers with same path and http methods") { var calledFirstPost = false var calledSecondPost = false From a6e2ea0941ca39f88b3ef01f95a144f69783e39a Mon Sep 17 00:00:00 2001 From: "Hernan G. Gonzalez" Date: Thu, 13 Oct 2016 00:17:35 -0300 Subject: [PATCH 2/6] Listed change in Changelog.md --- Changelog.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Changelog.md b/Changelog.md index dd6caf7..686803f 100644 --- a/Changelog.md +++ b/Changelog.md @@ -3,6 +3,8 @@ ### HEAD -------------- +- Added `PATCH` method support + ### 2.0.0 ----------- From f2b10416978f3c58e055629df3fd6cff424df79e Mon Sep 17 00:00:00 2001 From: "Hernan G. Gonzalez" Date: Thu, 13 Oct 2016 10:18:55 -0300 Subject: [PATCH 3/6] Use space for indentation. --- Source/Router.swift | 2 +- Tests/RouterTests.swift | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Source/Router.swift b/Source/Router.swift index 770484c..fe0be5f 100644 --- a/Source/Router.swift +++ b/Source/Router.swift @@ -118,7 +118,7 @@ public final class Router { case post = "POST" case put = "PUT" case delete = "DELETE" - case patch = "PATCH" + case patch = "PATCH" } private var routes: [Route : RouteHandler] = [:] diff --git a/Tests/RouterTests.swift b/Tests/RouterTests.swift index 0439f40..9966b9f 100644 --- a/Tests/RouterTests.swift +++ b/Tests/RouterTests.swift @@ -215,7 +215,7 @@ class RouterTests: QuickSpec { var calledPost = false var calledPut = false var calledDel = false - var calledPatch = false + var calledPatch = false router.post("/users/:user_id") { (request) -> Serializable? in calledPost = true @@ -232,10 +232,10 @@ class RouterTests: QuickSpec { return nil } - router.patch("/users/:user_id") { (request) -> Serializable? in - calledPatch = true - return nil - } + router.patch("/users/:user_id") { (request) -> Serializable? in + calledPatch = true + return nil + } var request = URLRequest(url: URL(string: "http://www.test.com/users/1")!) request.httpMethod = "POST" @@ -255,11 +255,11 @@ class RouterTests: QuickSpec { expect(calledDel).toEventually(beTrue()) - request = URLRequest(url: URL(string: "http://www.test.com/users/1")!) - request.httpMethod = "PATCH" - URLSession.shared.dataTask(with: request) { (_, _, _) in }.resume() + request = URLRequest(url: URL(string: "http://www.test.com/users/1")!) + request.httpMethod = "PATCH" + URLSession.shared.dataTask(with: request) { (_, _, _) in }.resume() - expect(calledPatch).toEventually(beTrue()) + expect(calledPatch).toEventually(beTrue()) } it("should replace handlers with same path and http methods") { From 0684511fc4197081e50fe3f19560817907a8f2a0 Mon Sep 17 00:00:00 2001 From: "Hernan G. Gonzalez" Date: Thu, 13 Oct 2016 10:20:44 -0300 Subject: [PATCH 4/6] Missing indentation fix --- Tests/RouterTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/RouterTests.swift b/Tests/RouterTests.swift index 9966b9f..5a2e6ce 100644 --- a/Tests/RouterTests.swift +++ b/Tests/RouterTests.swift @@ -260,7 +260,7 @@ class RouterTests: QuickSpec { URLSession.shared.dataTask(with: request) { (_, _, _) in }.resume() expect(calledPatch).toEventually(beTrue()) - } + } it("should replace handlers with same path and http methods") { var calledFirstPost = false From dfdbaa5120cd9e8543eb1c1da2a28bff3acb2579 Mon Sep 17 00:00:00 2001 From: "Hernan G. Gonzalez" Date: Thu, 13 Oct 2016 10:35:59 -0300 Subject: [PATCH 5/6] More indentation fixes --- Source/Router.swift | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Source/Router.swift b/Source/Router.swift index fe0be5f..a21f354 100644 --- a/Source/Router.swift +++ b/Source/Router.swift @@ -285,27 +285,27 @@ public final class Router { addRoute(with: path, method: .post, handler: handler) } - /** - Registers a PATCH request with the given path + /** + Registers a PATCH request with the given path - The path is used together with the `Router.baseURL` to match requests. It can contain wildcard components prefixed by ":" that are later used to retrieve the components of the request: + The path is used together with the `Router.baseURL` to match requests. It can contain wildcard components prefixed by ":" that are later used to retrieve the components of the request: - - "/users/:userid" and "/users/1234" will produce [userid: 1234] + - "/users/:userid" and "/users/1234" will produce [userid: 1234] - Other than wildcards the components must be matched by the request. - The path should not contain paths that are already contained in the baseURL: + Other than wildcards the components must be matched by the request. + The path should not contain paths that are already contained in the baseURL: - - base: "http://kakapo.com/api" -> path "/users/1234" ✅ - - base: "http://kakapo.com/api" -> path "api/users/1234" ❌ + - base: "http://kakapo.com/api" -> path "/users/1234" ✅ + - base: "http://kakapo.com/api" -> path "api/users/1234" ❌ - Trailing and leading slashes are not important for the route matching. + Trailing and leading slashes are not important for the route matching. - - parameter path: The path used to match URL requests. - - parameter handler: A `RouteHandler` handler that will be used when the route is matched for a GET request - */ - public func patch(_ path: String, handler: @escaping RouteHandler) { - addRoute(with: path, method: .patch, handler: handler) - } + - parameter path: The path used to match URL requests. + - parameter handler: A `RouteHandler` handler that will be used when the route is matched for a GET request + */ + public func patch(_ path: String, handler: @escaping RouteHandler) { + addRoute(with: path, method: .patch, handler: handler) + } /** Registers a DEL request with the given path From 0f8c72c94ce5b03071f22bb18ec8726bf2a06770 Mon Sep 17 00:00:00 2001 From: "Hernan G. Gonzalez" Date: Thu, 13 Oct 2016 10:37:07 -0300 Subject: [PATCH 6/6] missing separation --- Source/Router.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Source/Router.swift b/Source/Router.swift index a21f354..9f7bf46 100644 --- a/Source/Router.swift +++ b/Source/Router.swift @@ -306,6 +306,7 @@ public final class Router { public func patch(_ path: String, handler: @escaping RouteHandler) { addRoute(with: path, method: .patch, handler: handler) } + /** Registers a DEL request with the given path