GIDDataFetcherTest.m 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2022 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "GoogleSignIn/Sources/GIDDataFetcher/Implementations/GIDDataFetcher.h"
  15. #import <XCTest/XCTest.h>
  16. #ifdef SWIFT_PACKAGE
  17. @import GTMAppAuth;
  18. #else
  19. #import <GTMAppAuth/GTMAppAuth.h>
  20. #endif
  21. static NSString *const kTestURL = @"https://testURL.com";
  22. static NSString *const kErrorDomain = @"ERROR_DOMAIN";
  23. static NSInteger const kErrorCode = 400;
  24. @interface GIDDataFetcherTest : XCTestCase {
  25. GIDDataFetcher *_dataFetcher;
  26. }
  27. @end
  28. @implementation GIDDataFetcherTest
  29. - (void)setUp {
  30. [super setUp];
  31. _dataFetcher = [[GIDDataFetcher alloc] init];
  32. }
  33. - (void)testFetchData_success {
  34. NSURL *url = [NSURL URLWithString:kTestURL];
  35. XCTestExpectation *expectation =
  36. [self expectationWithDescription:@"Callback called with no error"];
  37. GTMSessionFetcherTestBlock block =
  38. ^(GTMSessionFetcher *fetcherToTest, GTMSessionFetcherTestResponse testResponse) {
  39. NSData *data = [[NSData alloc] init];
  40. testResponse(nil, data, nil);
  41. };
  42. [GTMSessionFetcher setGlobalTestBlock:block];
  43. [_dataFetcher fetchURL:url
  44. withComment:@"Test data fetcher."
  45. completion:^(NSData *data, NSError *error) {
  46. XCTAssertNil(error);
  47. [expectation fulfill];
  48. }];
  49. [self waitForExpectationsWithTimeout:1 handler:nil];
  50. }
  51. - (void)testFetchData_error {
  52. NSURL *url = [NSURL URLWithString:kTestURL];
  53. XCTestExpectation *expectation =
  54. [self expectationWithDescription:@"Callback called with an error"];
  55. GTMSessionFetcherTestBlock block =
  56. ^(GTMSessionFetcher *fetcherToTest, GTMSessionFetcherTestResponse testResponse) {
  57. NSData *data = [[NSData alloc] init];
  58. NSError *error = [self error];
  59. testResponse(nil, data, error);
  60. };
  61. [GTMSessionFetcher setGlobalTestBlock:block];
  62. [_dataFetcher fetchURL:url
  63. withComment:@"Test data fetcher."
  64. completion:^(NSData *data, NSError *error) {
  65. XCTAssertNotNil(error);
  66. XCTAssertEqual(error.code, kErrorCode);
  67. [expectation fulfill];
  68. }];
  69. [self waitForExpectationsWithTimeout:1 handler:nil];
  70. }
  71. #pragma mark - Helpers
  72. - (NSError *)error {
  73. return [NSError errorWithDomain:kErrorDomain code:kErrorCode userInfo:nil];
  74. }
  75. @end