UtilitiesTests.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <XCTest/XCTest.h>
  17. #import "DynamicLinks/Utilities/FDLUtilities.h"
  18. static NSString *const kURLScheme = @"gindeeplinkurl";
  19. @interface FDLUtilitiesTests : XCTestCase
  20. @end
  21. @implementation FDLUtilitiesTests
  22. - (void)testFDLCookieRetrievalURLCreatesCorrectURL {
  23. static NSString *const kCustomScheme = @"customscheme";
  24. static NSString *const kBundleID = @"com.My.Bundle.ID";
  25. NSString *expectedURLString = [NSString stringWithFormat:
  26. @"https://goo.gl/app/_/deeplink?fdl_ios_"
  27. "bundle_id=%@&fdl_ios_url_scheme=%@",
  28. kBundleID, kCustomScheme];
  29. NSURL *url = FIRDLCookieRetrievalURL(kCustomScheme, kBundleID);
  30. XCTAssertEqualObjects(url.absoluteString, expectedURLString);
  31. }
  32. - (void)testFDLURLQueryStringFromDictionaryReturnsEmptyStringWithEmptyDictionary {
  33. NSString *query = FIRDLURLQueryStringFromDictionary(@{});
  34. XCTAssertEqualObjects(query, @"");
  35. }
  36. - (void)testFDLURLQueryStringFromDictionaryReturnsCorrectStringWithSingleKVP {
  37. NSString *key = @"key";
  38. NSString *value = @"value";
  39. NSDictionary *queryDict = @{key : value};
  40. NSString *query = FIRDLURLQueryStringFromDictionary(queryDict);
  41. NSString *expectedQuery = [NSString stringWithFormat:@"?%@=%@", key, value];
  42. XCTAssertEqualObjects(query, expectedQuery);
  43. }
  44. - (void)testFDLURLQueryStringFromDictionary {
  45. NSDictionary *expectedQueryDict = @{
  46. @"key1" : @"va!lue1",
  47. @"key2" : @"val=ue2",
  48. @"key3" : @"val&ue3",
  49. @"key4" : @"valu?e4",
  50. @"key5" : @"val$ue5",
  51. };
  52. NSString *query = FIRDLURLQueryStringFromDictionary(expectedQueryDict);
  53. NSString *prefixToRemove = @"?";
  54. NSString *queryWithoutPrefix = [query substringFromIndex:prefixToRemove.length];
  55. NSDictionary *retrievedQueryDict = FIRDLDictionaryFromQuery(queryWithoutPrefix);
  56. XCTAssertEqualObjects(retrievedQueryDict, expectedQueryDict);
  57. }
  58. - (void)testGINDictionaryFromQueryWithNormalQuery {
  59. NSString *query = @"key1=value1&key2=value2";
  60. NSDictionary *returnedDictionary = FIRDLDictionaryFromQuery(query);
  61. NSDictionary *expectedDictionary = @{@"key1" : @"value1", @"key2" : @"value2"};
  62. XCTAssertEqualObjects(returnedDictionary, expectedDictionary);
  63. }
  64. - (void)testGINDictionaryFromQueryWithQueryMissingValue {
  65. NSString *query = @"key1=value1&key2=";
  66. NSDictionary *returnedDictionary = FIRDLDictionaryFromQuery(query);
  67. NSDictionary *expectedDictionary = @{@"key1" : @"value1", @"key2" : @""};
  68. XCTAssertEqualObjects(returnedDictionary, expectedDictionary);
  69. }
  70. - (void)testGINDictionaryFromQueryWithQueryMissingKey {
  71. NSString *query = @"key1=value1&=value2";
  72. NSDictionary *returnedDictionary = FIRDLDictionaryFromQuery(query);
  73. NSDictionary *expectedDictionary = @{@"key1" : @"value1", @"" : @"value2"};
  74. XCTAssertEqualObjects(returnedDictionary, expectedDictionary);
  75. }
  76. - (void)testGINDictionaryFromQueryWithQueryMissingKeyAndValue {
  77. NSString *query = @"key1=value1&=";
  78. NSDictionary *returnedDictionary = FIRDLDictionaryFromQuery(query);
  79. NSDictionary *expectedDictionary = @{@"key1" : @"value1", @"" : @""};
  80. XCTAssertEqualObjects(returnedDictionary, expectedDictionary);
  81. }
  82. - (void)testGINDictionaryFromQueryWithQueryMissingPairAtTheEnd {
  83. NSString *query = @"key1=value1&";
  84. NSDictionary *returnedDictionary = FIRDLDictionaryFromQuery(query);
  85. NSDictionary *expectedDictionary = @{@"key1" : @"value1"};
  86. XCTAssertEqualObjects(returnedDictionary, expectedDictionary);
  87. }
  88. - (void)testGINDictionaryFromQueryWithQueryMissingPairAtTheBeginning {
  89. NSString *query = @"&key1=value1";
  90. NSDictionary *returnedDictionary = FIRDLDictionaryFromQuery(query);
  91. NSDictionary *expectedDictionary = @{@"key1" : @"value1"};
  92. XCTAssertEqualObjects(returnedDictionary, expectedDictionary);
  93. }
  94. - (void)testGINDictionaryFromQueryWithQueryMissingPairInTheMiddle {
  95. NSString *query = @"key1=value1&&key2=value2";
  96. NSDictionary *returnedDictionary = FIRDLDictionaryFromQuery(query);
  97. NSDictionary *expectedDictionary = @{@"key1" : @"value1", @"key2" : @"value2"};
  98. XCTAssertEqualObjects(returnedDictionary, expectedDictionary);
  99. }
  100. - (void)testDeepLinkURLWithInviteIDDeepLinkStringWeakMatchEndpointCreatesExpectedCustomSchemeURL {
  101. NSString *inviteID = @"3082906yht4i02";
  102. NSString *deepLinkString = @"https://google.com/a%b!c=d";
  103. NSString *encodedDeepLinkString = @"https%3A%2F%2Fgoogle%2Ecom%2Fa%25b%21c%3Dd";
  104. NSString *weakMatchEndpoint = @"IPV6";
  105. NSString *utmSource = @"firebase";
  106. NSString *utmMedium = @"email";
  107. NSString *utmCampaign = @"testCampaign";
  108. NSString *matchType = @"unique";
  109. NSString *expectedURLString =
  110. [NSString stringWithFormat:
  111. @"%@://google/link/?utm_campaign=%@"
  112. @"&deep_link_id=%@&utm_medium=%@&invitation_weakMatchEndpoint=%@"
  113. @"&utm_source=%@&invitation_id=%@&match_type=%@",
  114. kURLScheme, utmCampaign, encodedDeepLinkString, utmMedium, weakMatchEndpoint,
  115. utmSource, inviteID, matchType];
  116. NSURLComponents *expectedURLComponents = [NSURLComponents componentsWithString:expectedURLString];
  117. NSURL *actualURL =
  118. FIRDLDeepLinkURLWithInviteID(inviteID, deepLinkString, utmSource, utmMedium, utmCampaign, NO,
  119. weakMatchEndpoint, nil, kURLScheme, nil);
  120. NSURLComponents *actualURLComponents =
  121. [NSURLComponents componentsWithURL:actualURL resolvingAgainstBaseURL:NO];
  122. // Since the parameters are not guaranteed to be in any specific order, we must compare
  123. // arrays of properties of the URLs rather than the URLs themselves.
  124. // sort both expected/actual arrays to prevent order influencing the test results
  125. NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
  126. NSArray<NSURLQueryItem *> *expectedURLQueryItems =
  127. [expectedURLComponents.queryItems sortedArrayUsingDescriptors:@[ sort ]];
  128. NSArray<NSURLQueryItem *> *actualQueryItems =
  129. [actualURLComponents.queryItems sortedArrayUsingDescriptors:@[ sort ]];
  130. XCTAssertEqualObjects(actualQueryItems, expectedURLQueryItems);
  131. XCTAssertEqualObjects(actualURLComponents.host, expectedURLComponents.host);
  132. }
  133. - (void)testGINOSVersionSupportedReturnsYESWhenCurrentIsGreaterThanMin {
  134. BOOL supported = FIRDLOSVersionSupported(@"8.0.1", @"8.0");
  135. XCTAssertTrue(supported, @"FIRDLOSVersionSupported() returned NO when the OS was supported.");
  136. }
  137. - (void)testGINOSVersionSupportedReturnsYESWhenCurrentIsEqualToMin {
  138. BOOL supported = FIRDLOSVersionSupported(@"8.0", @"8.0");
  139. XCTAssertTrue(supported, @"FIRDLOSVersionSupported() returned NO when the OS was supported.");
  140. }
  141. - (void)testGINOSVersionSupportedReturnsNOWhenCurrentIsLessThanMin {
  142. BOOL supported = FIRDLOSVersionSupported(@"7.1", @"8.1");
  143. XCTAssertFalse(supported,
  144. @"FIRDLOSVersionSupported() returned YES when the OS was not supported.");
  145. }
  146. @end