Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Sources/NIOHTTP1/HTTPTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,10 @@ public struct HTTPHeaders: CustomStringConvertible, ExpressibleByDictionaryLiter
internal var keepAliveState: KeepAliveState = .unknown

public var description: String {
self.headers.description
self.headers.lazy.map {
"\($0.0): \($0.1)"
}
.joined(separator: "; ")
}

internal var names: [String] {
Expand Down
11 changes: 11 additions & 0 deletions Sources/NIOPerformanceTester/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,17 @@ measureAndPrint(desc: "http_headers_canonical_form_trimming_whitespace_from_long
return count
}

measureAndPrint(desc: "http_headers_description_100k") {
let headers = HTTPHeaders(Array(repeating: ("String", "String"), count: 100))

for _ in 0..<100_000 {
let str = headers.description
precondition(str.utf8.count > 100)
}

return 0
}

measureAndPrint(desc: "bytebuffer_write_12MB_short_string_literals") {
let bufferSize = 12 * 1024 * 1024
var buffer = ByteBufferAllocator().buffer(capacity: bufferSize)
Expand Down
22 changes: 22 additions & 0 deletions Tests/NIOHTTP1Tests/HTTPHeadersTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,26 @@ class HTTPHeadersTest: XCTestCase {
headers.reserveCapacity(4)
XCTAssertEqual(headers.capacity, 4)
}

func testHTTPHeadersDescription() {
let originalHeaders = [
("User-Agent", "1"),
("host", "2"),
("X-SOMETHING", "3"),
("SET-COOKIE", "foo=bar"),
("Set-Cookie", "buz=cux"),
]

let headers = HTTPHeaders(originalHeaders)

let expectedOutput = """
User-Agent: 1; \
host: 2; \
X-SOMETHING: 3; \
SET-COOKIE: foo=bar; \
Set-Cookie: buz=cux
"""

XCTAssertEqual(expectedOutput, headers.description)
}
}
Loading