GIDFakeFetcher.m 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 "GoogleSignIn/Tests/Unit/GIDFakeFetcher.h"
  15. typedef void (^FetchCompletionHandler)(NSData *, NSError *);
  16. @implementation GIDFakeFetcher {
  17. FetchCompletionHandler _handler;
  18. NSURL *_requestURL;
  19. }
  20. - (instancetype)initWithRequest:(NSURLRequest *)request {
  21. self = [super initWithRequest:request configuration:nil];
  22. if (self) {
  23. _requestURL = [[request URL] copy];
  24. }
  25. return self;
  26. }
  27. - (void)beginFetchWithDelegate:(id)delegate didFinishSelector:(SEL)finishedSEL {
  28. [NSException raise:@"NotImplementedException" format:@"Implement this method if it is used"];
  29. }
  30. - (void)beginFetchWithCompletionHandler:(FetchCompletionHandler)handler {
  31. if (_handler) {
  32. [NSException raise:NSInvalidArgumentException format:@"Attempted start fetch again"];
  33. }
  34. _handler = [handler copy];
  35. }
  36. - (NSURL *)requestURL {
  37. return _requestURL;
  38. }
  39. - (void)didFinishWithData:(NSData *)data error:(NSError *)error {
  40. FetchCompletionHandler handler = _handler;
  41. _handler = nil;
  42. handler(data, error);
  43. }
  44. @end