Skip to content

Commit

Permalink
Fix #16
Browse files Browse the repository at this point in the history
  • Loading branch information
OrkhanAlikhanov committed Nov 10, 2019
1 parent fc35a99 commit 2d047e0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Sources/Route.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ open class Route {
self.handler = handler

self.paramNames = try! Regex.matches(path, pattern: "\\{(.+?)\\}")
self.regexPattern = try! Regex.replace(path, pattern: "\\{.+?\\}", with: "\\(\\.\\+\\)\\/\\?").replacingOccurrences(of: "/", with: "\\/")
self.regexPattern = try! Regex.replace(path, pattern: "\\{.+?\\}", with: "\\([^/]+\\)\\/\\?").replacingOccurrences(of: "/", with: "\\/") + "$"

}

Expand Down
35 changes: 33 additions & 2 deletions Tests/HttpSwiftTests/HttpSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,39 @@ class HttpSwiftTests: XCTestCase {
let route = Route(method: "", path: "/api/{param1}/{param2}/next/{param3}", handler: {_ in return Response(.ok, body: [])})
XCTAssertEqual(route.paramNames, ["param1", "param2", "param3"])

let pattern = "(.+)\\/?"
XCTAssertEqual(route.regexPattern, "\\/api\\/\(pattern)\\/\(pattern)\\/next\\/\(pattern)")
let pattern = "([^\\/]+)\\/?"
XCTAssertEqual(route.regexPattern, "\\/api\\/\(pattern)\\/\(pattern)\\/next\\/\(pattern)$")
}

/// See issue 16, https://github.com/BiAtoms/Http.swift/issues/16
func testRoute2() {
let firstResponseString = "Doctor profile"
let secondResponseString = "Doctor feedback"

// Doctor profile
server.get("/doctors/{id}/") { request in
return .ok(firstResponseString)
}

// Doctor feedbacks
server.get("/doctors/{id}/feedbacks") { request in
return .ok(secondResponseString)
}

let ex1 = expectation(description: "test")
client.request("/doctors/{id}", method: .get)
.responseString { r in
XCTAssertEqual(r.value, firstResponseString)
ex1.fulfill()
}

let ex2 = expectation(description: "test2")
client.request("/doctors/{id}/feedbacks", method: .get)
.responseString { r in
XCTAssertEqual(r.value, secondResponseString)
ex2.fulfill()
}
waitForExpectations()
}

func testRequestAndResponse() {
Expand Down

0 comments on commit 2d047e0

Please sign in to comment.