Skip to content

Commit

Permalink
Persist invalid conversion (#36)
Browse files Browse the repository at this point in the history
* Throw PersistError.invalidConversion on invalid get(key:as:)

* Use HB 2.5
  • Loading branch information
adam-fowler authored Nov 20, 2024
1 parent 4dd8ef9 commit 3b46c7a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let package = Package(
.library(name: "PostgresMigrations", targets: ["PostgresMigrations"]),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.5.0"),
.package(url: "https://github.com/vapor/postgres-nio", from: "1.21.0"),
],
targets: [
Expand Down
22 changes: 15 additions & 7 deletions Sources/HummingbirdPostgres/PostgresPersistDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ extension PSQLError {
/// to build your table.
/// ```
/// let migrations = DatabaseMigrations()
/// let persist = PostgresPersistDriver(client: postgresClient, migrations: migrations)
/// let persist = PostgresPersistDriver(
/// client: postgresClient,
/// migrations: migrations,
/// logger: logger
/// )
/// var app = Application(...)
/// app.runBeforeServerStart {
/// try await migrations.apply(client: postgresClient, logger: logger, dryRun: applyMigrations)
Expand Down Expand Up @@ -127,13 +131,17 @@ public final class PostgresPersistDriver: PersistDriver {
"SELECT data, expires FROM _hb_pg_persist WHERE id = \(key)",
logger: self.logger
)
guard let (object, expires) = try await stream.decode((WrapperObject<Object>, Date).self)
.first(where: { _ in true })
else {
return nil
do {
guard let (object, expires) = try await stream.decode((WrapperObject<Object>, Date).self)
.first(where: { _ in true })
else {
return nil
}
guard expires > .now else { return nil }
return object.value
} catch is DecodingError {
throw PersistError.invalidConversion
}
guard expires > .now else { return nil }
return object.value
}

/// Remove key
Expand Down
25 changes: 25 additions & 0 deletions Tests/HummingbirdPostgresTests/PersistTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,31 @@ final class PersistTests: XCTestCase {
}
}

func testInvalidGetAs() async throws {
struct TestCodable: Codable {
let buffer: String
}
let app = try await Self.createApplication { router, persist in
router.put("/invalid") { _, _ -> HTTPResponse.Status in
try await persist.set(key: "test", value: TestCodable(buffer: "hello"))
return .ok
}
router.get("/invalid") { _, _ -> String? in
do {
return try await persist.get(key: "test", as: String.self)
} catch let error as PersistError where error == .invalidConversion {
throw HTTPError(.badRequest)
}
}
}
try await app.test(.router) { client in
try await client.execute(uri: "/invalid", method: .put)
try await client.execute(uri: "/invalid", method: .get) { response in
XCTAssertEqual(response.status, .badRequest)
}
}
}

func testRemove() async throws {
let app = try await Self.createApplication()
try await app.test(.router) { client in
Expand Down

0 comments on commit 3b46c7a

Please sign in to comment.