Skip to content

Commit

Permalink
Add test for logging multiple headers with same anem
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed May 4, 2024
1 parent aea7a11 commit 843b655
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Sources/Hummingbird/Middleware/LogRequestMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public struct LogRequestsMiddleware<Context: BaseRequestContext>: 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] {
Expand All @@ -109,6 +109,6 @@ public struct LogRequestsMiddleware<Context: BaseRequestContext>: RouterMiddlewa
return (key: entry.name.canonicalName, value: entry.value)
}
}
return .init(headers) { "\($0),\($1)" }
return .init(headers) { "\($0), \($1)" }
}
}
30 changes: 30 additions & 0 deletions Tests/HummingbirdTests/MiddlewareTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand Down

0 comments on commit 843b655

Please sign in to comment.