Skip to content

Commit 7c12f3f

Browse files
committed
Updated GIDTokenClaimsInternalOptionsTest to test correct expected error.
1 parent 3c02be4 commit 7c12f3f

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

GoogleSignIn/Sources/GIDJSONSerializer/Fake/GIDFakeJSONSerializerImpl.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ NS_ASSUME_NONNULL_BEGIN
2222
@interface GIDFakeJSONSerializerImpl : NSObject <GIDJSONSerializer>
2323

2424
/**
25-
* The error to be returned by `stringWithJSONObject:error:`.
26-
* If `nil`, the method will attempt to perform a real serialization.
25+
* A flag to control whether the serialization method should fail.
26+
*
27+
* If set to `YES`, `stringWithJSONObject:error:` will return `nil` and
28+
* populate the error parameter with a serialization failure error.
29+
* If `NO` (the default), it will attempt a real serialization.
2730
*/
28-
@property(nonatomic, nullable) NSError *errorToReturn;
31+
@property(nonatomic, assign) BOOL shouldFailJSONSerialization;
2932

3033
/** The dictionary passed to the serialization method. */
3134
@property(nonatomic, readonly, nullable) NSDictionary<NSString *, id> *capturedJSONObject;

GoogleSignIn/Sources/GIDJSONSerializer/Fake/GIDFakeJSONSerializerImpl.m

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,28 @@
1616

1717
#import "GoogleSignIn/Sources/GIDJSONSerializer/Fake/GIDFakeJSONSerializerImpl.h"
1818

19+
#import "GoogleSignIn/Sources/GIDJSONSerializer/Implementation/GIDJSONSerializerImpl.h"
20+
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h"
21+
1922
@implementation GIDFakeJSONSerializerImpl
2023

2124
- (nullable NSString *)stringWithJSONObject:(NSDictionary<NSString *, id> *)jsonObject
2225
error:(NSError *_Nullable *_Nullable)error {
2326
_capturedJSONObject = [jsonObject copy];
24-
if (self.errorToReturn) {
27+
28+
// Check the boolean flag to see if we should simulate a failure.
29+
if (self.shouldFailJSONSerialization) {
2530
if (error) {
26-
*error = self.errorToReturn;
31+
*error = [NSError errorWithDomain:kGIDSignInErrorDomain
32+
code:kGIDSignInErrorCodeJSONSerializationFailure
33+
userInfo:@{
34+
NSLocalizedDescriptionKey:kGIDJSONSerializationErrorDescription,
35+
}];
2736
}
2837
return nil;
2938
}
39+
40+
// If not failing, fall back to the real serialization path.
3041
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonObject
3142
options:0
3243
error:error];

GoogleSignIn/Tests/Unit/GIDTokenClaimsInternalOptionsTest.m

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#import <XCTest/XCTest.h>
1616

1717
#import "GoogleSignIn/Sources/GIDJSONSerializer/Fake/GIDFakeJSONSerializerImpl.h"
18+
#import "GoogleSignIn/Sources/GIDJSONSerializer/Implementation/GIDJSONSerializerImpl.h"
1819
#import "GoogleSignIn/Sources/GIDTokenClaimsInternalOptions.h"
1920
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h"
2021
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDTokenClaim.h"
@@ -23,8 +24,10 @@
2324
static NSString *const kNonEssentialAuthTimeExpectedJSON = @"{\"id_token\":{\"auth_time\":{\"essential\":false}}}";
2425

2526
@interface GIDTokenClaimsInternalOptionsTest : XCTestCase
27+
2628
@property(nonatomic) GIDFakeJSONSerializerImpl *jsonSerializerFake;
2729
@property(nonatomic) GIDTokenClaimsInternalOptions *tokenClaimsInternalOptions;
30+
2831
@end
2932

3033
@implementation GIDTokenClaimsInternalOptionsTest
@@ -89,15 +92,22 @@ - (void)testValidatedJSONStringForClaims_WithConflictingClaims_ReturnsNilAndPopu
8992

9093
- (void)testValidatedJSONStringForClaims_WhenSerializationFails_ReturnsNilAndError {
9194
NSSet *claims = [NSSet setWithObject:[GIDTokenClaim authTimeClaim]];
92-
NSError *expectedJSONError = [NSError errorWithDomain:@"com.fake.json" code:-999 userInfo:nil];
93-
_jsonSerializerFake.errorToReturn = expectedJSONError;
95+
NSError *expectedJSONError = [NSError errorWithDomain:kGIDSignInErrorDomain
96+
code:kGIDSignInErrorCodeJSONSerializationFailure
97+
userInfo:@{
98+
NSLocalizedDescriptionKey: kGIDJSONSerializationErrorDescription,
99+
}];
100+
_jsonSerializerFake.shouldFailJSONSerialization = YES;
94101
NSError *actualError;
95-
NSString *result =
96-
[_tokenClaimsInternalOptions validatedJSONStringForClaims:claims error:&actualError];
102+
NSString *result = [_tokenClaimsInternalOptions validatedJSONStringForClaims:claims
103+
error:&actualError];
97104

98105
XCTAssertNil(result, @"The result should be nil when JSON serialization fails.");
99-
XCTAssertEqualObjects(actualError, expectedJSONError,
100-
@"The error from serialization should be passed back to the caller.");
106+
XCTAssertEqualObjects(
107+
actualError,
108+
expectedJSONError,
109+
@"The error from serialization should be passed back to the caller."
110+
);
101111
}
102112

103113
@end

0 commit comments

Comments
 (0)