Skip to content

Commit

Permalink
2.x.x testing (#31)
Browse files Browse the repository at this point in the history
* XCT -> Testing

* comment update

* Update Package.swift to use 2.x.x branch
  • Loading branch information
adam-fowler authored Mar 8, 2024
1 parent f3a2cdb commit 25dcb58
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 37 deletions.
14 changes: 7 additions & 7 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ let package = Package(
platforms: [.macOS(.v14), .iOS(.v17), .tvOS(.v17)],
products: [
.library(name: "HummingbirdAuth", targets: ["HummingbirdAuth"]),
.library(name: "HummingbirdAuthXCT", targets: ["HummingbirdAuthXCT"]),
.library(name: "HummingbirdAuthTesting", targets: ["HummingbirdAuthTesting"]),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-alpha.3"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", branch: "2.x.x"),
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0"..<"4.0.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.33.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.63.0"),
.package(url: "https://github.com/swift-extras/swift-extras-base64.git", .upToNextMinor(from: "0.7.0")),
],
targets: [
Expand All @@ -23,15 +23,15 @@ let package = Package(
.product(name: "ExtrasBase64", package: "swift-extras-base64"),
.product(name: "Hummingbird", package: "hummingbird"),
]),
.target(name: "HummingbirdAuthXCT", dependencies: [
.target(name: "HummingbirdAuthTesting", dependencies: [
.byName(name: "HummingbirdAuth"),
.product(name: "HummingbirdXCT", package: "hummingbird"),
.product(name: "HummingbirdTesting", package: "hummingbird"),
]),
.target(name: "CBcrypt", dependencies: []),
.testTarget(name: "HummingbirdAuthTests", dependencies: [
.byName(name: "HummingbirdAuth"),
.byName(name: "HummingbirdAuthXCT"),
.product(name: "HummingbirdXCT", package: "hummingbird"),
.byName(name: "HummingbirdAuthTesting"),
.product(name: "HummingbirdTesting", package: "hummingbird"),
.product(name: "NIOPosix", package: "swift-nio"),
]),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

import ExtrasBase64
import Hummingbird
import HummingbirdXCT
import HummingbirdTesting
import XCTest

/// Used to generate various authentication types for XCT tests
public struct HBXCTAuthentication: Equatable {
/// Used to generate various authentication types for Testing framework
public struct HBTestAuthentication: Equatable {
/// create basic authentication test
public static func basic(username: String, password: String) -> Self {
return .init(value: .basic(username: username, password: password))
Expand Down Expand Up @@ -71,7 +71,7 @@ public struct HBXCTAuthentication: Equatable {
private let value: Internal
}

extension HBXCTClientProtocol {
extension HBTestClientProtocol {
/// Send request with authentication and call test callback on the response returned
///
/// - Parameters:
Expand All @@ -82,16 +82,16 @@ extension HBXCTClientProtocol {
/// - body: Request body
/// - testCallback: Callback to test response
/// - Returns: Result of callback
public func XCTExecute<Return>(
public func execute<Return>(
uri: String,
method: HTTPRequest.Method,
headers: HTTPFields = [:],
auth: HBXCTAuthentication,
auth: HBTestAuthentication,
body: ByteBuffer? = nil,
testCallback: @escaping (HBXCTResponse) throws -> Return
testCallback: @escaping (HBTestResponse) throws -> Return
) async throws -> Return {
let request = auth.apply(uri: uri, method: method, headers: headers, body: body)
return try await self.XCTExecute(
return try await self.execute(
uri: request.uri,
method: request.method,
headers: request.headers,
Expand Down
26 changes: 13 additions & 13 deletions Tests/HummingbirdAuthTests/AuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import Hummingbird
import HummingbirdAuth
import HummingbirdAuthXCT
import HummingbirdXCT
import HummingbirdAuthTesting
import HummingbirdTesting
import NIOPosix
import XCTest

Expand Down Expand Up @@ -56,11 +56,11 @@ final class AuthTests: XCTestCase {
}
let app = HBApplication(responder: router.buildResponder())
try await app.test(.router) { client in
try await client.XCTExecute(uri: "/", method: .get, auth: .bearer("1234567890")) { response in
try await client.execute(uri: "/", method: .get, auth: .bearer("1234567890")) { response in
let body = try XCTUnwrap(response.body)
XCTAssertEqual(String(buffer: body), "1234567890")
}
try await client.XCTExecute(uri: "/", method: .get, auth: .basic(username: "adam", password: "1234")) { response in
try await client.execute(uri: "/", method: .get, auth: .basic(username: "adam", password: "1234")) { response in
XCTAssertEqual(response.status, .noContent)
}
}
Expand All @@ -73,7 +73,7 @@ final class AuthTests: XCTestCase {
}
let app = HBApplication(responder: router.buildResponder())
try await app.test(.router) { client in
try await client.XCTExecute(uri: "/", method: .get, auth: .basic(username: "adam", password: "password")) { response in
try await client.execute(uri: "/", method: .get, auth: .basic(username: "adam", password: "password")) { response in
let body = try XCTUnwrap(response.body)
XCTAssertEqual(String(buffer: body), "adam:password")
}
Expand Down Expand Up @@ -105,10 +105,10 @@ final class AuthTests: XCTestCase {
}
let app = HBApplication(responder: router.buildResponder())
try await app.test(.router) { client in
try await client.XCTExecute(uri: "/", method: .put, auth: .basic(username: "testuser", password: "testpassword123")) { response in
try await client.execute(uri: "/", method: .put, auth: .basic(username: "testuser", password: "testpassword123")) { response in
XCTAssertEqual(response.status, .ok)
}
try await client.XCTExecute(uri: "/", method: .post, auth: .basic(username: "testuser", password: "testpassword123")) { response in
try await client.execute(uri: "/", method: .post, auth: .basic(username: "testuser", password: "testpassword123")) { response in
XCTAssertEqual(response.status, .ok)
}
}
Expand All @@ -132,7 +132,7 @@ final class AuthTests: XCTestCase {
let app = HBApplication(responder: router.buildResponder())

try await app.test(.router) { client in
try await client.XCTExecute(uri: "/", method: .get) { response in
try await client.execute(uri: "/", method: .get) { response in
XCTAssertEqual(response.status, .accepted)
}
}
Expand All @@ -156,7 +156,7 @@ final class AuthTests: XCTestCase {
let app = HBApplication(responder: router.buildResponder())

try await app.test(.router) { client in
try await client.XCTExecute(uri: "/", method: .get) { response in
try await client.execute(uri: "/", method: .get) { response in
XCTAssertEqual(response.status, .ok)
}
}
Expand Down Expand Up @@ -186,10 +186,10 @@ final class AuthTests: XCTestCase {
let app = HBApplication(responder: router.buildResponder())

try await app.test(.router) { client in
try await client.XCTExecute(uri: "/authenticated", method: .get) { response in
try await client.execute(uri: "/authenticated", method: .get) { response in
XCTAssertEqual(response.status, .ok)
}
try await client.XCTExecute(uri: "/unauthenticated", method: .get) { response in
try await client.execute(uri: "/unauthenticated", method: .get) { response in
XCTAssertEqual(response.status, .unauthorized)
}
}
Expand Down Expand Up @@ -225,12 +225,12 @@ final class AuthTests: XCTestCase {
let app = HBApplication(responder: router.buildResponder())

try await app.test(.router) { client in
let responseCookies = try await client.XCTExecute(uri: "/session", method: .put) { response -> String? in
let responseCookies = try await client.execute(uri: "/session", method: .put) { response -> String? in
XCTAssertEqual(response.status, .ok)
return response.headers[.setCookie]
}
let cookies = try XCTUnwrap(responseCookies)
try await client.XCTExecute(uri: "/session", method: .get, headers: [.cookie: cookies]) { response in
try await client.execute(uri: "/session", method: .get, headers: [.cookie: cookies]) { response in
XCTAssertEqual(response.status, .ok)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/HummingbirdAuthTests/OTPTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import Hummingbird
import HummingbirdAuth
import HummingbirdXCT
import HummingbirdTesting
import XCTest

final class OTPTests: XCTestCase {
Expand Down
16 changes: 8 additions & 8 deletions Tests/HummingbirdAuthTests/SessionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import Hummingbird
import HummingbirdAuth
import HummingbirdAuthXCT
import HummingbirdXCT
import HummingbirdAuthTesting
import HummingbirdTesting
import NIOPosix
import XCTest

Expand Down Expand Up @@ -48,12 +48,12 @@ final class SessionTests: XCTestCase {
let app = HBApplication(responder: router.buildResponder())

try await app.test(.router) { client in
let responseCookies = try await client.XCTExecute(uri: "/session", method: .put) { response -> String? in
let responseCookies = try await client.execute(uri: "/session", method: .put) { response -> String? in
XCTAssertEqual(response.status, .ok)
return response.headers[.setCookie]
}
let cookies = try XCTUnwrap(responseCookies)
try await client.XCTExecute(uri: "/session", method: .get, headers: [.cookie: cookies]) { response in
try await client.execute(uri: "/session", method: .get, headers: [.cookie: cookies]) { response in
XCTAssertEqual(response.status, .ok)
}
}
Expand Down Expand Up @@ -86,17 +86,17 @@ final class SessionTests: XCTestCase {
let app = HBApplication(responder: router.buildResponder())

try await app.test(.router) { client in
let cookies = try await client.XCTExecute(uri: "/save?name=john", method: .post) { response -> String? in
let cookies = try await client.execute(uri: "/save?name=john", method: .post) { response -> String? in
XCTAssertEqual(response.status, .ok)
return response.headers[.setCookie]
}
try await client.XCTExecute(uri: "/update?name=jane", method: .post, headers: cookies.map { [.cookie: $0] } ?? [:]) { response in
try await client.execute(uri: "/update?name=jane", method: .post, headers: cookies.map { [.cookie: $0] } ?? [:]) { response in
XCTAssertEqual(response.status, .ok)
XCTAssertNil(response.headers[.setCookie])
}

// get save username
try await client.XCTExecute(uri: "/name", method: .get, headers: cookies.map { [.cookie: $0] } ?? [:]) { response in
try await client.execute(uri: "/name", method: .get, headers: cookies.map { [.cookie: $0] } ?? [:]) { response in
XCTAssertEqual(response.status, .ok)
let buffer = try XCTUnwrap(response.body)
XCTAssertEqual(String(buffer: buffer), "jane")
Expand All @@ -120,7 +120,7 @@ final class SessionTests: XCTestCase {
let app = HBApplication(responder: router.buildResponder())

try await app.test(.router) { client in
try await client.XCTExecute(uri: "/update", method: .post) { response in
try await client.execute(uri: "/update", method: .post) { response in
XCTAssertEqual(response.status, .badRequest)
}
}
Expand Down

0 comments on commit 25dcb58

Please sign in to comment.