diff --git a/Sources/HTTPTypes/HTTPFields.swift b/Sources/HTTPTypes/HTTPFields.swift index d538ecc..b05c127 100644 --- a/Sources/HTTPTypes/HTTPFields.swift +++ b/Sources/HTTPTypes/HTTPFields.swift @@ -108,7 +108,29 @@ public struct HTTPFields: Sendable, Hashable { } static func == (lhs: _Storage, rhs: _Storage) -> Bool { - lhs.fields.lazy.map(\.0) == rhs.fields.lazy.map(\.0) + let leftFieldsIndex = lhs.ensureIndex + let rightFieldsIndex = rhs.ensureIndex + if leftFieldsIndex.count != rightFieldsIndex.count { + return false + } + for (name, var leftIndex) in leftFieldsIndex { + guard var rightIndex = rightFieldsIndex[name] else { + return false + } + while leftIndex != .max && rightIndex != .max { + let (leftField, leftNext) = lhs.fields[Int(leftIndex)] + let (rightField, rightNext) = rhs.fields[Int(rightIndex)] + if leftField != rightField { + return false + } + leftIndex = leftNext + rightIndex = rightNext + } + if leftIndex != rightIndex { + return false + } + } + return true } func append(field: HTTPField) { diff --git a/Tests/HTTPTypesTests/HTTPTypesTests.swift b/Tests/HTTPTypesTests/HTTPTypesTests.swift index ff3ed79..239d4f5 100644 --- a/Tests/HTTPTypesTests/HTTPTypesTests.swift +++ b/Tests/HTTPTypesTests/HTTPTypesTests.swift @@ -73,6 +73,49 @@ final class HTTPTypesTests: XCTestCase { XCTAssertEqual(response2.headerFields[.server], "HTTPServer/1.0") } + func testComparison() { + let fields1: HTTPFields = [ + .acceptEncoding: "br", + .acceptEncoding: "gzip", + .accept: "*/*", + ] + XCTAssertNotEqual(fields1, [:]) + + let fields2: HTTPFields = [ + .acceptEncoding: "br", + .acceptEncoding: "gzip", + .accept: "*/*", + ] + XCTAssertEqual(fields1, fields2) + + let fields3: HTTPFields = [ + .acceptEncoding: "br", + .accept: "*/*", + .acceptEncoding: "gzip", + ] + XCTAssertEqual(fields1, fields3) + + let fields4: HTTPFields = [ + .acceptEncoding: "br", + .accept: "*/*", + ] + XCTAssertNotEqual(fields1, fields4) + + let fields5: HTTPFields = [ + .acceptEncoding: "gzip", + .acceptEncoding: "br", + .accept: "*/*", + ] + XCTAssertNotEqual(fields1, fields5) + + let fields6: HTTPFields = [ + .acceptEncoding: "gzip", + .acceptEncoding: "br", + .acceptLanguage: "en", + ] + XCTAssertNotEqual(fields1, fields6) + } + func testSendable() { func isSendable(_ value: some Sendable) -> Bool { true } func isSendable(_ value: Any) -> Bool { false }