Skip to content

Commit

Permalink
Fix AlchemyTest
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuawright11 committed Jul 14, 2024
1 parent 47bd7c2 commit be46f14
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 32 deletions.
4 changes: 2 additions & 2 deletions AlchemyTest/Assertions/Client+Assertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ extension Client.Builder {
}

extension Client.Request {
public func hasHeader(_ name: String, value: String? = nil) -> Bool {
guard let header = headers.first(name: name) else {
public func hasHeader(_ name: HTTPField.Name, value: String? = nil) -> Bool {
guard let header = headers[name] else {
return false
}

Expand Down
17 changes: 10 additions & 7 deletions AlchemyTest/Assertions/HTTP/ContentInspector+Assertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ extension HTTPInspector {
// MARK: Header Assertions

@discardableResult
public func assertHeader(_ header: String, value: String, file: StaticString = #filePath, line: UInt = #line) -> Self {
let values = headers[header]
public func assertHeader(_ name: HTTPField.Name, value: String, file: StaticString = #filePath, line: UInt = #line) -> Self {
let values = headers[values: name]
XCTAssertFalse(values.isEmpty, file: file, line: line)
for v in values {
XCTAssertEqual(v, value, file: file, line: line)
Expand All @@ -15,14 +15,14 @@ extension HTTPInspector {
}

@discardableResult
public func assertHeaderMissing(_ header: String, file: StaticString = #filePath, line: UInt = #line) -> Self {
XCTAssert(headers[header].isEmpty, file: file, line: line)
public func assertHeaderMissing(_ header: HTTPField.Name, file: StaticString = #filePath, line: UInt = #line) -> Self {
XCTAssertNil(headers[header], file: file, line: line)
return self
}

@discardableResult
public func assertLocation(_ uri: String, file: StaticString = #filePath, line: UInt = #line) -> Self {
assertHeader("Location", value: uri, file: file, line: line)
assertHeader(.location, value: uri, file: file, line: line)
}

// MARK: Body Assertions
Expand All @@ -44,8 +44,11 @@ extension HTTPInspector {
XCTFail("Request body was nil.", file: file, line: line)
return self
}

try await body.stream.readAll(chunkHandler: assertChunk)

for try await chunk in body.stream {
assertChunk(chunk)
}

return self
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extension ResponseInspector {
}

@discardableResult
public func assertStatus(_ code: UInt, file: StaticString = #filePath, line: UInt = #line) -> Self {
public func assertStatus(_ code: Int, file: StaticString = #filePath, line: UInt = #line) -> Self {
XCTAssertEqual(status.code, code, file: file, line: line)
return self
}
Expand Down
7 changes: 1 addition & 6 deletions AlchemyTest/Fakes/Database+Fake.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ extension Database {
@discardableResult
public static func fake(_ id: Identifier? = nil, keyMapping: KeyMapping = .snakeCase, migrations: [Migration] = [], seeders: [Seeder] = []) async throws -> Database {
let db = Database.sqlite.keyMapping(keyMapping)
Container.register(db, id: id).singleton()
Container
.require(ServiceLifecycle.self)
.registerShutdown(label: "fake db", .async {
try await db.shutdown()
})
// TODO: shutdown database
db.migrations = migrations
db.seeders = seeders
if !migrations.isEmpty { try await db.migrate() }
Expand Down
6 changes: 1 addition & 5 deletions AlchemyTest/Fixtures/Request+Fake.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,21 @@ import Alchemy

extension Request {
public static func fake(
method: HTTPRequest.Method = .GET,
method: HTTPRequest.Method = .get,
uri: String = "foo",
headers: HTTPFields = [:],
version: HTTPVersion = .http1_1,
body: Bytes? = nil,
localAddress: SocketAddress? = nil,
remoteAddress: SocketAddress? = nil,
eventLoop: EventLoop = EmbeddedEventLoop(),
container: Container = Container()
) -> Request {
Request(
method: method,
uri: uri,
headers: headers,
version: version,
body: body,
localAddress: localAddress,
remoteAddress: remoteAddress,
eventLoop: eventLoop,
container: container
)
}
Expand Down
15 changes: 4 additions & 11 deletions AlchemyTest/TestCase/TestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,16 @@ import XCTest
open class TestCase<A: Application>: XCTestCase {
public final class Builder: RequestBuilder {
public var urlComponents = URLComponents()
public var method: HTTPRequest.Method = .GET
public var method: HTTPRequest.Method = .get
public var headers: HTTPFields = [:]
public var body: Bytes? = nil
private var version: HTTPVersion = .http1_1
private var remoteAddress: SocketAddress? = nil
private var app: A

fileprivate init(app: A) {
self.app = app
}

/// Set the http version of the mock request.
public func withHttpVersion(_ version: HTTPVersion) -> Self {
with { $0.version = version }
}

/// Set the remote address of the mock request.
public func withRemoteAddress(_ address: SocketAddress) -> Self {
with { $0.remoteAddress = address }
Expand All @@ -37,8 +31,7 @@ open class TestCase<A: Application>: XCTestCase {
method: method,
uri: urlComponents.path,
headers: headers,
version: version,
body: body,
body: body,
remoteAddress: remoteAddress
)
)
Expand All @@ -52,13 +45,13 @@ open class TestCase<A: Application>: XCTestCase {
open override func setUp() async throws {
try await super.setUp()
app = A()
app.bootPlugins()
try await app.bootPlugins()
try app.boot()
}

open override func tearDown() async throws {
try await super.tearDown()
try await app.stop()
await app.stop()
app.container.reset()
}
}
Expand Down

0 comments on commit be46f14

Please sign in to comment.