Skip to content

Commit

Permalink
Check for HTTPResponseError in FileMiddleware, rather than a concrete…
Browse files Browse the repository at this point in the history
… type (#569)

* Check for HTTPResponseError in FileMiddleware, rather than a concrete type

* Add tests

* Formatting
  • Loading branch information
Joannis authored Sep 29, 2024
1 parent e343b69 commit 56336d7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Sources/Hummingbird/Middleware/FileMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public struct FileMiddleware<Context: RequestContext, Provider: FileProvider>: R
return try await next(request, context)
} catch {
// Guard that error is HTTP error notFound
guard let httpError = error as? HTTPError, httpError.status == .notFound else {
guard let httpError = error as? HTTPResponseError, httpError.status == .notFound else {
throw error
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/HummingbirdRouter/RouteGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public struct RouteGroup<Context: RouterRequestContext, Handler: MiddlewareProto
public typealias Input = Request
public typealias Output = Response

@usableFromInline
/// Full URI path to route
@usableFromInline
let fullPath: RouterPath
/// Path local to group route this group is defined in.
@usableFromInline
Expand Down
2 changes: 1 addition & 1 deletion Sources/HummingbirdTLS/TLSChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public struct TLSChannel<BaseChannel: ServerChildChannel>: ServerChildChannel {
}
}

@inlinable
/// handle messages being passed down the channel pipeline
/// - Parameters:
/// - value: Object to process input/output on child channel
/// - logger: Logger to use while processing messages
@inlinable
public func handle(value: BaseChannel.Value, logger: Logging.Logger) async {
await self.baseChannel.handle(value: value, logger: logger)
}
Expand Down
28 changes: 28 additions & 0 deletions Tests/HummingbirdTests/FileMiddlewareTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,34 @@ final class FileMiddlewareTests: XCTestCase {
}
}

func testOnThrowCustom404() async throws {
let router = Router()
router.middlewares.add(FileMiddleware(".", searchForIndexHtml: true))
struct Custom404Error: HTTPResponseError {
var status: HTTPResponse.Status { .notFound }

func response(from request: Request, context: some RequestContext) throws -> Response {
Response(status: self.status)
}
}
router.get { _, _ -> String in
throw Custom404Error()
}
let app = Application(responder: router.buildResponder())

let text = "Test file contents"
let data = Data(text.utf8)
let fileURL = URL(fileURLWithPath: "index.html")
XCTAssertNoThrow(try data.write(to: fileURL))
defer { XCTAssertNoThrow(try FileManager.default.removeItem(at: fileURL)) }

try await app.test(.router) { client in
try await client.execute(uri: "/", method: .get) { response in
XCTAssertEqual(String(buffer: response.body), text)
}
}
}

func testFolder() async throws {
let router = Router()
router.middlewares.add(FileMiddleware(".", searchForIndexHtml: false))
Expand Down

0 comments on commit 56336d7

Please sign in to comment.