Skip to content

Commit

Permalink
nio conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
rafiki270 committed Mar 16, 2018
1 parent 95e6793 commit e19f7aa
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let package = Package(
.library(name: "VaporTestTools", targets: ["VaporTestTools"]),
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0-beta.3.1.3"),
.package(url: "https://github.com/vapor/vapor.git", .branch("nio")),
],
targets: [
.target(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ extension TestableProperty where TestableType == Dictionary<String, String> {
func asHTTPHeaders() -> HTTPHeaders {
var headersObject = HTTPHeaders()
for key in element.keys {
let value = element[key]
headersObject[HTTPHeaderName(key)] = value
let value = element[key]!
headersObject.replaceOrAdd(name: key, value: value)
}
return headersObject
}
Expand Down
27 changes: 15 additions & 12 deletions Sources/VaporTestTools/Extensions/Requests/HTTPRequest+Make.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,69 @@ import Vapor
import Routing


public typealias URI = String


extension TestableProperty where TestableType == HTTPRequest {

public static func request(method: HTTPMethod, uri: URI, data: Data? = nil, headers: [String: String]? = nil) -> HTTPRequest {
var req = HTTPRequest(method: method, uri: uri)
var req = HTTPRequest(method: method, url: URL(string: uri)!)
if let headers = headers {
req.headers = headers.testable.asHTTPHeaders()
}
if let data = data {
req.body = HTTPBody(data)
req.body = HTTPBody(data: data)
}
return req
}

public static func get(uri: URI, headers: [String: String]? = nil) -> HTTPRequest {
let req = request(method: .get, uri: uri, headers: headers)
let req = request(method: .GET, uri: uri, headers: headers)
return req
}

public static func put(uri: URI, data: Data? = nil, headers: [String: String]? = nil) -> HTTPRequest {
let req = request(method: .put, uri: uri, data: data, headers: headers)
let req = request(method: .PUT, uri: uri, data: data, headers: headers)
return req
}

public static func post(uri: URI, data: Data? = nil, headers: [String: String]? = nil) -> HTTPRequest {
let req = request(method: .post, uri: uri, data: data, headers: headers)
let req = request(method: .POST, uri: uri, data: data, headers: headers)
return req
}

public static func patch(uri: URI, data: Data? = nil, headers: [String: String]? = nil) -> HTTPRequest {
let req = request(method: .patch, uri: uri, data: data, headers: headers)
let req = request(method: .PATCH, uri: uri, data: data, headers: headers)
return req
}

public static func delete(uri: URI, headers: [String: String]? = nil) -> HTTPRequest {
let req = request(method: .delete, uri: uri, headers: headers)
let req = request(method: .DELETE, uri: uri, headers: headers)
return req
}

public static func response(get uri: URI, headers: [String: String]? = nil, with app: Application) -> TestResponse {
let req = request(method: .get, uri: uri, headers: headers)
let req = request(method: .GET, uri: uri, headers: headers)
return app.testable.response(to: req)
}

public static func response(put uri: URI, data: Data? = nil, headers: [String: String]? = nil, with app: Application) -> TestResponse {
let req = request(method: .put, uri: uri, data: data, headers: headers)
let req = request(method: .PUT, uri: uri, data: data, headers: headers)
return app.testable.response(to: req)
}

public static func response(post uri: URI, data: Data? = nil, headers: [String: String]? = nil, with app: Application) -> TestResponse {
let req = request(method: .post, uri: uri, data: data, headers: headers)
let req = request(method: .POST, uri: uri, data: data, headers: headers)
return app.testable.response(to: req)
}

public static func response(patch uri: URI, data: Data? = nil, headers: [String: String]? = nil, with app: Application) -> TestResponse {
let req = request(method: .patch, uri: uri, data: data, headers: headers)
let req = request(method: .PATCH, uri: uri, data: data, headers: headers)
return app.testable.response(to: req)
}

public static func response(delete uri: URI, headers: [String: String]? = nil, with app: Application) -> TestResponse {
let req = request(method: .delete, uri: uri, headers: headers)
let req = request(method: .DELETE, uri: uri, headers: headers)
return app.testable.response(to: req)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@

import Foundation
import Vapor
@testable import NIO

extension TestableProperty where TestableType == HTTPRequest {

func response(using app: Application) -> Response {
let responder = try! app.make(Responder.self)
let wrappedRequest = Request(http: element, using: app)
return try! responder.respond(to: wrappedRequest).await(on: app)
return try! responder.respond(to: wrappedRequest).wait()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import Foundation
@testable import Vapor
@testable import NIO


extension TestableProperty where TestableType: Response {
Expand Down Expand Up @@ -40,21 +41,16 @@ extension TestableProperty where TestableType: Response {
return has(header: headerName, value: String(value))
}

public func has(statusCode value: Int) -> Bool {
let status = HTTPStatus(code: value)
return has(statusCode: status)
}

public func has(statusCode value: Int, message: String) -> Bool {
return element.http.status.code == value && element.http.status.message == message
}

public func has(statusCode value: HTTPStatus) -> Bool {
return element.http.status.code == value.code
return has(statusCode: value)
}

public func has(content value: String) -> Bool {
return contentString == value
public func has(statusCode value: HTTPStatus, message: String) -> Bool {
return element.http.status.code == value.code && element.http.status.reasonPhrase == message
}

// public func has(content value: String) -> Bool {
// return contentString == value
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension TestableProperty where TestableType: Response {
print("\t\(header.name.description) = \(header.value)")
}
print("Content:")
if let size = element.content.body.count {
if let size = element.content.body?.count {
print("\tSize: \(String(size))")
}
if let mediaType = element.content.mediaType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Vapor
extension TestableProperty where TestableType: Response {

public func content<T>(as type: T.Type) -> T? where T: Decodable {
let object = try? element.content.decode(type).await(on: element)
let object = try? element.content.decode(type).wait()
return object
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,38 @@ import Foundation

extension TestableProperty where TestableType: Response {

public func fakeRequest() -> Request {
let http = HTTPRequest(method: .GET, url: URL(string: "/")!)
let req = Request(http: http, using: element)
return req
}

public func header(name: String) -> String? {
let headerName = HTTPHeaderName(name)
return header(name: headerName)
}

public func header(name: HTTPHeaderName) -> String? {
return element.http.headers[name]
return element.http.headers[name].first
}

public var contentSize: Int? {
return element.content.body.count
return element.content.body?.count
}

public var contentString: String? {
guard let data = try? element.content.body.makeData(max: 500000).await(on: element) else {

guard let data = try? element.content.body?.consumeData(max: 500000, on: fakeRequest()).wait() else {
return nil
}
return String(data: data, encoding: .utf8)
return String(data: data!, encoding: .utf8)
}

public func contentString(encoding: String.Encoding) -> String? {
guard let data = try? element.content.body.makeData(max: 500000).await(on: element) else {
guard let data = try? element.content.body?.consumeData(max: 500000, on: fakeRequest()).wait() else {
return nil
}
return String(data: data, encoding: encoding)
return String(data: data!, encoding: encoding)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ extension TestableProperty where TestableType: Application {
public func response(to request: HTTPRequest) -> TestResponse {
let responder = try! element.make(Responder.self)
let wrappedRequest = Request(http: request, using: element)
return try! (response: responder.respond(to: wrappedRequest).await(on: element), request: wrappedRequest)
return try! (response: responder.respond(to: wrappedRequest).wait(), request: wrappedRequest)
}

public func response(throwingTo request: HTTPRequest) throws -> TestResponse {
let responder = try element.make(Responder.self)
let wrappedRequest = Request(http: request, using: element)
return try (response: responder.respond(to: wrappedRequest).await(on: element), request: wrappedRequest)
return try (response: responder.respond(to: wrappedRequest).wait(), request: wrappedRequest)
}

public func fakeRequest() -> Request {
let http = HTTPRequest(method: .get, uri: "/")
let req = Request.init(http: http, using: element)
let http = HTTPRequest(method: .GET, url: URL(string: "/")!)
let req = Request(http: http, using: element)
return req
}

Expand Down

0 comments on commit e19f7aa

Please sign in to comment.