FIRDynamicLinkNetworkingTests.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <GoogleUtilities/GULSwizzler.h>
  19. #import "DynamicLinks/FIRDynamicLinkNetworking+Private.h"
  20. static NSString *const kAPIKey = @"myfakeapikey";
  21. static NSString *const kClientID = @"myfakeclientid";
  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
  35. clientID:kClientID
  36. URLScheme:kURLScheme];
  37. }
  38. return _service;
  39. }
  40. - (void)testFIRDynamicLinkAPIKeyParameterReturnsCorrectlyFormattedParameterString {
  41. NSString *expectedValue = [NSString stringWithFormat:@"?key=%@", kAPIKey];
  42. NSString *parameter = FIRDynamicLinkAPIKeyParameter(kAPIKey);
  43. XCTAssertEqualObjects(parameter, expectedValue,
  44. @"FIRDynamicLinkAPIKeyParameter() returned incorrect parameter string");
  45. }
  46. - (void)testFIRDynamicLinkAPIKeyParameterReturnsNilParameterStringWhenAPIKeyIsNil {
  47. #pragma clang diagnostic push
  48. #pragma clang diagnostic ignored "-Wnonnull"
  49. NSString *parameter = FIRDynamicLinkAPIKeyParameter(nil);
  50. #pragma clang diagnostic pop
  51. XCTAssertNil(parameter,
  52. @"FIRDynamicLinkAPIKeyParameter() returned non-nil result when API key was nil");
  53. }
  54. - (void)testResolveShortLinkServiceCompletionDoesntCrashWhenNilDataIsRetrieved {
  55. NSURL *url = [NSURL URLWithString:@"https://google.com"];
  56. void (^executeRequestBlock)(id, NSDictionary *, NSString *, FIRNetworkRequestCompletionHandler) =
  57. ^(id p1, NSDictionary *requestBody, NSString *requestURLString,
  58. FIRNetworkRequestCompletionHandler handler) {
  59. handler(nil, nil);
  60. };
  61. SEL executeRequestSelector = @selector(executeOnePlatformRequest:forURL:completionHandler:);
  62. [GULSwizzler swizzleClass:[FIRDynamicLinkNetworking class]
  63. selector:executeRequestSelector
  64. isClassSelector:NO
  65. withBlock:executeRequestBlock];
  66. XCTestExpectation *expectation = [self expectationWithDescription:@"completion called"];
  67. [self.service resolveShortLink:url
  68. completion:^(NSURL *_Nullable url, NSError *_Nullable error) {
  69. [expectation fulfill];
  70. }];
  71. [self waitForExpectationsWithTimeout:kAsyncTestTimout handler:nil];
  72. [GULSwizzler unswizzleClass:[FIRDynamicLinkNetworking class]
  73. selector:executeRequestSelector
  74. isClassSelector:NO];
  75. }
  76. @end