From 6cfd4a9867a079d43f61659fb718df4ea14aca11 Mon Sep 17 00:00:00 2001 From: tanner0101 Date: Tue, 8 May 2018 19:58:41 -0400 Subject: [PATCH] clean up typed data response, related to #1657 --- Sources/Vapor/Request/Request.swift | 14 ++++++++ .../Vapor/Response/TypedDataResponse.swift | 33 ------------------- Tests/VaporTests/ApplicationTests.swift | 6 ++-- 3 files changed, 17 insertions(+), 36 deletions(-) delete mode 100644 Sources/Vapor/Response/TypedDataResponse.swift diff --git a/Sources/Vapor/Request/Request.swift b/Sources/Vapor/Request/Request.swift index 4a42b58d6d..990e4eb25e 100644 --- a/Sources/Vapor/Request/Request.swift +++ b/Sources/Vapor/Request/Request.swift @@ -139,6 +139,20 @@ public final class Request: ContainerAlias, DatabaseConnectable, HTTPMessageCont return Response(http: http, using: sharedContainer) } + /// Generate a `Response` for a `HTTPBody` convertible object using the supplied `MediaType`. + /// + /// router.get("html") { req in + /// return req.makeResponse("

Hello, world!

", as: .html) + /// } + /// + /// - parameters: + /// - type: The type of data to return the container with. + public func makeResponse(_ body: LosslessHTTPBodyRepresentable, as contentType: MediaType = .plainText) -> Response { + let res = makeResponse(http: .init(body: body)) + res.http.contentType = contentType + return res + } + // MARK: Database /// See `DatabaseConnectable`. diff --git a/Sources/Vapor/Response/TypedDataResponse.swift b/Sources/Vapor/Response/TypedDataResponse.swift deleted file mode 100644 index 964e73c370..0000000000 --- a/Sources/Vapor/Response/TypedDataResponse.swift +++ /dev/null @@ -1,33 +0,0 @@ -/// A container representing `Data` with a specific content type (used for returning as a `Response`). -private struct TypedDataResponse: ResponseEncodable { - /// The data held by this container. - let data: Data - - /// The type of the data held by this container. - let contentType: MediaType - - /// See `ResponseEncodable`. - public func encode(for req: Request) throws -> EventLoopFuture { - let res = req.makeResponse() - res.http.body = data.convertToHTTPBody() - res.http.contentType = contentType - return try res.encode(for: req) - } -} - -extension Data: ResponseEncodable { - /// See `ResponseEncodable`. - public func encode(for req: Request) throws -> EventLoopFuture { - return try response(type: .any).encode(for: req) - } - - /// Get a `ResponseEncodable` container holding this data with your provided contentType. - /// - /// data.response(type: .html) - /// - /// - parameters: - /// - type: The type of data to return the container with. - public func response(type: MediaType) -> ResponseEncodable { - return TypedDataResponse(data: self, contentType: type) - } -} diff --git a/Tests/VaporTests/ApplicationTests.swift b/Tests/VaporTests/ApplicationTests.swift index 24fbdc7dba..f30f8e9b25 100644 --- a/Tests/VaporTests/ApplicationTests.swift +++ b/Tests/VaporTests/ApplicationTests.swift @@ -524,8 +524,8 @@ class ApplicationTests: XCTestCase { func testDataResponses() throws { // without specific content type try Application.makeTest { router in - router.get("hello") { req -> Data in - return "Hello!".data(using: .utf8)! + router.get("hello") { req in + return req.makeResponse("Hello!") } }.test(.GET, "hello") { res in XCTAssertEqual(res.http.status, .ok) @@ -535,7 +535,7 @@ class ApplicationTests: XCTestCase { // with specific content type try Application.makeTest { router in router.get("hey") { req -> Response in - return "Hey!".data(using: .utf8)!.response(type: .html) + return req.makeResponse("Hey!", as: .html) } }.test(.GET, "hey") { res in XCTAssertEqual(res.http.status, .ok)