Skip to content

Commit

Permalink
Return 404 for loading a folder
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed May 1, 2024
1 parent ec9d486 commit 45f5ba0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
4 changes: 1 addition & 3 deletions Sources/Hummingbird/Files/FileMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ public protocol FileMiddlewareFileAttributes {
/// "modified-date", "eTag", "content-type", "cache-control" and "content-range" headers where
/// they are relevant.
public struct FileMiddleware<Context: BaseRequestContext, Provider: FileProvider>: RouterMiddleware where Provider.FileAttributes: FileMiddlewareFileAttributes {
struct IsDirectoryError: Error {}

let cacheControl: CacheControl
let searchForIndexHtml: Bool
let fileProvider: Provider
Expand Down Expand Up @@ -152,7 +150,7 @@ extension FileMiddleware {
// if file is a directory seach and `searchForIndexHtml` is set to true
// then search for index.html in directory
if attributes.isFolder {
guard self.searchForIndexHtml else { throw IsDirectoryError() }
guard self.searchForIndexHtml else { throw HTTPError(.notFound) }
let indexPath = self.appendingPathComponent(path, "index.html")
guard let indexAttributes = try await self.fileProvider.getAttributes(path: indexPath) else {
throw HTTPError(.notFound)
Expand Down
12 changes: 12 additions & 0 deletions Tests/HummingbirdTests/FileMiddlewareTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,18 @@ class FileMiddlewareTests: XCTestCase {
}
}

func testFolder() async throws {
let router = Router()
router.middlewares.add(FileMiddleware(".", searchForIndexHtml: false))
let app = Application(responder: router.buildResponder())

try await app.test(.router) { client in
try await client.execute(uri: "/", method: .get) { response in
XCTAssertEqual(response.status, .notFound)
}
}
}

func testCustomFileProvider() async throws {
// basic file provider
struct MemoryFileProvider: FileProvider {
Expand Down

0 comments on commit 45f5ba0

Please sign in to comment.