Skip to content

Commit f010edb

Browse files
authored
Merge pull request #5 from tib/main
Fix incorrect content length for head requests
2 parents 7100ea1 + 0dc252e commit f010edb

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

Sources/OpenAPIVapor/VaporTransport.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,12 @@ extension VaporTransport: ServerTransport {
4949
from: vaporRequest,
5050
forPath: path
5151
)
52-
let response = try await handler(request, body, requestMetadata)
53-
return Vapor.Response(response: response.0, body: response.1)
52+
let res = try await handler(request, body, requestMetadata)
53+
let response = Vapor.Response(response: res.0, body: res.1)
54+
if let contentLength = res.0.headerFields.first(where: { $0.name == .contentLength }) {
55+
response.headers.replaceOrAdd(name: .contentLength, value: contentLength.value)
56+
}
57+
return response
5458
}
5559
}
5660
}

Tests/OpenAPIVaporTests/VaporTransportTests.swift

+24
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,30 @@ final class VaporTransportTests: XCTestCase {
2828
override func tearDown() async throws {
2929
app.shutdown()
3030
}
31+
32+
func testHeadRequestExplicitContentLength() async throws {
33+
let transport = VaporTransport(routesBuilder: app)
34+
let response = HTTPTypes.HTTPResponse(
35+
status: .ok,
36+
headerFields: [
37+
.contentLength: "42"
38+
]
39+
)
40+
try transport.register(
41+
{ _, _, _ in (response, nil) },
42+
method: .head,
43+
path: "/test"
44+
)
45+
try app.test(
46+
.HEAD,
47+
"/test",
48+
afterResponse: { response in
49+
XCTAssertEqual(response.status, .ok)
50+
let contentLength = response.headers.first(name: .contentLength)
51+
XCTAssertEqual(contentLength, "42")
52+
}
53+
)
54+
}
3155

3256
func testRequestConversion() async throws {
3357
// POST /hello/{name}

0 commit comments

Comments
 (0)