Skip to content

Commit

Permalink
docs plus fixes for the latest release
Browse files Browse the repository at this point in the history
  • Loading branch information
rafiki270 committed Apr 18, 2018
1 parent 0791548 commit 1ae4ca5
Show file tree
Hide file tree
Showing 19 changed files with 89 additions and 71 deletions.
16 changes: 0 additions & 16 deletions Sources/AppVaporTestTools/routes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,4 @@ public func routes(_ router: Router) throws {
return "Hello, world!"
}

router.get("ping") { (req)->Future<MyObject> in
let ping = "{ \"code\": \"pong\" }"
return try JSONDecoder().decode(MyObject.self, from: HTTPBody(string: ping), maxSize: 500, on: req)
}

// Example of creating a Service and using it.
router.get("hash", String.parameter) { req -> String in
// Create a BCryptHasher using the Request's Container
let hasher = try req.make(BCryptHasher.self)

// Fetch the String parameter (as described in the route)
let string = try req.parameter(String.self)

// Return the hashed string!
return try hasher.make(string)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
// Created by Ondrej Rafaj on 28/02/2018.
//

import Foundation
import Vapor
@_exported import Foundation
@_exported import Vapor


extension TestableProperty where TestableType == Dictionary<String, String> {

/// Converts dictionary into HTTPHeaders
func asHTTPHeaders() -> HTTPHeaders {
var headersObject = HTTPHeaders()
for key in element.keys {
Expand Down
17 changes: 14 additions & 3 deletions Sources/VaporTestTools/Extensions/Requests/HTTPRequest+Make.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
// Created by Ondrej Rafaj on 27/02/2018.
//

import Foundation
import Vapor
import Routing
@_exported import Foundation
@_exported import Vapor
@_exported import Routing


public typealias URI = String


extension TestableProperty where TestableType == HTTPRequest {

/// MAke HTTPRequest
public static func request(method: HTTPMethod, uri: URI, data: Data? = nil, headers: [String: String]? = nil) -> HTTPRequest {
var req = HTTPRequest(method: method, url: URL(string: uri)!)
if let headers = headers {
Expand All @@ -26,51 +27,61 @@ extension TestableProperty where TestableType == HTTPRequest {
return req
}

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

/// PUT HTTPRequest
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)
return req
}

/// POST HTTPRequest
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)
return req
}

/// PATVH HTTPRequest
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)
return req
}

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

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

/// Return request and response for PUT url string & data
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)
return app.testable.response(to: req)
}

/// Return request and response for POST url string & data
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)
return app.testable.response(to: req)
}

/// Return request and response for PATCH url string & data
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)
return app.testable.response(to: req)
}

/// Return request and response for DELETE url string & data
public static func response(delete uri: URI, headers: [String: String]? = nil, with app: Application) -> TestResponse {
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 @@ -5,12 +5,14 @@
// Created by Ondrej Rafaj on 27/02/2018.
//

import Foundation
import Vapor
@testable import NIO
@_exported import Foundation
@_exported import Vapor
@_exported @testable import NIO


extension TestableProperty where TestableType == HTTPRequest {

/// Make response
func response(using app: Application) -> Response {
let responder = try! app.make(Responder.self)
let wrappedRequest = Request(http: element, using: app)
Expand Down
2 changes: 2 additions & 0 deletions Sources/VaporTestTools/Extensions/Requests/Request+Make.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import Vapor

extension TestableProperty where TestableType: Request {

/// HTTPRequest access from request
public static var http: TestableProperty<HTTPRequest>.Type {
return TestableProperty<HTTPRequest>.self
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Foundation

extension TestableProperty where TestableType: Response {

/// Test header value
public func has(header name: HTTPHeaderName, value: String? = nil) -> Bool {
guard let header = header(name: name) else {
return false
Expand All @@ -26,29 +27,35 @@ extension TestableProperty where TestableType: Response {
}
}

/// Test header value
public func has(header name: String, value: String? = nil) -> Bool {
let headerName = HTTPHeaderName(name)
return has(header: headerName, value: value)
}

/// Test header Content-Type
public func has(contentType value: String) -> Bool {
let headerName = HTTPHeaderName("Content-Type")
return has(header: headerName, value: value)
}

/// Test header Content-Length
public func has(contentLength value: Int) -> Bool {
let headerName = HTTPHeaderName("Content-Length")
return has(header: headerName, value: String(value))
}

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

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

/// Test response content
public func has(content value: String) -> Bool {
return contentString == value
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Foundation

extension TestableProperty where TestableType: Response {

/// Prints out useful information out the response
public func debug() {
print("Debugging response:")
print("HTTP [\(element.http.version.major).\(element.http.version.minor)] with status code [\(element.http.status.code)]")
Expand All @@ -19,10 +20,10 @@ 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.container.http.body.count {
print("\tSize: \(String(size))")
}
if let mediaType = element.content.mediaType {
if let mediaType = element.content.container.http.mediaType {
print("\tMedia type: \(mediaType.description)")
}
if let stringContent = element.testable.contentString {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Vapor

extension TestableProperty where TestableType: Response {

/// Decode returned content
public func content<T>(as type: T.Type) -> T? where T: Decodable {
let object = try? element.content.decode(type).wait()
return object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,45 @@

import Foundation
@testable import Vapor
@testable import NIO


extension TestableProperty where TestableType: Response {

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

/// Get header by it's name
public func header(name: String) -> String? {
let headerName = HTTPHeaderName(name)
return header(name: headerName)
}

/// Get header by it's HTTPHeaderName representation
public func header(name: HTTPHeaderName) -> String? {
return element.http.headers[name].first
}

/// Size of the content
public var contentSize: Int? {
return element.content.body.count
return element.content.container.http.body.data?.count
}

/// Get content string. Maximum of 0.5Mb of text will be returned
public var contentString: String? {

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

/// Get content string with encoding. Maximum of 0.5Mb of text will be returned
public func contentString(encoding: String.Encoding) -> String? {
guard let data = try? element.content.body.consumeData(max: 500000, on: fakeRequest()).wait() else {
guard let data = try? element.content.container.http.body.consumeData(max: 500000, on: fakeRequest()).wait() else {
return nil
}
return String(data: data, encoding: encoding)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Foundation

extension Services {

/// Remove particular service
public mutating func remove<S>(type: S.Type) {
if let existing = factories.index(where: {
$0.serviceType is S.Type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation
import Vapor
import Routing


/// Response tuple containing response as well as the request
public typealias TestResponse = (response: Response, request: Request)


Expand All @@ -18,6 +18,7 @@ extension TestableProperty where TestableType: Application {
public typealias AppConfigClosure = ((_ config: inout Config, _ env: inout Vapor.Environment, _ services: inout Services) -> Void)
public typealias AppRouterClosure = ((_ router: Router) -> Void)

/// Configure a new test app (in test setup)
public static func new(config: Config = Config.default(), env: Environment? = nil, services: Services = Services.default(), _ configClosure: AppConfigClosure? = nil, _ routerClosure: AppRouterClosure) -> Application {
var config = config
var env = try! env ?? Environment.detect()
Expand All @@ -32,18 +33,21 @@ extension TestableProperty where TestableType: Application {
return app
}

/// Respond to HTTPRequest
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).wait(), request: wrappedRequest)
}

/// Respond to HTTPRequest (throwing)
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).wait(), request: wrappedRequest)
}

/// Create fake request
public func fakeRequest() -> Request {
let http = HTTPRequest(method: .GET, url: URL(string: "/")!)
let req = Request(http: http, using: element)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import Foundation


extension TestableProperty where TestableType: Decodable {


/// Make accessor
public static var make: TestableProperty<TestableType>.Type {
return TestableProperty<TestableType>.self
}
Expand All @@ -19,6 +20,7 @@ extension TestableProperty where TestableType: Decodable {

extension TestableProperty where TestableType: Decodable {

/// Decode data from JSON source
public static func fromJSON(fileNamed fileName: String, ofType type: String? = nil, inBundle bundle: Bundle? = nil) -> TestableType {
var bundle = bundle
if bundle == nil {
Expand All @@ -31,18 +33,21 @@ extension TestableProperty where TestableType: Decodable {
return fromJSON(file: url)
}

/// Decode data from JSON source
public static func fromJSON(file fileUrl: URL) -> TestableType {
let data = try! Data(contentsOf: fileUrl)
return fromJSON(data: data)
}

/// Decode data from JSON source
public static func fromJSON(string: String) -> TestableType {
guard let data = string.data(using: .utf8) else {
fatalError("Invalid string")
}
return fromJSON(data: data)
}

/// Decode data from JSON source
public static func fromJSON(data: Data) -> TestableType {
let decoder = JSONDecoder()
let object = try! decoder.decode(TestableType.self, from: data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Foundation

extension TestableProperty where TestableType: Encodable {

/// Convert to Data
public func asData() -> Data? {
let encoder = JSONEncoder()
let jsonData = try? encoder.encode(element)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
// Created by Ondrej Rafaj on 27/02/2018.
//

import Foundation
@_exported import Foundation


extension TestableProperty where TestableType == String {

/// Return data in utf8 encoding
public func asData() -> Data? {
let data = element.data(using: .utf8)
return data
Expand Down
3 changes: 2 additions & 1 deletion Sources/VaporTestTools/Properties/TestableProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
// Created by Ondrej Rafaj on 27/02/2018.
//

import Foundation
@_exported import Foundation


public struct TestableProperty<TestableType> {

/// Testable element accessor
public var element: TestableType

init(_ obj: TestableType) {
Expand Down
Loading

0 comments on commit 1ae4ca5

Please sign in to comment.