Skip to content

FIRDynamicLink: preserve all parameters passed to initializer #2478

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

Merged
merged 7 commits into from
Mar 11, 2019
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
44 changes: 44 additions & 0 deletions Example/DynamicLinks/Tests/FIRDynamicLinksTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#import <FirebaseCore/FIROptions.h>
#import <GoogleUtilities/GULSwizzler.h>
#import "DynamicLinks/FIRDLRetrievalProcessFactory.h"
#import "DynamicLinks/FIRDLRetrievalProcessResult+Private.h"
#import "DynamicLinks/FIRDynamicLink+Private.h"
#import "DynamicLinks/FIRDynamicLinkNetworking+Private.h"
#import "DynamicLinks/FIRDynamicLinks+FirstParty.h"
Expand Down Expand Up @@ -967,6 +968,49 @@ - (void)testCheckForPendingDynamicLinkReturnsImmediatelyIfAlreadyRead {
[mockService stopMocking];
}

- (void)testRetrievalProcessResultURLContainsAllParametersPassedToDynamicLinkInitializer {
NSDictionary<NSString *, NSString *> *linkParameters = @{
@"deep_link_id" : @"https://mmaksym.com/test-app1",
@"match_message" : @"Link is uniquely matched for this device.",
@"match_type" : @"unique",
@"utm_campaign" : @"Maksym M Test",
@"utm_medium" : @"test_medium",
@"utm_source" : @"test_source",
@"a_parameter" : @"a_value"
};

FIRDynamicLink *dynamicLink =
[[FIRDynamicLink alloc] initWithParametersDictionary:linkParameters];
FIRDLRetrievalProcessResult *result =
[[FIRDLRetrievalProcessResult alloc] initWithDynamicLink:dynamicLink
error:nil
message:nil
matchSource:nil];

NSURL *customSchemeURL = [result URLWithCustomURLScheme:@"scheme"];
XCTAssertNotNil(customSchemeURL);

// Validate URL parameters
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:customSchemeURL
resolvingAgainstBaseURL:NO];
XCTAssertNotNil(urlComponents);
XCTAssertEqualObjects(urlComponents.scheme, @"scheme");

NSMutableDictionary<NSString *, NSString *> *notEncodedParameters = [linkParameters mutableCopy];

for (NSURLQueryItem *queryItem in urlComponents.queryItems) {
NSString *expectedValue = notEncodedParameters[queryItem.name];
XCTAssertNotNil(expectedValue, @"Extra parameter encoded: %@ = %@", queryItem.name,
queryItem.value);

XCTAssertEqualObjects(queryItem.value, expectedValue);
[notEncodedParameters removeObjectForKey:queryItem.name];
}

XCTAssertEqual(notEncodedParameters.count, 0, @"The parameters must have been encoded: %@",
notEncodedParameters);
}

- (void)test_multipleRequestsToRetrievePendingDeepLinkShouldNotCrash {
id mockService = OCMPartialMock(self.service);
[[mockService expect] handlePendingDynamicLinkRetrievalFailureWithErrorCode:-1
Expand Down
4 changes: 2 additions & 2 deletions Firebase/DynamicLinks/FIRDynamicLink+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ typedef NS_ENUM(NSUInteger, FIRDynamicLinkMatchConfidence) {

@property(nonatomic, copy, nullable) NSString *matchMessage;

@property(nonatomic, copy, readonly) NSDictionary *parametersDictionary;
@property(nonatomic, copy, readonly) NSDictionary<NSString *, id> *parametersDictionary;

@property(nonatomic, assign, readwrite) FIRDLMatchType matchType;

- (instancetype)initWithParametersDictionary:(NSDictionary *)parametersDictionary;
- (instancetype)initWithParametersDictionary:(NSDictionary<NSString *, id> *)parametersDictionary;

@end

Expand Down
62 changes: 50 additions & 12 deletions Firebase/DynamicLinks/FIRDynamicLink.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,75 @@ - (NSString *)description {
self.minimumAppVersion ?: @"N/A", self.matchMessage];
}

- (instancetype)initWithParametersDictionary:(NSDictionary *)parameters {
- (instancetype)initWithParametersDictionary:(NSDictionary<NSString *, id> *)parameters {
NSParameterAssert(parameters.count > 0);

if (self = [super init]) {
_parametersDictionary = [parameters copy];

NSString *urlString = parameters[kFIRDLParameterDeepLinkIdentifier];
_url = [NSURL URLWithString:urlString];
_inviteId = parameters[kFIRDLParameterInviteId];
_weakMatchEndpoint = parameters[kFIRDLParameterWeakMatchEndpoint];
_minimumAppVersion = parameters[kFIRDLParameterMinimumAppVersion];

if (parameters[kFIRDLParameterMatchType]) {
_matchType = [[self class] matchTypeWithString:parameters[kFIRDLParameterMatchType]];
[self setMatchType:[[self class] matchTypeWithString:parameters[kFIRDLParameterMatchType]]];
} else if (_url || _inviteId) {
// If matchType not present assume unique match for compatibility with server side behavior
// on iOS 8.
_matchType = FIRDLMatchTypeUnique;
[self setMatchType:FIRDLMatchTypeUnique];
}

_matchMessage = parameters[kFIRDLParameterMatchMessage];
}
return self;
}

- (NSDictionary *)parametersDictionary {
NSMutableDictionary *parametersDictionary = [NSMutableDictionary dictionary];
parametersDictionary[kFIRDLParameterInviteId] = _inviteId;
parametersDictionary[kFIRDLParameterDeepLinkIdentifier] = [_url absoluteString];
parametersDictionary[kFIRDLParameterMatchType] = [[self class] stringWithMatchType:_matchType];
parametersDictionary[kFIRDLParameterWeakMatchEndpoint] = _weakMatchEndpoint;
parametersDictionary[kFIRDLParameterMinimumAppVersion] = _minimumAppVersion;
parametersDictionary[kFIRDLParameterMatchMessage] = _matchMessage;
return parametersDictionary;
#pragma mark - Properties

- (void)setUrl:(NSURL *)url {
_url = [url copy];
[self setParametersDictionaryValue:[_url absoluteString]
forKey:kFIRDLParameterDeepLinkIdentifier];
}

- (void)setMinimumAppVersion:(NSString *)minimumAppVersion {
_minimumAppVersion = [minimumAppVersion copy];
[self setParametersDictionaryValue:_minimumAppVersion forKey:kFIRDLParameterMinimumAppVersion];
}

- (void)setInviteId:(NSString *)inviteId {
_inviteId = [inviteId copy];
[self setParametersDictionaryValue:_inviteId forKey:kFIRDLParameterInviteId];
}

- (void)setWeakMatchEndpoint:(NSString *)weakMatchEndpoint {
_weakMatchEndpoint = [weakMatchEndpoint copy];
[self setParametersDictionaryValue:_weakMatchEndpoint forKey:kFIRDLParameterWeakMatchEndpoint];
}

- (void)setMatchType:(FIRDLMatchType)matchType {
_matchType = matchType;
[self setParametersDictionaryValue:[[self class] stringWithMatchType:_matchType]
forKey:kFIRDLParameterMatchType];
}

- (void)setMatchMessage:(NSString *)matchMessage {
_matchMessage = [matchMessage copy];
[self setParametersDictionaryValue:_matchMessage forKey:kFIRDLParameterMatchMessage];
}

- (void)setParametersDictionaryValue:(id)value forKey:(NSString *)key {
NSMutableDictionary<NSString *, id> *parametersDictionary =
[self.parametersDictionary mutableCopy];
if (value == nil) {
[parametersDictionary removeObjectForKey:key];
} else {
parametersDictionary[key] = value;
}

_parametersDictionary = [parametersDictionary copy];
}

- (FIRDynamicLinkMatchConfidence)matchConfidence {
Expand Down