Skip to content

Commit 1ae4ca5

Browse files
committed
docs plus fixes for the latest release
1 parent 0791548 commit 1ae4ca5

19 files changed

+89
-71
lines changed

Sources/AppVaporTestTools/routes.swift

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,4 @@ public func routes(_ router: Router) throws {
1111
return "Hello, world!"
1212
}
1313

14-
router.get("ping") { (req)->Future<MyObject> in
15-
let ping = "{ \"code\": \"pong\" }"
16-
return try JSONDecoder().decode(MyObject.self, from: HTTPBody(string: ping), maxSize: 500, on: req)
17-
}
18-
19-
// Example of creating a Service and using it.
20-
router.get("hash", String.parameter) { req -> String in
21-
// Create a BCryptHasher using the Request's Container
22-
let hasher = try req.make(BCryptHasher.self)
23-
24-
// Fetch the String parameter (as described in the route)
25-
let string = try req.parameter(String.self)
26-
27-
// Return the hashed string!
28-
return try hasher.make(string)
29-
}
3014
}

Sources/VaporTestTools/Extensions/Requests/HTTPHeaders+Tools.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
// Created by Ondrej Rafaj on 28/02/2018.
66
//
77

8-
import Foundation
9-
import Vapor
8+
@_exported import Foundation
9+
@_exported import Vapor
1010

1111

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

14+
/// Converts dictionary into HTTPHeaders
1415
func asHTTPHeaders() -> HTTPHeaders {
1516
var headersObject = HTTPHeaders()
1617
for key in element.keys {

Sources/VaporTestTools/Extensions/Requests/HTTPRequest+Make.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
// Created by Ondrej Rafaj on 27/02/2018.
66
//
77

8-
import Foundation
9-
import Vapor
10-
import Routing
8+
@_exported import Foundation
9+
@_exported import Vapor
10+
@_exported import Routing
1111

1212

1313
public typealias URI = String
1414

1515

1616
extension TestableProperty where TestableType == HTTPRequest {
1717

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

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

36+
/// PUT HTTPRequest
3437
public static func put(uri: URI, data: Data? = nil, headers: [String: String]? = nil) -> HTTPRequest {
3538
let req = request(method: .PUT, uri: uri, data: data, headers: headers)
3639
return req
3740
}
3841

42+
/// POST HTTPRequest
3943
public static func post(uri: URI, data: Data? = nil, headers: [String: String]? = nil) -> HTTPRequest {
4044
let req = request(method: .POST, uri: uri, data: data, headers: headers)
4145
return req
4246
}
4347

48+
/// PATVH HTTPRequest
4449
public static func patch(uri: URI, data: Data? = nil, headers: [String: String]? = nil) -> HTTPRequest {
4550
let req = request(method: .PATCH, uri: uri, data: data, headers: headers)
4651
return req
4752
}
4853

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

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

66+
/// Return request and response for PUT url string & data
5967
public static func response(put uri: URI, data: Data? = nil, headers: [String: String]? = nil, with app: Application) -> TestResponse {
6068
let req = request(method: .PUT, uri: uri, data: data, headers: headers)
6169
return app.testable.response(to: req)
6270
}
6371

72+
/// Return request and response for POST url string & data
6473
public static func response(post uri: URI, data: Data? = nil, headers: [String: String]? = nil, with app: Application) -> TestResponse {
6574
let req = request(method: .POST, uri: uri, data: data, headers: headers)
6675
return app.testable.response(to: req)
6776
}
6877

78+
/// Return request and response for PATCH url string & data
6979
public static func response(patch uri: URI, data: Data? = nil, headers: [String: String]? = nil, with app: Application) -> TestResponse {
7080
let req = request(method: .PATCH, uri: uri, data: data, headers: headers)
7181
return app.testable.response(to: req)
7282
}
7383

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

Sources/VaporTestTools/Extensions/Requests/HTTPRequest+Response.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
// Created by Ondrej Rafaj on 27/02/2018.
66
//
77

8-
import Foundation
9-
import Vapor
10-
@testable import NIO
8+
@_exported import Foundation
9+
@_exported import Vapor
10+
@_exported @testable import NIO
11+
1112

1213
extension TestableProperty where TestableType == HTTPRequest {
1314

15+
/// Make response
1416
func response(using app: Application) -> Response {
1517
let responder = try! app.make(Responder.self)
1618
let wrappedRequest = Request(http: element, using: app)

Sources/VaporTestTools/Extensions/Requests/Request+Make.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import Vapor
1111

1212
extension TestableProperty where TestableType: Request {
1313

14+
/// HTTPRequest access from request
1415
public static var http: TestableProperty<HTTPRequest>.Type {
1516
return TestableProperty<HTTPRequest>.self
1617
}
1718

1819
}
20+

Sources/VaporTestTools/Extensions/Response tools/Response+Checks.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Foundation
1212

1313
extension TestableProperty where TestableType: Response {
1414

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

30+
/// Test header value
2931
public func has(header name: String, value: String? = nil) -> Bool {
3032
let headerName = HTTPHeaderName(name)
3133
return has(header: headerName, value: value)
3234
}
3335

36+
/// Test header Content-Type
3437
public func has(contentType value: String) -> Bool {
3538
let headerName = HTTPHeaderName("Content-Type")
3639
return has(header: headerName, value: value)
3740
}
3841

42+
/// Test header Content-Length
3943
public func has(contentLength value: Int) -> Bool {
4044
let headerName = HTTPHeaderName("Content-Length")
4145
return has(header: headerName, value: String(value))
4246
}
4347

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

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

58+
/// Test response content
5259
public func has(content value: String) -> Bool {
5360
return contentString == value
5461
}

Sources/VaporTestTools/Extensions/Response tools/Response+Debug.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Foundation
1111

1212
extension TestableProperty where TestableType: Response {
1313

14+
/// Prints out useful information out the response
1415
public func debug() {
1516
print("Debugging response:")
1617
print("HTTP [\(element.http.version.major).\(element.http.version.minor)] with status code [\(element.http.status.code)]")
@@ -19,10 +20,10 @@ extension TestableProperty where TestableType: Response {
1920
print("\t\(header.name.description) = \(header.value)")
2021
}
2122
print("Content:")
22-
if let size = element.content.body.count {
23+
if let size = element.content.container.http.body.count {
2324
print("\tSize: \(String(size))")
2425
}
25-
if let mediaType = element.content.mediaType {
26+
if let mediaType = element.content.container.http.mediaType {
2627
print("\tMedia type: \(mediaType.description)")
2728
}
2829
if let stringContent = element.testable.contentString {

Sources/VaporTestTools/Extensions/Response tools/Response+Decoding.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Vapor
1111

1212
extension TestableProperty where TestableType: Response {
1313

14+
/// Decode returned content
1415
public func content<T>(as type: T.Type) -> T? where T: Decodable {
1516
let object = try? element.content.decode(type).wait()
1617
return object

Sources/VaporTestTools/Extensions/Response tools/Response+Getters.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,45 @@
77

88
import Foundation
99
@testable import Vapor
10+
@testable import NIO
1011

1112

1213
extension TestableProperty where TestableType: Response {
1314

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

22+
/// Get header by it's name
2023
public func header(name: String) -> String? {
2124
let headerName = HTTPHeaderName(name)
2225
return header(name: headerName)
2326
}
2427

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

33+
/// Size of the content
2934
public var contentSize: Int? {
30-
return element.content.body.count
35+
return element.content.container.http.body.data?.count
3136
}
3237

38+
/// Get content string. Maximum of 0.5Mb of text will be returned
3339
public var contentString: String? {
34-
35-
guard let data = try? element.content.body.consumeData(max: 500000, on: fakeRequest()).wait() else {
40+
guard let data = try? element.content.container.http.body.consumeData(max: 500000, on: fakeRequest()).wait() else {
3641
return nil
3742
}
3843
return String(data: data, encoding: .utf8)
3944
}
4045

46+
/// Get content string with encoding. Maximum of 0.5Mb of text will be returned
4147
public func contentString(encoding: String.Encoding) -> String? {
42-
guard let data = try? element.content.body.consumeData(max: 500000, on: fakeRequest()).wait() else {
48+
guard let data = try? element.content.container.http.body.consumeData(max: 500000, on: fakeRequest()).wait() else {
4349
return nil
4450
}
4551
return String(data: data, encoding: encoding)

Sources/VaporTestTools/Extensions/Services/Services+Testing.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Foundation
1111

1212
extension Services {
1313

14+
/// Remove particular service
1415
public mutating func remove<S>(type: S.Type) {
1516
if let existing = factories.index(where: {
1617
$0.serviceType is S.Type

0 commit comments

Comments
 (0)