UtilitiesTests.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 "FirebaseDynamicLinks/Sources/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:@"https://goo.gl/app/_/deeplink?fdl_ios_"
  26. "bundle_id=%@&fdl_ios_url_scheme=%@",
  27. kBundleID, kCustomScheme];
  28. NSURL *url = FIRDLCookieRetrievalURL(kCustomScheme, kBundleID);
  29. XCTAssertEqualObjects(url.absoluteString, expectedURLString);
  30. }
  31. - (void)testFDLURLQueryStringFromDictionaryReturnsEmptyStringWithEmptyDictionary {
  32. NSString *query = FIRDLURLQueryStringFromDictionary(@{});
  33. XCTAssertEqualObjects(query, @"");
  34. }
  35. - (void)testFDLURLQueryStringFromDictionaryReturnsCorrectStringWithSingleKVP {
  36. NSString *key = @"key";
  37. NSString *value = @"value";
  38. NSDictionary *queryDict = @{key : value};
  39. NSString *query = FIRDLURLQueryStringFromDictionary(queryDict);
  40. NSString *expectedQuery = [NSString stringWithFormat:@"?%@=%@", key, value];
  41. XCTAssertEqualObjects(query, expectedQuery);
  42. }
  43. - (void)testFDLURLQueryStringFromDictionary {
  44. NSDictionary *expectedQueryDict = @{
  45. @"key1" : @"va!lue1",
  46. @"key2" : @"val=ue2",
  47. @"key3" : @"val&ue3",
  48. @"key4" : @"valu?e4",
  49. @"key5" : @"val$ue5",
  50. };
  51. NSString *query = FIRDLURLQueryStringFromDictionary(expectedQueryDict);
  52. NSString *prefixToRemove = @"?";
  53. NSString *queryWithoutPrefix = [query substringFromIndex:prefixToRemove.length];
  54. NSDictionary *retrievedQueryDict = FIRDLDictionaryFromQuery(queryWithoutPrefix);
  55. XCTAssertEqualObjects(retrievedQueryDict, expectedQueryDict);
  56. }
  57. - (void)testGINDictionaryFromQueryWithNormalQuery {
  58. NSString *query = @"key1=value1&key2=value2";
  59. NSDictionary *returnedDictionary = FIRDLDictionaryFromQuery(query);
  60. NSDictionary *expectedDictionary = @{@"key1" : @"value1", @"key2" : @"value2"};
  61. XCTAssertEqualObjects(returnedDictionary, expectedDictionary);
  62. }
  63. - (void)testGINDictionaryFromQueryWithQueryMissingValue {
  64. NSString *query = @"key1=value1&key2=";
  65. NSDictionary *returnedDictionary = FIRDLDictionaryFromQuery(query);
  66. NSDictionary *expectedDictionary = @{@"key1" : @"value1", @"key2" : @""};
  67. XCTAssertEqualObjects(returnedDictionary, expectedDictionary);
  68. }
  69. - (void)testGINDictionaryFromQueryWithQueryMissingKey {
  70. NSString *query = @"key1=value1&=value2";
  71. NSDictionary *returnedDictionary = FIRDLDictionaryFromQuery(query);
  72. NSDictionary *expectedDictionary = @{@"key1" : @"value1", @"" : @"value2"};
  73. XCTAssertEqualObjects(returnedDictionary, expectedDictionary);
  74. }
  75. - (void)testGINDictionaryFromQueryWithQueryMissingKeyAndValue {
  76. NSString *query = @"key1=value1&=";
  77. NSDictionary *returnedDictionary = FIRDLDictionaryFromQuery(query);
  78. NSDictionary *expectedDictionary = @{@"key1" : @"value1", @"" : @""};
  79. XCTAssertEqualObjects(returnedDictionary, expectedDictionary);
  80. }
  81. - (void)testGINDictionaryFromQueryWithQueryMissingPairAtTheEnd {
  82. NSString *query = @"key1=value1&";
  83. NSDictionary *returnedDictionary = FIRDLDictionaryFromQuery(query);
  84. NSDictionary *expectedDictionary = @{@"key1" : @"value1"};
  85. XCTAssertEqualObjects(returnedDictionary, expectedDictionary);
  86. }
  87. - (void)testGINDictionaryFromQueryWithQueryMissingPairAtTheBeginning {
  88. NSString *query = @"&key1=value1";
  89. NSDictionary *returnedDictionary = FIRDLDictionaryFromQuery(query);
  90. NSDictionary *expectedDictionary = @{@"key1" : @"value1"};
  91. XCTAssertEqualObjects(returnedDictionary, expectedDictionary);
  92. }
  93. - (void)testGINDictionaryFromQueryWithQueryMissingPairInTheMiddle {
  94. NSString *query = @"key1=value1&&key2=value2";
  95. NSDictionary *returnedDictionary = FIRDLDictionaryFromQuery(query);
  96. NSDictionary *expectedDictionary = @{@"key1" : @"value1", @"key2" : @"value2"};
  97. XCTAssertEqualObjects(returnedDictionary, expectedDictionary);
  98. }
  99. - (void)testDeepLinkURLWithInviteIDDeepLinkStringWeakMatchEndpointCreatesExpectedCustomSchemeURL {
  100. NSString *inviteID = @"3082906yht4i02";
  101. NSString *deepLinkString = @"https://google.com/a%b!c=d";
  102. NSString *encodedDeepLinkString = @"https%3A%2F%2Fgoogle%2Ecom%2Fa%25b%21c%3Dd";
  103. NSString *weakMatchEndpoint = @"IPV6";
  104. NSString *utmSource = @"firebase";
  105. NSString *utmMedium = @"email";
  106. NSString *utmCampaign = @"testCampaign";
  107. NSString *matchType = @"unique";
  108. NSString *expectedURLString =
  109. [NSString stringWithFormat:@"%@://google/link/?utm_campaign=%@"
  110. @"&deep_link_id=%@&utm_medium=%@&invitation_weakMatchEndpoint=%@"
  111. @"&utm_source=%@&invitation_id=%@&match_type=%@",
  112. kURLScheme, utmCampaign, encodedDeepLinkString, utmMedium,
  113. weakMatchEndpoint, utmSource, inviteID, matchType];
  114. NSURLComponents *expectedURLComponents = [NSURLComponents componentsWithString:expectedURLString];
  115. NSURL *actualURL =
  116. FIRDLDeepLinkURLWithInviteID(inviteID, deepLinkString, utmSource, utmMedium, utmCampaign, NO,
  117. weakMatchEndpoint, nil, kURLScheme, nil);
  118. NSURLComponents *actualURLComponents = [NSURLComponents componentsWithURL:actualURL
  119. resolvingAgainstBaseURL:NO];
  120. // Since the parameters are not guaranteed to be in any specific order, we must compare
  121. // arrays of properties of the URLs rather than the URLs themselves.
  122. // sort both expected/actual arrays to prevent order influencing the test results
  123. NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
  124. NSArray<NSURLQueryItem *> *expectedURLQueryItems =
  125. [expectedURLComponents.queryItems sortedArrayUsingDescriptors:@[ sort ]];
  126. NSArray<NSURLQueryItem *> *actualQueryItems =
  127. [actualURLComponents.queryItems sortedArrayUsingDescriptors:@[ sort ]];
  128. XCTAssertEqualObjects(actualQueryItems, expectedURLQueryItems);
  129. XCTAssertEqualObjects(actualURLComponents.host, expectedURLComponents.host);
  130. }
  131. - (void)testGINOSVersionSupportedReturnsYESWhenCurrentIsGreaterThanMin {
  132. BOOL supported = FIRDLOSVersionSupported(@"8.0.1", @"8.0");
  133. XCTAssertTrue(supported, @"FIRDLOSVersionSupported() returned NO when the OS was supported.");
  134. }
  135. - (void)testGINOSVersionSupportedReturnsYESWhenCurrentIsEqualToMin {
  136. BOOL supported = FIRDLOSVersionSupported(@"8.0", @"8.0");
  137. XCTAssertTrue(supported, @"FIRDLOSVersionSupported() returned NO when the OS was supported.");
  138. }
  139. - (void)testGINOSVersionSupportedReturnsNOWhenCurrentIsLessThanMin {
  140. BOOL supported = FIRDLOSVersionSupported(@"7.1", @"8.1");
  141. XCTAssertFalse(supported,
  142. @"FIRDLOSVersionSupported() returned YES when the OS was not supported.");
  143. }
  144. @end