-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add annotation RequestCachePolicy (#59)
- Loading branch information
Showing
5 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
Tests/PostieTests/Configuration/RequestCachePolicyCodingTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |