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

feat: add annotation RequestCachePolicy #59

Merged
merged 1 commit into from
Jan 30, 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
20 changes: 20 additions & 0 deletions Sources/Postie/Configuration/RequestCachePolicy.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Foundation

@propertyWrapper
public struct RequestCachePolicy {

public var wrappedValue: URLRequest.CachePolicy

public init(wrappedValue: URLRequest.CachePolicy = .useProtocolCachePolicy) {
self.wrappedValue = wrappedValue
}
}

// MARK: - Encodable

extension RequestCachePolicy: Encodable {
public func encode(to encoder: any Encoder) throws {
// This method needs to defined because `URLRequest.CachePolicy` does not conform to `Encodable`, but should never be called anyways
preconditionFailure("\(Self.self).encode(to encoder:) should not be called")
}
}
3 changes: 3 additions & 0 deletions Sources/Postie/Encoder/RequestEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ public class RequestEncoder {
.requestHeaderFields(with: encoder.cookies)
// Merge the Cookie headers with the custom headers, where custom headers have precedence
.merging(encoder.headers, uniquingKeysWith: { $1 })
if let cachePolicy = encoder.cachePolicy {
request.cachePolicy = cachePolicy
}
return request
}
}
Expand Down
9 changes: 9 additions & 0 deletions Sources/Postie/Encoder/RequestEncoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal class RequestEncoding: Encoder {
private(set) var headers: [String: String] = [:]
private(set) var pathParameters: [String: RequestPathParameterValue] = [:]
private(set) var cookies: [HTTPCookie] = []
private(set) var cachePolicy: URLRequest.CachePolicy?

init(parent: RequestEncoding? = nil, codingPath: [CodingKey] = []) {
self.parent = parent
Expand Down Expand Up @@ -88,6 +89,14 @@ internal class RequestEncoding: Encoder {
}
}

func setCachePolicy(_ cachePolicy: URLRequest.CachePolicy) {
if let parent = parent {
parent.setCachePolicy(cachePolicy)
} else {
self.cachePolicy = cachePolicy
}
}

// MARK: - Accessors

func resolvedPath() throws -> String {
Expand Down
2 changes: 2 additions & 0 deletions Sources/Postie/Encoder/RequestKeyedEncodingContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class RequestKeyedEncodingContainer<Key>: KeyedEncodingContainerProtocol where K
encoder.setCustomURL(url: customURL)
case let cookies as RequestCookies:
encoder.setCookies(cookies.wrappedValue)
case let cachePolicy as RequestCachePolicy:
encoder.setCachePolicy(cachePolicy.wrappedValue)
default:
// ignore any other values
break
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// swiftlint:disable nesting
@testable import Postie
import XCTest

class RequestCachePolicyCodingTests: XCTestCase {
let baseURL = URL(string: "https://local.url")!

func testEncoding_cachePolicyIsNotDefined_shouldUseDefault() {
// Arrange
struct Request: Postie.Request {
typealias Response = EmptyResponse
}

let request = Request()
let encoder = RequestEncoder(baseURL: baseURL)

// Act
let encoded: URLRequest
do {
encoded = try encoder.encode(request)
} catch {
XCTFail("Failed to encode: " + error.localizedDescription)
return
}

// Assert
XCTAssertEqual(encoded.cachePolicy, .useProtocolCachePolicy)
}

func testEncoding_cachePolicyIsDefined_shouldUseDefault() {
// Arrange
struct Request: Postie.Request {
typealias Response = EmptyResponse

@RequestCachePolicy var cachePolicy
}

let request = Request()
let encoder = RequestEncoder(baseURL: baseURL)

// Act
let encoded: URLRequest
do {
encoded = try encoder.encode(request)
} catch {
XCTFail("Failed to encode: " + error.localizedDescription)
return
}

// Assert
XCTAssertEqual(encoded.cachePolicy, .useProtocolCachePolicy)
}

func testEncoding_cachePolicyIsOverwritten_shouldBeSetInRequest() {
// Arrange
struct Request: Postie.Request {
typealias Response = EmptyResponse

@RequestCachePolicy var cachePolicy = .returnCacheDataDontLoad
}

let request = Request()
let encoder = RequestEncoder(baseURL: baseURL)

// Act
let encoded: URLRequest
do {
encoded = try encoder.encode(request)
} catch {
XCTFail("Failed to encode: " + error.localizedDescription)
return
}

// Assert
XCTAssertEqual(encoded.cachePolicy, .returnCacheDataDontLoad)
}
}

// swiftlint:enable nesting