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

ETagManager: refactored e-tag creation and tests #2671

Merged
merged 2 commits into from
Jun 21, 2023
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
21 changes: 15 additions & 6 deletions Sources/Networking/HTTPClient/ETagManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ class ETagManager {

}

extension ETagManager {

// Visible for tests
static func cacheKey(for request: URLRequest) -> String? {
return request.url?.absoluteString
}

}

// MARK: - Private

private extension ETagManager {

func shouldUseCachedVersion(responseCode: HTTPStatusCode) -> Bool {
Expand All @@ -141,7 +152,7 @@ private extension ETagManager {

func storedETagAndResponse(for request: URLRequest) -> Response? {
return self.userDefaults.read {
if let cacheKey = self.eTagDefaultCacheKey(for: request),
if let cacheKey = Self.cacheKey(for: request),
let value = $0.object(forKey: cacheKey),
let data = value as? Data {
return try? JSONDecoder.default.decode(Response.self, jsonData: data)
Expand Down Expand Up @@ -172,7 +183,7 @@ private extension ETagManager {
}

func storeIfPossible(_ response: Response, for request: URLRequest) {
if let cacheKey = self.eTagDefaultCacheKey(for: request),
if let cacheKey = Self.cacheKey(for: request),
let dataToStore = response.asData() {
Logger.verbose(Strings.etag.storing_response(request, response))

Expand All @@ -182,10 +193,6 @@ private extension ETagManager {
}
}

func eTagDefaultCacheKey(for request: URLRequest) -> String? {
return request.url?.absoluteString
}

var shouldIgnoreVerificationErrors: Bool {
return !self.verificationMode.isEnabled
}
Expand Down Expand Up @@ -274,6 +281,8 @@ private extension HTTPResponse {
func shouldStore(ignoreVerificationErrors: Bool) -> Bool {
return (
self.statusCode != .notModified &&
// Note that we do want to store 400 responses to help the server
// If the request was wrong, it will also be wrong the next time.
!self.statusCode.isServerError &&
(ignoreVerificationErrors || self.verificationResult != .failed)
)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Networking/HTTPClient/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ extension HTTPClient {
enum RequestHeader: String {

case authorization = "Authorization"
case location = "Location"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was in the wrong enum: it's a response header not request.

case nonce = "X-Nonce"
case eTag = "X-RevenueCat-ETag"
case eTagValidationTime = "X-RC-Last-Refresh-Time"
Expand All @@ -110,6 +109,7 @@ extension HTTPClient {
enum ResponseHeader: String {

case eTag = "X-RevenueCat-ETag"
case location = "Location"
case signature = "X-Signature"
case requestDate = "X-RevenueCat-Request-Time"
case contentType = "Content-Type"
Expand Down
Loading