Skip to content

Commit 56336d7

Browse files
authored
Check for HTTPResponseError in FileMiddleware, rather than a concrete type (#569)
* Check for HTTPResponseError in FileMiddleware, rather than a concrete type * Add tests * Formatting
1 parent e343b69 commit 56336d7

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

Sources/Hummingbird/Middleware/FileMiddleware.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public struct FileMiddleware<Context: RequestContext, Provider: FileProvider>: R
9797
return try await next(request, context)
9898
} catch {
9999
// Guard that error is HTTP error notFound
100-
guard let httpError = error as? HTTPError, httpError.status == .notFound else {
100+
guard let httpError = error as? HTTPResponseError, httpError.status == .notFound else {
101101
throw error
102102
}
103103

Sources/HummingbirdRouter/RouteGroup.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public struct RouteGroup<Context: RouterRequestContext, Handler: MiddlewareProto
1919
public typealias Input = Request
2020
public typealias Output = Response
2121

22-
@usableFromInline
2322
/// Full URI path to route
23+
@usableFromInline
2424
let fullPath: RouterPath
2525
/// Path local to group route this group is defined in.
2626
@usableFromInline

Sources/HummingbirdTLS/TLSChannel.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ public struct TLSChannel<BaseChannel: ServerChildChannel>: ServerChildChannel {
4242
}
4343
}
4444

45-
@inlinable
4645
/// handle messages being passed down the channel pipeline
4746
/// - Parameters:
4847
/// - value: Object to process input/output on child channel
4948
/// - logger: Logger to use while processing messages
49+
@inlinable
5050
public func handle(value: BaseChannel.Value, logger: Logging.Logger) async {
5151
await self.baseChannel.handle(value: value, logger: logger)
5252
}

Tests/HummingbirdTests/FileMiddlewareTests.swift

+28
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,34 @@ final class FileMiddlewareTests: XCTestCase {
316316
}
317317
}
318318

319+
func testOnThrowCustom404() async throws {
320+
let router = Router()
321+
router.middlewares.add(FileMiddleware(".", searchForIndexHtml: true))
322+
struct Custom404Error: HTTPResponseError {
323+
var status: HTTPResponse.Status { .notFound }
324+
325+
func response(from request: Request, context: some RequestContext) throws -> Response {
326+
Response(status: self.status)
327+
}
328+
}
329+
router.get { _, _ -> String in
330+
throw Custom404Error()
331+
}
332+
let app = Application(responder: router.buildResponder())
333+
334+
let text = "Test file contents"
335+
let data = Data(text.utf8)
336+
let fileURL = URL(fileURLWithPath: "index.html")
337+
XCTAssertNoThrow(try data.write(to: fileURL))
338+
defer { XCTAssertNoThrow(try FileManager.default.removeItem(at: fileURL)) }
339+
340+
try await app.test(.router) { client in
341+
try await client.execute(uri: "/", method: .get) { response in
342+
XCTAssertEqual(String(buffer: response.body), text)
343+
}
344+
}
345+
}
346+
319347
func testFolder() async throws {
320348
let router = Router()
321349
router.middlewares.add(FileMiddleware(".", searchForIndexHtml: false))

0 commit comments

Comments
 (0)