GIDHTTPFetcherTest.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/GIDHTTPFetcher/Implementations/GIDHTTPFetcher.h"
  15. #import "GoogleSignIn/Tests/Unit/OIDAuthState+Testing.h"
  16. #import <XCTest/XCTest.h>
  17. @import GTMAppAuth;
  18. static NSString *const kTestURL = @"https://testURL.com";
  19. static NSString *const kErrorDomain = @"ERROR_DOMAIN";
  20. static NSInteger const kErrorCode = 400;
  21. @interface GIDHTTPFetcherTest : XCTestCase {
  22. GIDHTTPFetcher *_httpFetcher;
  23. }
  24. @end
  25. @implementation GIDHTTPFetcherTest
  26. - (void)setUp {
  27. [super setUp];
  28. _httpFetcher = [[GIDHTTPFetcher alloc] init];
  29. }
  30. - (void)testFetchData_success {
  31. NSURL *url = [NSURL URLWithString:kTestURL];
  32. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  33. OIDAuthState *authState = [OIDAuthState testInstance];
  34. GTMAuthSession *authorization = [[GTMAuthSession alloc] initWithAuthState:authState];
  35. id<GTMSessionFetcherServiceProtocol> fetcherService = authorization.fetcherService;
  36. GTMSessionFetcherTestBlock block =
  37. ^(GTMSessionFetcher *fetcherToTest, GTMSessionFetcherTestResponse testResponse) {
  38. NSData *data = [[NSData alloc] init];
  39. testResponse(nil, data, nil);
  40. };
  41. [GTMSessionFetcher setGlobalTestBlock:block];
  42. XCTestExpectation *expectation =
  43. [self expectationWithDescription:@"Callback called with no error"];
  44. [_httpFetcher fetchURLRequest:request
  45. withFetcherService:fetcherService
  46. withComment:@"Test data fetcher."
  47. completion:^(NSData *data, NSError *error) {
  48. XCTAssertNil(error);
  49. [expectation fulfill];
  50. }];
  51. [self waitForExpectationsWithTimeout:1 handler:nil];
  52. }
  53. - (void)testFetchData_error {
  54. NSURL *url = [NSURL URLWithString:kTestURL];
  55. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  56. OIDAuthState *authState = [OIDAuthState testInstance];
  57. GTMAuthSession *authorization = [[GTMAuthSession alloc] initWithAuthState:authState];
  58. id<GTMSessionFetcherServiceProtocol> fetcherService = authorization.fetcherService;
  59. GTMSessionFetcherTestBlock block =
  60. ^(GTMSessionFetcher *fetcherToTest, GTMSessionFetcherTestResponse testResponse) {
  61. NSData *data = [[NSData alloc] init];
  62. NSError *error = [self error];
  63. testResponse(nil, data, error);
  64. };
  65. [GTMSessionFetcher setGlobalTestBlock:block];
  66. XCTestExpectation *expectation =
  67. [self expectationWithDescription:@"Callback called with an error"];
  68. [_httpFetcher fetchURLRequest:request
  69. withFetcherService:fetcherService
  70. withComment:@"Test data fetcher."
  71. completion:^(NSData *data, NSError *error) {
  72. XCTAssertNotNil(error);
  73. XCTAssertEqual(error.code, kErrorCode);
  74. [expectation fulfill];
  75. }];
  76. [self waitForExpectationsWithTimeout:1 handler:nil];
  77. }
  78. #pragma mark - Helpers
  79. - (NSError *)error {
  80. return [NSError errorWithDomain:kErrorDomain code:kErrorCode userInfo:nil];
  81. }
  82. @end