Improved and documented the DocCModilleware middleware in the library target.

This commit is contained in:
2025-03-08 13:52:20 +01:00
parent 5b379fbc86
commit 980c8e15f6
5 changed files with 318 additions and 24 deletions
@@ -0,0 +1,55 @@
import Foundation
import Hummingbird
struct FileProviderMock {
// MARK: Properties
private var attributes: [String: Data] = [:]
private var identifiers: [String] = []
// MARK: Functions
mutating func setFile(identifier: String) {
identifiers += [identifier]
attributes[identifier] = identifier.data(using: .utf8)
}
}
// MARK: - FileProvider
extension FileProviderMock: FileProvider {
// MARK: Type aliases
typealias FileAttributes = Data
typealias FileIdentifier = String
// MARK: Functions
func getFileIdentifier(_ path: String) -> String? {
identifiers.first { $0 == path }
}
func getAttributes(id: String) async throws -> Data? {
attributes[id]
}
func loadFile(id: String, context: some RequestContext) async throws -> ResponseBody {
guard let fileData = attributes[id] else {
throw HTTPError(.notFound)
}
return .init(byteBuffer: .init(data: fileData))
}
func loadFile(
id: String,
range: ClosedRange<Int>,
context: some RequestContext
) async throws -> ResponseBody {
try await loadFile(id: id, context: context)
}
}