diff --git a/Package.swift b/Package.swift index ddcfa09..ee1faed 100644 --- a/Package.swift +++ b/Package.swift @@ -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( diff --git a/Sources/VaporTestTools/Extensions/Requests/HTTPHeaders+Tools.swift b/Sources/VaporTestTools/Extensions/Requests/HTTPHeaders+Tools.swift index 6439e52..8fdfda1 100644 --- a/Sources/VaporTestTools/Extensions/Requests/HTTPHeaders+Tools.swift +++ b/Sources/VaporTestTools/Extensions/Requests/HTTPHeaders+Tools.swift @@ -14,8 +14,8 @@ extension TestableProperty where TestableType == Dictionary { 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 } diff --git a/Sources/VaporTestTools/Extensions/Requests/HTTPRequest+Make.swift b/Sources/VaporTestTools/Extensions/Requests/HTTPRequest+Make.swift index 53bb7fc..e76ab96 100644 --- a/Sources/VaporTestTools/Extensions/Requests/HTTPRequest+Make.swift +++ b/Sources/VaporTestTools/Extensions/Requests/HTTPRequest+Make.swift @@ -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) } diff --git a/Sources/VaporTestTools/Extensions/Requests/HTTPRequest+Response.swift b/Sources/VaporTestTools/Extensions/Requests/HTTPRequest+Response.swift index 250c04d..8306692 100644 --- a/Sources/VaporTestTools/Extensions/Requests/HTTPRequest+Response.swift +++ b/Sources/VaporTestTools/Extensions/Requests/HTTPRequest+Response.swift @@ -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() } } diff --git a/Sources/VaporTestTools/Extensions/Response tools/Response+Checks.swift b/Sources/VaporTestTools/Extensions/Response tools/Response+Checks.swift index e7ed1a4..4a986e6 100644 --- a/Sources/VaporTestTools/Extensions/Response tools/Response+Checks.swift +++ b/Sources/VaporTestTools/Extensions/Response tools/Response+Checks.swift @@ -7,6 +7,7 @@ import Foundation @testable import Vapor +@testable import NIO extension TestableProperty where TestableType: Response { @@ -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 +// } + } diff --git a/Sources/VaporTestTools/Extensions/Response tools/Response+Debug.swift b/Sources/VaporTestTools/Extensions/Response tools/Response+Debug.swift index 3bd7a0e..1ddd923 100644 --- a/Sources/VaporTestTools/Extensions/Response tools/Response+Debug.swift +++ b/Sources/VaporTestTools/Extensions/Response tools/Response+Debug.swift @@ -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 { diff --git a/Sources/VaporTestTools/Extensions/Response tools/Response+Decoding.swift b/Sources/VaporTestTools/Extensions/Response tools/Response+Decoding.swift index 7753bf9..173a216 100644 --- a/Sources/VaporTestTools/Extensions/Response tools/Response+Decoding.swift +++ b/Sources/VaporTestTools/Extensions/Response tools/Response+Decoding.swift @@ -12,7 +12,7 @@ import Vapor extension TestableProperty where TestableType: Response { public func content(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 } diff --git a/Sources/VaporTestTools/Extensions/Response tools/Response+Getters.swift b/Sources/VaporTestTools/Extensions/Response tools/Response+Getters.swift index 8f49f4c..e9497ff 100644 --- a/Sources/VaporTestTools/Extensions/Response tools/Response+Getters.swift +++ b/Sources/VaporTestTools/Extensions/Response tools/Response+Getters.swift @@ -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) } } diff --git a/Sources/VaporTestTools/Extensions/Testable/Application+Testable.swift b/Sources/VaporTestTools/Extensions/Testable/Application+Testable.swift index efe8a06..9ba3cd8 100644 --- a/Sources/VaporTestTools/Extensions/Testable/Application+Testable.swift +++ b/Sources/VaporTestTools/Extensions/Testable/Application+Testable.swift @@ -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 }