Skip to content

Commit

Permalink
clean up typed data response, related to vapor#1657
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner0101 committed May 8, 2018
1 parent 8f62360 commit 6cfd4a9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 36 deletions.
14 changes: 14 additions & 0 deletions Sources/Vapor/Request/Request.swift
Expand Up @@ -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("<h1>Hello, world!</h1>", 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`.
Expand Down
33 changes: 0 additions & 33 deletions Sources/Vapor/Response/TypedDataResponse.swift

This file was deleted.

6 changes: 3 additions & 3 deletions Tests/VaporTests/ApplicationTests.swift
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 6cfd4a9

Please sign in to comment.