Skip to content

Commit

Permalink
bug: fix redirection with query parameters.
Browse files Browse the repository at this point in the history
fix: redirect to entire path
  • Loading branch information
kirkH committed Oct 7, 2023
1 parent 036d67e commit 479d052
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Sources/Vapor/Middleware/FileMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ public final class FileMiddleware: Middleware {
guard absPath.hasSuffix("/") else {
switch directoryAction.kind {
case .redirect:
let redirectLocation = request.url.string.replacingOccurrences(of: request.url.path, with: "\(request.url.path)/")
return request.eventLoop.future(
request.redirect(to: request.url.path + "/", redirectType: .permanent)
request.redirect(to: redirectLocation, redirectType: .permanent)
)
case .none:
return next.respond(to: request)
Expand Down
25 changes: 25 additions & 0 deletions Tests/VaporTests/FileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,31 @@ final class FileTests: XCTestCase {
}
}

func testRedirectWithQueryParams() throws {
let app = Application(.testing)
defer { app.shutdown() }

let path = #file.split(separator: "/").dropLast().joined(separator: "/")
app.middleware.use(
FileMiddleware(
publicDirectory: "/" + path,
defaultFile: "index.html",
directoryAction: .redirect
)
)

try app.test(.GET, "Utilities?vaporTest=test") { res in
XCTAssertEqual(res.status, .movedPermanently)
XCTAssertEqual(res.headers.first(name: .location), "/Utilities/?vaporTest=test")
}.test(.GET, "Utilities/SubUtilities?vaporTest=test") { res in
XCTAssertEqual(res.status, .movedPermanently)
XCTAssertEqual( res.headers.first(name: .location), "/Utilities/SubUtilities/?vaporTest=test")
}.test(.GET, "Utilities/SubUtilities?vaporTest=test#vapor") { res in
XCTAssertEqual(res.status, .movedPermanently)
XCTAssertEqual( res.headers.first(name: .location), "/Utilities/SubUtilities/?vaporTest=test#vapor")
}
}

func testNoRedirect() throws {
let app = Application(.testing)
defer { app.shutdown() }
Expand Down

0 comments on commit 479d052

Please sign in to comment.