GIDFakeFetcher.m 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2021 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 GTMAppAuth;
  15. #import "GoogleSignIn/Tests/Unit/GIDFakeFetcher.h"
  16. typedef void (^FetchCompletionHandler)(NSData *, NSError *);
  17. @implementation GIDFakeFetcher {
  18. FetchCompletionHandler _handler;
  19. NSURL *_requestURL;
  20. }
  21. - (instancetype)initWithRequest:(NSURLRequest *)request {
  22. self = [super initWithRequest:request configuration:nil];
  23. if (self) {
  24. _requestURL = [[request URL] copy];
  25. }
  26. return self;
  27. }
  28. #pragma clang diagnostic push
  29. #pragma clang diagnostic ignored "-Wdeprecated"
  30. - (instancetype)initWithRequest:(NSURLRequest *)request
  31. authorizer:(id<GTMFetcherAuthorizationProtocol>)authorizer {
  32. #pragma clang diagnostic pop
  33. self = [self initWithRequest:request];
  34. if (self) {
  35. self.authorizer = authorizer;
  36. }
  37. return self;
  38. }
  39. - (void)beginFetchWithDelegate:(id)delegate didFinishSelector:(SEL)finishedSEL {
  40. [NSException raise:@"NotImplementedException" format:@"Implement this method if it is used"];
  41. }
  42. - (void)beginFetchWithCompletionHandler:(FetchCompletionHandler)handler {
  43. if (_handler) {
  44. [NSException raise:NSInvalidArgumentException format:@"Attempted start fetch again"];
  45. }
  46. _handler = [handler copy];
  47. [self authorizeRequestWithCompletion:handler];
  48. }
  49. - (void)authorizeRequestWithCompletion:(FetchCompletionHandler)completion {
  50. NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:self.request.URL];
  51. [self.authorizer authorizeRequest:mutableRequest completionHandler:^(NSError * _Nullable error) {
  52. completion(nil, error);
  53. }];
  54. }
  55. - (NSURL *)requestURL {
  56. return _requestURL;
  57. }
  58. - (void)didFinishWithData:(NSData *)data error:(NSError *)error {
  59. FetchCompletionHandler handler = _handler;
  60. _handler = nil;
  61. handler(data, error);
  62. }
  63. @end