Skip to content

Commit

Permalink
Fix setting public folder for FileMiddleware when using bundles (#3113
Browse files Browse the repository at this point in the history
)

Fix setting public folder for bundles.

Co-authored-by: Sam Bishop <sambobjr@gmail.com>
  • Loading branch information
grantjbutler and dannflor committed Dec 14, 2023
1 parent 9c830d4 commit 67fe736
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/Vapor/Middleware/FileMiddleware.swift
Expand Up @@ -118,7 +118,7 @@ public final class FileMiddleware: Middleware {
throw BundleSetupError.publicDirectoryIsNotAFolder
}

self.init(publicDirectory: bundleResourceURL.path, defaultFile: defaultFile, directoryAction: directoryAction)
self.init(publicDirectory: publicDirectoryURL.path, defaultFile: defaultFile, directoryAction: directoryAction)
}

/// Possible actions to take when the request doesn't have a trailing slash but matches a directory
Expand Down
15 changes: 15 additions & 0 deletions Tests/VaporTests/MiddlewareTests.swift
Expand Up @@ -126,6 +126,21 @@ final class MiddlewareTests: XCTestCase {
}
}

func testFileMiddlewareFromBundleSubfolder() throws {
var fileMiddleware: FileMiddleware!

XCTAssertNoThrow(fileMiddleware = try FileMiddleware(bundle: .module, publicDirectory: "SubUtilities"), "FileMiddleware instantiation from Bundle should not fail")

let app = Application(.testing)
defer { app.shutdown() }
app.middleware.use(fileMiddleware)

try app.testable().test(.GET, "/index.html") { result in
XCTAssertEqual(result.status, .ok)
XCTAssertEqual(result.body.string, "<h1>Subdirectory Default</h1>\n")
}
}

func testFileMiddlewareFromBundleInvalidPublicDirectory() {
XCTAssertThrowsError(try FileMiddleware(bundle: .module, publicDirectory: "/totally-real/folder")) { error in
guard let error = error as? FileMiddleware.BundleSetupError else {
Expand Down

0 comments on commit 67fe736

Please sign in to comment.