FIRDynamicLinkNetworkingTests.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <OCMock/OCMock.h>
  18. #import "FirebaseDynamicLinks/Sources/FIRDynamicLinkNetworking+Private.h"
  19. #import "GoogleUtilities/MethodSwizzler/Private/GULSwizzler.h"
  20. #import "GoogleUtilities/SwizzlerTestHelpers/GULSwizzler+Unswizzle.h"
  21. static NSString *const kAPIKey = @"myfakeapikey";
  22. static NSString *const kClientID = @"myfakeclientid";
  23. const NSInteger kJSONParsingErrorCode = 3840;
  24. static NSString *const kURLScheme = @"gindeeplinkurl";
  25. static const NSTimeInterval kAsyncTestTimout = 0.5;
  26. @interface FIRDynamicLinkNetworkingTests : XCTestCase
  27. @property(strong, nonatomic) FIRDynamicLinkNetworking *service;
  28. @end
  29. @implementation FIRDynamicLinkNetworkingTests
  30. - (void)tearDown {
  31. self.service = nil;
  32. }
  33. - (FIRDynamicLinkNetworking *)service {
  34. if (!_service) {
  35. _service = [[FIRDynamicLinkNetworking alloc] initWithAPIKey:kAPIKey
  36. clientID:kClientID
  37. URLScheme:kURLScheme];
  38. }
  39. return _service;
  40. }
  41. - (void)testFIRDynamicLinkAPIKeyParameterReturnsCorrectlyFormattedParameterString {
  42. NSString *expectedValue = [NSString stringWithFormat:@"?key=%@", kAPIKey];
  43. NSString *parameter = FIRDynamicLinkAPIKeyParameter(kAPIKey);
  44. XCTAssertEqualObjects(parameter, expectedValue,
  45. @"FIRDynamicLinkAPIKeyParameter() returned incorrect parameter string");
  46. }
  47. - (void)testFIRDynamicLinkAPIKeyParameterReturnsNilParameterStringWhenAPIKeyIsNil {
  48. #pragma clang diagnostic push
  49. #pragma clang diagnostic ignored "-Wnonnull"
  50. NSString *parameter = FIRDynamicLinkAPIKeyParameter(nil);
  51. #pragma clang diagnostic pop
  52. XCTAssertNil(parameter,
  53. @"FIRDynamicLinkAPIKeyParameter() returned non-nil result when API key was nil");
  54. }
  55. - (void)testResolveShortLinkServiceCompletionDoesntCrashWhenNilDataIsRetrieved {
  56. NSURL *url = [NSURL URLWithString:@"https://google.com"];
  57. void (^executeRequestBlock)(id, NSDictionary *, NSString *, FIRNetworkRequestCompletionHandler) =
  58. ^(id p1, NSDictionary *requestBody, NSString *requestURLString,
  59. FIRNetworkRequestCompletionHandler handler) {
  60. handler(nil, nil);
  61. };
  62. SEL executeRequestSelector = @selector(executeOnePlatformRequest:forURL:completionHandler:);
  63. [GULSwizzler swizzleClass:[FIRDynamicLinkNetworking class]
  64. selector:executeRequestSelector
  65. isClassSelector:NO
  66. withBlock:executeRequestBlock];
  67. XCTestExpectation *expectation = [self expectationWithDescription:@"completion called"];
  68. [self.service resolveShortLink:url
  69. FDLSDKVersion:@"1.0.0"
  70. completion:^(NSURL *_Nullable url, NSError *_Nullable error) {
  71. [expectation fulfill];
  72. }];
  73. [self waitForExpectationsWithTimeout:kAsyncTestTimout handler:nil];
  74. [GULSwizzler unswizzleClass:[FIRDynamicLinkNetworking class]
  75. selector:executeRequestSelector
  76. isClassSelector:NO];
  77. }
  78. @end