Skip to content

Commit

Permalink
add drain end on collected buffer (#2222)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner0101 committed Mar 6, 2020
1 parent c064c8a commit dd933fd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions Sources/Vapor/Request/Request+Body.swift
Expand Up @@ -29,6 +29,7 @@ extension Request {
}
case .collected(let buffer):
_ = handler(.buffer(buffer))
_ = handler(.end)
case .none: break
}
}
Expand Down
47 changes: 47 additions & 0 deletions Tests/VaporTests/ApplicationTests.swift
Expand Up @@ -1439,7 +1439,54 @@ final class ApplicationTests: XCTestCase {
let data = Data([1, 2, 3, 4])
XCTAssertEqual(data.base32EncodedString(), "AEBAGBA")
XCTAssertEqual(Data(base32Encoded: "AEBAGBA"), data)
}

func testSimilarRoutingPath() throws {
let app = Application(.testing)
defer { app.shutdown() }

app.get("api","addresses") { req in
"a"
}
app.get("api", "addresses","search", ":id") { req in
"b"
}

try app.testable(method: .running).test(.GET, "/api/addresses/") { res in
XCTAssertEqual(res.body.string, "a")
}.test(.GET, "/api/addresses/search/test") { res in
XCTAssertEqual(res.body.string, "b")
}.test(.GET, "/api/addresses/search/") { res in
XCTAssertEqual(res.status, .notFound)
}.test(.GET, "/api/addresses/search") { res in
XCTAssertEqual(res.status, .notFound)
}
}

func testCollectedResponseBodyEnd() throws {
let app = Application(.testing)
defer { app.shutdown() }

app.post("drain") { req -> EventLoopFuture<HTTPStatus> in
let promise = req.eventLoop.makePromise(of: HTTPStatus.self)
req.body.drain { result in
switch result {
case .buffer: break
case .error(let error):
promise.fail(error)
case .end:
promise.succeed(.ok)
}
return req.eventLoop.makeSucceededFuture(())
}
return promise.futureResult
}

try app.testable(method: .running).test(.POST, "drain", beforeRequest: { req in
try req.content.encode(["hello": "world"])
}, afterResponse: { res in
XCTAssertEqual(res.status, .ok)
})
}
}

Expand Down

0 comments on commit dd933fd

Please sign in to comment.