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: added test to verify that reading cached data in old format fails gracefully #1438

Merged
merged 1 commit into from
Apr 8, 2022
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
2 changes: 0 additions & 2 deletions Sources/Networking/ETagManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ private extension ETagManager {
return request.url?.absoluteString
}

// TODO: delete old data since this isn't backwards compatible

static let suiteNameBase: String = "revenuecat.etags"
static var suiteName: String {
guard let bundleID = Bundle.main.bundleIdentifier else {
Expand Down
43 changes: 43 additions & 0 deletions Tests/UnitTests/Networking/ETagManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,49 @@ class ETagManagerTests: XCTestCase {

expect(self.mockUserDefaults.mockValues.count) == 0
}

func testReadingETagWithInvalidBodyFormatFails() {
/// The data that `ETagManager` serialized up until version 4.1.
struct OldWrapper: Encodable {

let eTag: String
let statusCode: Int
let responseObject: [String: AnyEncodable]

var asData: Data? {
return try? JSONSerialization.data(withJSONObject: self.asDictionary(),
options: .prettyPrinted)
}

}

let eTag = "the_etag"
let url = urlForTest()
let request = URLRequest(url: url)
let cacheKey = url.absoluteString

let actualResponse = "response".data(using: .utf8)!

let wrapper = OldWrapper(eTag: eTag,
statusCode: HTTPStatusCode.success.rawValue,
responseObject: [
"response": AnyEncodable("cached")
])
self.mockUserDefaults.mockValues[cacheKey] = wrapper.asData

let httpURLResponse = self.httpURLResponseForTest(url: url,
eTag: eTag,
statusCode: .notModified)

let response = eTagManager.httpResultFromCacheOrBackend(with: httpURLResponse,
data: actualResponse,
request: request,
retried: true)
expect(response).toNot(beNil())
expect(response?.statusCode) == .notModified
expect(response?.body) == actualResponse
}

}

private extension ETagManagerTests {
Expand Down