Skip to content

Commit

Permalink
AppleReceipt.debugDescription: don't pretty-print JSON (#2564)
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoSoto authored May 31, 2023
1 parent fc30034 commit 2276d5c
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Sources/LocalReceiptParsing/BasicTypes/AppleReceipt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ extension AppleReceipt: CustomDebugStringConvertible {

/// swiftlint:disable:next missing_docs
public var debugDescription: String {
return (try? self.prettyPrintedJSON) ?? "<null>"
return (try? self.encodedJSON) ?? "<null>"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,28 @@ extension Encodable {
}
}

/// - Throws: if encoding failed
/// - Returns: `nil` if the encoded `Data` can't be serialized into a `String`.
var encodedJSON: String? {
get throws {
return String(data: try self.jsonEncodedData, encoding: .utf8)
}
}

// MARK: -

var prettyPrintedData: Data {
get throws {
return try JSONEncoder.prettyPrinted.encode(self)
}
}

var jsonEncodedData: Data {
get throws {
return try JSONEncoder.default.encode(self)
}
}

}

extension JSONEncoder {
Expand Down
10 changes: 1 addition & 9 deletions Sources/Networking/HTTPClient/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ private extension HTTPClient {
urlRequest.allHTTPHeaderFields = self.headers(for: request, urlRequest: urlRequest)

do {
urlRequest.httpBody = try request.httpRequest.requestBody?.asData()
urlRequest.httpBody = try request.httpRequest.requestBody?.jsonEncodedData
} catch {
Logger.error(Strings.network.creating_json_error(error: error.localizedDescription))
return nil
Expand Down Expand Up @@ -459,14 +459,6 @@ extension HTTPRequest.Path {

}

private extension Encodable {

func asData() throws -> Data {
return try JSONEncoder.default.encode(self)
}

}

private extension NetworkError {

/// Creates a `NetworkError` from any request `Error`.
Expand Down
2 changes: 1 addition & 1 deletion Tests/UnitTests/Caching/DeviceCacheTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class DeviceCacheTests: TestCase {

expect(self.deviceCache.cachedOfferings) === expectedOfferings
try expect(self.mockUserDefaults.mockValues["com.revenuecat.userdefaults.offerings.user"] as? Data)
== expectedOfferings.response.asJSONEncodedData()
== expectedOfferings.response.jsonEncodedData
}

func testCacheOfferingsInMemory() throws {
Expand Down
12 changes: 6 additions & 6 deletions Tests/UnitTests/Identity/CustomerInfoManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class CustomerInfoManagerTests: BaseCustomerInfoManagerTests {
func testSendCachedCustomerInfoIfAvailableForAppUserIDSendsIfNeverSent() throws {
let info: CustomerInfo = .emptyInfo

let object = try info.asJSONEncodedData()
let object = try info.jsonEncodedData
self.mockDeviceCache.cachedCustomerInfo[Self.appUserID] = object

customerInfoManager.sendCachedCustomerInfoIfAvailable(appUserID: Self.appUserID)
Expand All @@ -210,7 +210,7 @@ class CustomerInfoManagerTests: BaseCustomerInfoManagerTests {
func testSendCachedCustomerInfoIfAvailableForAppUserIDSendsIfDifferent() throws {
let oldInfo: CustomerInfo = .emptyInfo

var object = try oldInfo.asJSONEncodedData()
var object = try oldInfo.jsonEncodedData

mockDeviceCache.cachedCustomerInfo[Self.appUserID] = object

Expand All @@ -226,7 +226,7 @@ class CustomerInfoManagerTests: BaseCustomerInfoManagerTests {
] as [String: Any]
])

object = try newInfo.asJSONEncodedData()
object = try newInfo.jsonEncodedData
mockDeviceCache.cachedCustomerInfo[Self.appUserID] = object

customerInfoManager.sendCachedCustomerInfoIfAvailable(appUserID: Self.appUserID)
Expand All @@ -236,7 +236,7 @@ class CustomerInfoManagerTests: BaseCustomerInfoManagerTests {
func testSendCachedCustomerInfoIfAvailableForAppUserIDSendsOnMainThread() throws {
let oldInfo: CustomerInfo = .emptyInfo

let object = try oldInfo.asJSONEncodedData()
let object = try oldInfo.jsonEncodedData
mockDeviceCache.cachedCustomerInfo[Self.appUserID] = object

customerInfoManager.sendCachedCustomerInfoIfAvailable(appUserID: Self.appUserID)
Expand Down Expand Up @@ -302,7 +302,7 @@ class CustomerInfoManagerTests: BaseCustomerInfoManagerTests {
] as [String: Any]
])

let object = try info.asJSONEncodedData()
let object = try info.jsonEncodedData
self.mockDeviceCache.cachedCustomerInfo[Self.appUserID] = object

let receivedCustomerInfo = try XCTUnwrap(self.customerInfoManager.cachedCustomerInfo(appUserID: Self.appUserID))
Expand Down Expand Up @@ -333,7 +333,7 @@ class CustomerInfoManagerTests: BaseCustomerInfoManagerTests {
] as [String: Any]
])

let object = try info.asJSONEncodedData()
let object = try info.jsonEncodedData
mockDeviceCache.cachedCustomerInfo["firstUser"] = object

let receivedCustomerInfo = customerInfoManager.cachedCustomerInfo(appUserID: "secondUser")
Expand Down
10 changes: 1 addition & 9 deletions Tests/UnitTests/Misc/CustomerInfo+TestExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@
@testable import RevenueCat
import XCTest

extension Encodable {

func asJSONEncodedData() throws -> Data {
return try JSONEncoder.default.encode(self)
}

}

extension CustomerInfo {

/// Initializes the customer with a dictionary
Expand Down Expand Up @@ -56,7 +48,7 @@ extension CustomerInfo {

func asData(withNewSchemaVersion version: Any?) throws -> Data {
var dictionary = try XCTUnwrap(
JSONSerialization.jsonObject(with: try self.asJSONEncodedData()) as? [String: Any]
JSONSerialization.jsonObject(with: try self.jsonEncodedData) as? [String: Any]
)

if let version = version {
Expand Down
4 changes: 2 additions & 2 deletions Tests/UnitTests/Networking/HTTPClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1285,7 +1285,7 @@ final class HTTPClientTests: BaseHTTPClientTests {
func testResponseFromServerUpdatesRequestDate() throws {
let path: HTTPRequest.Path = .mockPath
let mockedResponse = BodyWithDate(data: "test", requestDate: Date().addingTimeInterval(-3000000))
let encodedResponse = try mockedResponse.asJSONEncodedData()
let encodedResponse = try mockedResponse.jsonEncodedData
let requestDate = Date().addingTimeInterval(-100000)

stub(condition: isPath(path)) { _ in
Expand All @@ -1310,7 +1310,7 @@ final class HTTPClientTests: BaseHTTPClientTests {
let path: HTTPRequest.Path = .mockPath
let eTag = "etag"
let mockedResponse = BodyWithDate(data: "test", requestDate: Date().addingTimeInterval(-30000000))
let encodedResponse = try mockedResponse.asJSONEncodedData()
let encodedResponse = try mockedResponse.jsonEncodedData
let requestDate = Date().addingTimeInterval(-1000000)

self.eTagManager.stubResponseEtag(eTag)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ private extension BaseSignatureVerificationHTTPClientTests {
responseHeaders: [
HTTPClient.ResponseHeader.requestDate.rawValue: String(requestDate.millisecondsSince1970)
],
body: try response.asJSONEncodedData(),
body: try response.jsonEncodedData,
verificationResult: verificationResult
)
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/UnitTests/Purchasing/OfferingsManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ extension OfferingsManagerTests {
func testReturnsOfferingsFromDiskCacheIfNetworkRequestWithServerDown() throws {
self.mockDeviceCache.stubbedOfferings = nil
self.mockOfferings.stubbedGetOfferingsCompletionResult = .failure(.networkError(.serverDown()))
self.mockDeviceCache.stubbedCachedOfferingsData = try MockData.anyBackendOfferingsResponse.asJSONEncodedData()
self.mockDeviceCache.stubbedCachedOfferingsData = try MockData.anyBackendOfferingsResponse.jsonEncodedData

let result: Result<Offerings, OfferingsManager.Error>? = waitUntilValue { completed in
self.offeringsManager.offerings(appUserID: MockData.anyAppUserID) { result in
Expand All @@ -441,7 +441,7 @@ extension OfferingsManagerTests {

self.mockDeviceCache.stubbedOfferings = nil
self.mockOfferings.stubbedGetOfferingsCompletionResult = .failure(error)
self.mockDeviceCache.stubbedCachedOfferingsData = try MockData.anyBackendOfferingsResponse.asJSONEncodedData()
self.mockDeviceCache.stubbedCachedOfferingsData = try MockData.anyBackendOfferingsResponse.jsonEncodedData
self.mockOfferingsFactory.nilOfferings = true

let result: Result<Offerings, OfferingsManager.Error>? = waitUntilValue { completed in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class PurchasesConfiguringTests: BasePurchasesTests {
self.systemInfo.stubbedIsApplicationBackgrounded = true

let info = try CustomerInfo(data: Self.emptyCustomerInfoData)
let object = try info.asJSONEncodedData()
let object = try info.jsonEncodedData

self.deviceCache.cachedCustomerInfo[identityManager.currentAppUserID] = object

Expand All @@ -250,7 +250,7 @@ class PurchasesConfiguringTests: BasePurchasesTests {

func testSettingTheDelegateAfterInitializationSendsCachedCustomerInfo() throws {
let info = try CustomerInfo(data: Self.emptyCustomerInfoData)
let object = try info.asJSONEncodedData()
let object = try info.jsonEncodedData

self.deviceCache.cachedCustomerInfo[identityManager.currentAppUserID] = object

Expand All @@ -264,7 +264,7 @@ class PurchasesConfiguringTests: BasePurchasesTests {

func testSettingTheDelegateLaterPastInitializationSendsCachedCustomerInfo() throws {
let info = try CustomerInfo(data: Self.emptyCustomerInfoData)
let object = try info.asJSONEncodedData()
let object = try info.jsonEncodedData

self.deviceCache.cachedCustomerInfo[identityManager.currentAppUserID] = object

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PurchasesGetCustomerInfoTests: BasePurchasesTests {
func testCachedCustomerInfoHasSchemaVersion() throws {
let info = try CustomerInfo(data: Self.emptyCustomerInfoData)

let object = try info.asJSONEncodedData()
let object = try info.jsonEncodedData
self.deviceCache.cachedCustomerInfo[self.identityManager.currentAppUserID] = object

self.setupPurchases()
Expand Down Expand Up @@ -68,7 +68,7 @@ class PurchasesGetCustomerInfoTests: BasePurchasesTests {
func testSendsCachedCustomerInfoToGetter() throws {
let info = try CustomerInfo(data: Self.emptyCustomerInfoData)

self.deviceCache.cachedCustomerInfo[self.identityManager.currentAppUserID] = try info.asJSONEncodedData()
self.deviceCache.cachedCustomerInfo[self.identityManager.currentAppUserID] = try info.jsonEncodedData

self.setupPurchases()

Expand All @@ -83,7 +83,7 @@ class PurchasesGetCustomerInfoTests: BasePurchasesTests {
func testCustomerInfoCompletionBlockCalledExactlyOnceWhenInfoCached() throws {
let info = try CustomerInfo(data: Self.emptyCustomerInfoData)

self.deviceCache.cachedCustomerInfo[self.identityManager.currentAppUserID] = try info.asJSONEncodedData()
self.deviceCache.cachedCustomerInfo[self.identityManager.currentAppUserID] = try info.jsonEncodedData
self.deviceCache.stubbedIsCustomerInfoCacheStale = true

self.setupPurchases()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class PurchasesRestoreTests: BasePurchasesTests {
func testRestoringPurchasesPostsIfReceiptHasTransactionsAndCustomerInfoLoaded() throws {
let info = try CustomerInfo(data: Self.emptyCustomerInfoData)

let object = try info.asJSONEncodedData()
let object = try info.jsonEncodedData
self.deviceCache.cachedCustomerInfo[identityManager.currentAppUserID] = object

self.mockTransactionsManager.stubbedCustomerHasTransactionsCompletionParameter = true
Expand Down Expand Up @@ -260,7 +260,7 @@ class PurchasesRestoreNoSetupTests: BasePurchasesTests {
] as [String: Any]
])

let object = try info.asJSONEncodedData()
let object = try info.jsonEncodedData

self.deviceCache.cachedCustomerInfo[self.identityManager.currentAppUserID] = object

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class PurchasesSyncPurchasesTests: BasePurchasesTests {
] as [String: Any]
])

let object = try info.asJSONEncodedData()
let object = try info.jsonEncodedData
self.deviceCache.cachedCustomerInfo[identityManager.currentAppUserID] = object

self.mockTransactionsManager.stubbedCustomerHasTransactionsCompletionParameter = false
Expand All @@ -65,7 +65,7 @@ class PurchasesSyncPurchasesTests: BasePurchasesTests {
func testSyncPurchasesPostsIfReceiptHasTransactionsAndCustomerInfoLoaded() throws {
let info: CustomerInfo = .emptyInfo

let object = try info.asJSONEncodedData()
let object = try info.jsonEncodedData
self.deviceCache.cachedCustomerInfo[identityManager.currentAppUserID] = object

self.mockTransactionsManager.stubbedCustomerHasTransactionsCompletionParameter = true
Expand Down

0 comments on commit 2276d5c

Please sign in to comment.