diff --git a/Sources/Hummingbird/HTTP/MediaType.swift b/Sources/Hummingbird/HTTP/MediaType.swift index aeaf587a..01c56510 100644 --- a/Sources/Hummingbird/HTTP/MediaType.swift +++ b/Sources/Hummingbird/HTTP/MediaType.swift @@ -126,7 +126,7 @@ public struct MediaType: Sendable, CustomStringConvertible { /// Return if media type matches the input public func isType(_ type: MediaType) -> Bool { - guard self.type == type.type, + guard self.type ~= type.type, self.subType == type.subType || type.subType == "*" else { return false @@ -154,7 +154,7 @@ public struct MediaType: Sendable, CustomStringConvertible { /// Media type categories public struct Category: Sendable, Equatable, RawRepresentable, CustomStringConvertible { - internal enum Internal: String, Sendable, Equatable { + internal enum Internal: String, Sendable { case application case audio case example @@ -166,15 +166,6 @@ public struct MediaType: Sendable, CustomStringConvertible { case text case video case any - - public static func == (_ lhs: Self, _ rhs: Self) -> Bool { - switch (lhs, rhs) { - case (.any, _), (_, .any): - return true - default: - return lhs.rawValue == rhs.rawValue - } - } } let value: Internal @@ -195,6 +186,15 @@ public struct MediaType: Sendable, CustomStringConvertible { self.value.rawValue } + static func ~= (_ lhs: Self, _ rhs: Self) -> Bool { + switch (lhs.value, rhs.value) { + case (.any, _), (_, .any): + true + default: + lhs.value == rhs.value + } + } + public static var application: Self { .init(value: .application) } public static var audio: Self { .init(value: .audio) } public static var example: Self { .init(value: .example) } diff --git a/Tests/HummingbirdTests/CacheControlTests.swift b/Tests/HummingbirdTests/CacheControlTests.swift new file mode 100644 index 00000000..a1a683d2 --- /dev/null +++ b/Tests/HummingbirdTests/CacheControlTests.swift @@ -0,0 +1,26 @@ +import Hummingbird +import XCTest + +final class CacheControlTests: XCTestCase { + func testCssIsText() { + let cacheControl = CacheControl([ + (MediaType(type: .text), [.noCache, .public]) + ]) + XCTAssertEqual(cacheControl.getCacheControlHeader(for: "test.css"), "no-cache, public") + } + + func testMultipleEntries() { + let cacheControl = CacheControl([ + (MediaType.textCss, [.noStore]), + (MediaType.text, [.noCache, .public]), + ]) + XCTAssertEqual(cacheControl.getCacheControlHeader(for: "test.css"), "no-store") + } + + func testCssIsAny() { + let cacheControl = CacheControl([ + (MediaType(type: .any), [.noCache, .public]) + ]) + XCTAssertEqual(cacheControl.getCacheControlHeader(for: "test.css"), "no-cache, public") + } +}