Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return 404 for loading a folder #432

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions Sources/Hummingbird/Middleware/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
Loading