Skip to content

Commit

Permalink
ETagManager: added test to verify that reading cached data in old f…
Browse files Browse the repository at this point in the history
…ormat fails gracefully (#1438)
  • Loading branch information
NachoSoto committed Apr 8, 2022
1 parent 12a49e8 commit df18302
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
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

0 comments on commit df18302

Please sign in to comment.