FIRAppCheckBackoffWrapperFake.m 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright 2021 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "SharedTestUtilities/AppCheckBackoffWrapperFake/FIRAppCheckBackoffWrapperFake.h"
  17. #if __has_include(<FBLPromises/FBLPromises.h>)
  18. #import <FBLPromises/FBLPromises.h>
  19. #else
  20. #import "FBLPromises.h"
  21. #endif
  22. NS_ASSUME_NONNULL_BEGIN
  23. @implementation FIRAppCheckBackoffWrapperFake
  24. - (FBLPromise *)applyBackoffToOperation:(FIRAppCheckBackoffOperationProvider)operationProvider
  25. errorHandler:(FIRAppCheckBackoffErrorHandler)errorHandler {
  26. [self.backoffExpectation fulfill];
  27. if (self.isNextOperationAllowed) {
  28. return operationProvider()
  29. .then(^id(id value) {
  30. self->_operationResult = value;
  31. self->_operationError = nil;
  32. return value;
  33. })
  34. .recover(^id(NSError *error) {
  35. self->_operationError = error;
  36. self->_operationResult = nil;
  37. errorHandler(error);
  38. return error;
  39. });
  40. } else {
  41. FBLPromise *rejectedPromise = [FBLPromise pendingPromise];
  42. [rejectedPromise reject:self.backoffError];
  43. return rejectedPromise;
  44. }
  45. }
  46. - (FIRAppCheckBackoffErrorHandler)defaultAppCheckProviderErrorHandler {
  47. if (_defaultErrorHandler) {
  48. return _defaultErrorHandler;
  49. }
  50. return ^FIRAppCheckBackoffType(NSError *error) {
  51. return FIRAppCheckBackoffTypeNone;
  52. };
  53. }
  54. - (NSError *)backoffError {
  55. return [NSError errorWithDomain:@"FIRAppCheckBackoffWrapperFake.backoff" code:-1 userInfo:nil];
  56. }
  57. @end
  58. NS_ASSUME_NONNULL_END