Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CacheControl tests #680

Merged
merged 8 commits into from
Mar 2, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 11 additions & 11 deletions Sources/Hummingbird/HTTP/MediaType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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) }
Expand Down
26 changes: 26 additions & 0 deletions Tests/HummingbirdTests/CacheControlTests.swift
Original file line number Diff line number Diff line change
@@ -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")
}
}
Loading