From 4b1dd0cab305bc4790fefae952d15e00c80a3322 Mon Sep 17 00:00:00 2001 From: Fabian Fett Date: Fri, 14 Aug 2026 12:56:29 +0200 Subject: [PATCH] Add hash tests, incl. a known failing one (#137) ### Motivation The more we test the more we know about current behavior ### Changes - Add tests for hashing, incl. a failing one ### Result We know that our hash implementation isn't respecting different header order --- Tests/HTTPTypesTests/HTTPTypesTests.swift | 38 +++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/Tests/HTTPTypesTests/HTTPTypesTests.swift b/Tests/HTTPTypesTests/HTTPTypesTests.swift index acd2541..0172950 100644 --- a/Tests/HTTPTypesTests/HTTPTypesTests.swift +++ b/Tests/HTTPTypesTests/HTTPTypesTests.swift @@ -123,6 +123,44 @@ extension HTTPField.Name { #expect(fields1 != fields6) } + @Test func hashMatchesEqualityForSameOrder() { + let fields1: HTTPFields = [ + .acceptEncoding: "br", + .acceptEncoding: "gzip", + .accept: "*/*", + ] + + let fields2: HTTPFields = [ + .acceptEncoding: "br", + .acceptEncoding: "gzip", + .accept: "*/*", + ] + #expect(fields1 == fields2) + #expect(fields1.hashValue == fields2.hashValue) + } + + @Test func hashMatchesEqualityForDifferentOrder() { + let fields1: HTTPFields = [ + .acceptEncoding: "br", + .acceptEncoding: "gzip", + .accept: "*/*", + ] + + // Fields with differently named fields in a different order are equal, since the + // relative order of same-named fields is preserved. + let fields2: HTTPFields = [ + .acceptEncoding: "br", + .accept: "*/*", + .acceptEncoding: "gzip", + ] + #expect(fields1 == fields2) + + // Equal values must therefore hash equally. + withKnownIssue("HTTPFields.hash(into:) is order sensitive while == is not") { + #expect(fields1.hashValue == fields2.hashValue) + } + } + @Test func sendable() { func isSendable(_ value: some Sendable) -> Bool { true } func isSendable(_ value: Any) -> Bool { false }