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

Create separate HEAD routes instead of forwarding to GET route handler #2614

Merged
merged 1 commit into from
Apr 29, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions Sources/Vapor/Responder/DefaultResponder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@ internal struct DefaultResponder: Responder {
return true
}
}

// If the route isn't explicitly a HEAD route,
// and it's made up solely of .constant components,
// register a HEAD route with the same path
if route.method != .HEAD &&
route.path.allSatisfy({ component in
if case .constant(_) = component { return true }
return false
}) {
let headRoute = Route(
method: .HEAD,
path: cached.route.path,
responder: middleware.makeResponder(chainingTo: HeadResponder()),
requestType: cached.route.requestType,
responseType: cached.route.responseType)

let headCachedRoute = CachedRoute(route: headRoute, responder: middleware.makeResponder(chainingTo: HeadResponder()))

router.register(headCachedRoute, at: [.constant(HTTPMethod.HEAD.string)] + path)
}

router.register(cached, at: [.constant(route.method.string)] + path)
}
self.router = router
Expand Down Expand Up @@ -69,7 +90,17 @@ internal struct DefaultResponder: Responder {
.split(separator: "/")
.map(String.init)

// If it's a HEAD request and a HEAD route exists, return that route...
if request.method == .HEAD, let route = self.router.route(
path: [HTTPMethod.HEAD.string] + pathComponents,
parameters: &request.parameters
) {
return route
}

// ...otherwise forward HEAD requests to GET route
let method = (request.method == .HEAD) ? .GET : request.method

return self.router.route(
path: [method.string] + pathComponents,
parameters: &request.parameters
Expand Down Expand Up @@ -114,6 +145,12 @@ internal struct DefaultResponder: Responder {
}
}

private struct HeadResponder: Responder {
func respond(to request: Request) -> EventLoopFuture<Response> {
request.eventLoop.makeSucceededFuture(.init(status: .ok))
}
}

private struct NotFoundResponder: Responder {
func respond(to request: Request) -> EventLoopFuture<Response> {
request.eventLoop.makeFailedFuture(RouteNotFound())
Expand Down
38 changes: 36 additions & 2 deletions Tests/VaporTests/RouteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,22 +220,56 @@ final class RouteTests: XCTestCase {
}
}

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

app.get("hello") { req -> String in
XCTAssertEqual(req.method, .HEAD)
return "hi"
}

try app.testable(method: .running).test(.HEAD, "/hello") { res in
XCTAssertEqual(res.status, .ok)
XCTAssertEqual(res.headers.first(name: .contentLength), "0")
XCTAssertEqual(res.body.readableBytes, 0)
}
}

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

app.get("hello", ":name") { req -> String in
XCTAssertEqual(req.method, .HEAD)
return "hi"
}

try app.testable(method: .running).test(.HEAD, "/hello/joe") { res in
XCTAssertEqual(res.status, .ok)
XCTAssertEqual(res.headers.first(name: .contentLength), "2")
XCTAssertEqual(res.body.readableBytes, 0)
}
}

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

app.get("hello") { req -> Response in
return Response(status: .badRequest)
}

app.on(.HEAD, "hello") { req -> Response in
return Response(status: .found)
}

try app.testable(method: .running).test(.HEAD, "/hello") { res in
XCTAssertEqual(res.status, .found)
XCTAssertEqual(res.headers.first(name: .contentLength), "0")
XCTAssertEqual(res.body.readableBytes, 0)
}
}

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