diff --git a/Sources/Hummingbird/Middleware/LogRequestMiddleware.swift b/Sources/Hummingbird/Middleware/LogRequestMiddleware.swift index 79b821b14..5acb8e30d 100644 --- a/Sources/Hummingbird/Middleware/LogRequestMiddleware.swift +++ b/Sources/Hummingbird/Middleware/LogRequestMiddleware.swift @@ -96,7 +96,7 @@ public struct LogRequestsMiddleware: RouterMiddlewa return (key: entry.canonicalName, value: value) } } - return .init(headers) { "\($0),\($1)" } + return .init(headers) { "\($0), \($1)" } } func allHeaders(headers: HTTPFields, except: [HTTPField.Name]) -> [String: String] { @@ -109,6 +109,6 @@ public struct LogRequestsMiddleware: RouterMiddlewa return (key: entry.name.canonicalName, value: entry.value) } } - return .init(headers) { "\($0),\($1)" } + return .init(headers) { "\($0), \($1)" } } } diff --git a/Tests/HummingbirdTests/MiddlewareTests.swift b/Tests/HummingbirdTests/MiddlewareTests.swift index 7e6acf617..1f5c9dd32 100644 --- a/Tests/HummingbirdTests/MiddlewareTests.swift +++ b/Tests/HummingbirdTests/MiddlewareTests.swift @@ -357,6 +357,36 @@ final class MiddlewareTests: XCTestCase { } } } + + func testLogRequestMiddlewareMultipleHeaders() async throws { + let logAccumalator = TestLogHandler.LogAccumalator() + let router = Router() + router.middlewares.add(LogRequestsMiddleware(.info, includeHeaders: [.test])) + router.get("test") { _, _ in + return HTTPResponse.Status.ok + } + let app = Application( + responder: router.buildResponder(), + logger: Logger(label: "TestLogging") { label in + TestLogHandler(label, accumalator: logAccumalator) + } + ) + try await app.test(.router) { client in + var headers = HTTPFields() + headers[.test] = "One" + headers.append(.init(name: .test, value: "Two")) + try await client.execute( + uri: "/test", + method: .get, + headers: headers, + body: .init(string: "{}") + ) { _ in + let logs = logAccumalator.filter { $0.metadata?["hb_uri"]?.description == "/test" } + let firstLog = try XCTUnwrap(logs.first) + XCTAssertEqual(firstLog.metadata?["hb_headers"], .stringConvertible(["hbtest": "One, Two"])) + } + } + } } /// LogHandler used in tests. Stores all log entries in provided `LogAccumalator``