FIRDynamicLinkNetworkingTests.m 3.4 KB

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