Skip to content

Commit

Permalink
XCT additions (#21)
Browse files Browse the repository at this point in the history
* XCT returning values

* Replace XCTExecute with Generic version

* swift 5.6 fixes

* Keep Void version of XCTExecute
  • Loading branch information
adam-fowler committed Feb 27, 2023
1 parent 3fc6be2 commit 512af7c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 16 deletions.
43 changes: 42 additions & 1 deletion Sources/HummingbirdAuthXCT/XCT+Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,47 @@ public struct HBXCTAuthentication: Equatable {

extension HBApplication {
/// Send request with authentication and call test callback on the response returned
///
/// - Parameters:
/// - uri: URI to test
/// - method: HTTP Method to test
/// - headers: Request headers
/// - auth: Authentication details
/// - body: Request body
/// - testCallback: Callback to test response
/// - Returns: Result of callback
public func XCTExecute<Return>(
uri: String,
method: HTTPMethod,
headers: HTTPHeaders = [:],
auth: HBXCTAuthentication,
body: ByteBuffer? = nil,
testCallback: @escaping (HBXCTResponse) throws -> Return
) throws -> Return {
let request = auth.apply(uri: uri, method: method, headers: headers, body: body)
return try self.xct.execute(
uri: request.uri,
method: request.method,
headers: request.headers,
body: request.body
).flatMapThrowing { response in
try testCallback(response)
}.wait()
}

/// Send request with authentication and call test callback on the response returned
///
/// Although we have a generic version of this function that returns a generic value
/// we need to keep this version around for backwards compatibility
///
/// - Parameters:
/// - uri: URI to test
/// - method: HTTP Method to test
/// - headers: Request headers
/// - auth: Authentication details
/// - body: Request body
/// - testCallback: Callback to test response
/// - Returns: Result of callback
public func XCTExecute(
uri: String,
method: HTTPMethod,
Expand All @@ -82,7 +123,7 @@ extension HBApplication {
testCallback: @escaping (HBXCTResponse) throws -> Void
) throws {
let request = auth.apply(uri: uri, method: method, headers: headers, body: body)
try self.xct.execute(
return try self.xct.execute(
uri: request.uri,
method: request.method,
headers: request.headers,
Expand Down
14 changes: 8 additions & 6 deletions Tests/HummingbirdAuthTests/AuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,9 @@ final class AuthTests: XCTestCase {
try app.XCTStart()
defer { app.XCTStop() }

var responseCookies: String?
try app.XCTExecute(uri: "/session", method: .PUT) { response in
responseCookies = response.headers["Set-Cookie"].first
let responseCookies = try app.XCTExecute(uri: "/session", method: .PUT) { response -> String? in
XCTAssertEqual(response.status, .ok)
return response.headers["Set-Cookie"].first
}
let cookies = try XCTUnwrap(responseCookies)
try app.XCTExecute(uri: "/session", method: .GET, headers: ["Cookie": cookies]) { response in
Expand Down Expand Up @@ -291,10 +290,13 @@ final class AuthTests: XCTestCase {
try app.XCTStart()
defer { app.XCTStop() }

var responseCookies: String?
try app.XCTExecute(uri: "/session", method: .PUT, auth: .basic(username: "adam", password: "password123")) { response in
responseCookies = response.headers["Set-Cookie"].first
let responseCookies = try app.XCTExecute(
uri: "/session",
method: .PUT,
auth: .basic(username: "adam", password: "password123")
) { response -> String? in
XCTAssertEqual(response.status, .ok)
return response.headers["Set-Cookie"].first
}
let cookies = try XCTUnwrap(responseCookies)
try app.XCTExecute(uri: "/session", method: .GET, headers: ["Cookie": cookies]) { response in
Expand Down
19 changes: 10 additions & 9 deletions Tests/HummingbirdAuthTests/SessionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ final class SessionTests: XCTestCase {
try app.XCTStart()
defer { app.XCTStop() }

var responseCookies: String?
try app.XCTExecute(uri: "/session", method: .PUT) { response in
responseCookies = response.headers["Set-Cookie"].first
let responseCookies = try app.XCTExecute(uri: "/session", method: .PUT) { response -> String? in
XCTAssertEqual(response.status, .ok)
return response.headers["Set-Cookie"].first
}
let cookies = try XCTUnwrap(responseCookies)
try app.XCTExecute(uri: "/session", method: .GET, headers: ["Cookie": cookies]) { response in
Expand Down Expand Up @@ -79,10 +78,9 @@ final class SessionTests: XCTestCase {
try app.XCTStart()
defer { app.XCTStop() }

var sessionHeader: String?
try app.XCTExecute(uri: "/session", method: .PUT) { response in
sessionHeader = response.headers["session_id"].first
let sessionHeader = try app.XCTExecute(uri: "/session", method: .PUT) { response -> String? in
XCTAssertEqual(response.status, .ok)
return response.headers["session_id"].first
}
let header = try XCTUnwrap(sessionHeader)
try app.XCTExecute(uri: "/session", method: .GET, headers: ["session_id": header]) { response in
Expand Down Expand Up @@ -125,10 +123,13 @@ final class SessionTests: XCTestCase {
try app.XCTStart()
defer { app.XCTStop() }

var responseCookies: String?
try app.XCTExecute(uri: "/session", method: .PUT, auth: .basic(username: "adam", password: "password123")) { response in
responseCookies = response.headers["Set-Cookie"].first
let responseCookies = try app.XCTExecute(
uri: "/session",
method: .PUT,
auth: .basic(username: "adam", password: "password123")
) { response -> String? in
XCTAssertEqual(response.status, .ok)
return response.headers["Set-Cookie"].first
}
let cookies = try XCTUnwrap(responseCookies)
try app.XCTExecute(uri: "/session", method: .GET, headers: ["Cookie": cookies]) { response in
Expand Down

0 comments on commit 512af7c

Please sign in to comment.