Skip to content

Commit

Permalink
Add PATCH support (#134)
Browse files Browse the repository at this point in the history
* Add PATCH support

* Listed change in Changelog.md

* Use space for indentation.

* Missing indentation fix

* More indentation fixes

* missing separation
  • Loading branch information
hernangonzalez authored and MP0w committed Oct 14, 2016
1 parent a72a9a5 commit 32e8196
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
### HEAD
--------------

- Added `PATCH` method support

### 2.0.0
-----------

Expand Down
27 changes: 25 additions & 2 deletions Source/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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] = [:]
Expand Down Expand Up @@ -283,10 +284,32 @@ 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]
Expand Down
16 changes: 14 additions & 2 deletions Tests/RouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit 32e8196

Please sign in to comment.