GIDHTTPFetcherTest.m 3.4 KB

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