diff --git a/Sources/Identity/CustomerInfo.swift b/Sources/Identity/CustomerInfo.swift index c194ed9570..ef84c9b6e3 100644 --- a/Sources/Identity/CustomerInfo.swift +++ b/Sources/Identity/CustomerInfo.swift @@ -238,6 +238,8 @@ extension CustomerInfo { /// Creates a copy of this ``CustomerInfo`` modifying only the ``VerificationResult``. func copy(with entitlementVerification: VerificationResult) -> Self { + guard entitlementVerification != self.data.entitlementVerification else { return self } + var copy = self.data copy.entitlementVerification = entitlementVerification return .init(data: copy) diff --git a/Sources/Networking/HTTPClient/HTTPResponse.swift b/Sources/Networking/HTTPClient/HTTPResponse.swift index 5962d9fc0f..9c2ba47453 100644 --- a/Sources/Networking/HTTPClient/HTTPResponse.swift +++ b/Sources/Networking/HTTPClient/HTTPResponse.swift @@ -98,6 +98,8 @@ extension HTTPResponse { } func copy(with newVerificationResult: VerificationResult) -> Self { + guard newVerificationResult != self.verificationResult else { return self } + return .init( statusCode: self.statusCode, responseHeaders: self.responseHeaders, diff --git a/Tests/UnitTests/Networking/HTTPResponseTests.swift b/Tests/UnitTests/Networking/HTTPResponseTests.swift index 278810c671..a863b508d1 100644 --- a/Tests/UnitTests/Networking/HTTPResponseTests.swift +++ b/Tests/UnitTests/Networking/HTTPResponseTests.swift @@ -77,6 +77,51 @@ class HTTPResponseTests: TestCase { expect(response.requestDate).to(beNil()) } + func testCopyWithSameVerificationResult() throws { + self.verifyCopy(of: try Self.sampleResponse.copy(with: .verified), + onlyModifiesEntitlementVerification: .verified) + } + + func testCopyWithVerificationResultVerified() throws { + self.verifyCopy(of: try Self.sampleResponse, + onlyModifiesEntitlementVerification: .verified) + } + + func testCopyWithVerificationResultFailedVerified() throws { + self.verifyCopy(of: try Self.sampleResponse, + onlyModifiesEntitlementVerification: .failed) + } + + func testCopyWithVerificationResultNotRequested() throws { + self.verifyCopy(of: try Self.sampleResponse.copy(with: .verified), + onlyModifiesEntitlementVerification: .notRequested) + } + + // MARK: - + + private func verifyCopy( + of response: HTTPResponse, + onlyModifiesEntitlementVerification newVerification: VerificationResult + ) { + let copy = response.copy(with: newVerification) + expect(copy.verificationResult) == newVerification + expect(copy.statusCode) == response.statusCode + expect(copy.responseHeaders).to(haveCount(response.responseHeaders.count)) + expect(copy.body) == response.body + expect(copy.requestDate) == response.requestDate + } + + private static var sampleResponse: HTTPResponse { + get throws { + return .create( + body: try CustomerInfo.emptyInfo.prettyPrintedData, + headers: [ + "X-Header": "true" + ] + ) + } + } + } private extension HTTPResponse where Body == HTTPEmptyResponseBody { @@ -90,6 +135,18 @@ private extension HTTPResponse where Body == HTTPEmptyResponseBody { } +private extension HTTPResponse where Body == Data { + + static func create(body: Data, headers: HTTPResponse.Headers) -> Self { + return .init(statusCode: .success, + responseHeaders: headers, + body: body, + requestDate: Date(), + verificationResult: .notRequested) + } + +} + // MARK: - HTTPResponseBody class HTTPResponseBodyTests: TestCase { diff --git a/Tests/UnitTests/Purchasing/CustomerInfoTests.swift b/Tests/UnitTests/Purchasing/CustomerInfoTests.swift index 3510b896e6..e7a05954ca 100644 --- a/Tests/UnitTests/Purchasing/CustomerInfoTests.swift +++ b/Tests/UnitTests/Purchasing/CustomerInfoTests.swift @@ -843,6 +843,10 @@ class BasicCustomerInfoTests: TestCase { == Set(["onemonth_freetrial", "twomonth_freetrial", "threemonth_freetrial"]) } + func testCopyWithSameVerificationResult() throws { + expect(self.customerInfo.copy(with: .notRequested)) === self.customerInfo + } + func testCopyWithVerificationResultVerified() throws { self.verifyCopy(of: self.customerInfo, onlyModifiesEntitlementVerification: .verified)