FIRFakeBackendRPCIssuer.m 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright 2017 Google
  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 "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
  17. /** @var kFakeErrorDomain
  18. @brief Fake error domain used for testing.
  19. */
  20. static NSString *const kFakeErrorDomain = @"fake domain";
  21. @implementation FIRFakeBackendRPCIssuer {
  22. /** @var _handler
  23. @brief A block we must invoke when @c respondWithError or @c respondWithJSON are called.
  24. */
  25. FIRAuthBackendRPCIssuerCompletionHandler _handler;
  26. }
  27. - (void)asyncPostToURLWithRequestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration
  28. URL:(NSURL *)URL
  29. body:(NSData *)body
  30. contentType:(NSString *)contentType
  31. completionHandler:(FIRAuthBackendRPCIssuerCompletionHandler)handler {
  32. _requestURL = [URL copy];
  33. if (body) {
  34. _requestData = body;
  35. NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:body options:0 error:nil];
  36. _decodedRequest = JSON;
  37. }
  38. _contentType = contentType;
  39. _handler = handler;
  40. }
  41. - (void)respondWithData:(NSData *)data error:(NSError *)error {
  42. NSAssert(_handler, @"There is no pending RPC request.");
  43. NSAssert(data || error, @"At least one of: data or error should be been non-nil.");
  44. FIRAuthBackendRPCIssuerCompletionHandler handler = _handler;
  45. _handler = nil;
  46. handler(data, error);
  47. }
  48. - (NSData *)respondWithServerErrorMessage:(NSString *)errorMessage error:(NSError *)error {
  49. return [self respondWithJSON:@{@"error" : @{@"message" : errorMessage}} error:error];
  50. }
  51. - (NSData *)respondWithServerErrorMessage:(NSString *)errorMessage {
  52. NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:nil];
  53. return [self respondWithServerErrorMessage:errorMessage error:error];
  54. }
  55. - (nullable NSData *)respondWithJSON:(NSDictionary *)JSON error:(NSError *)error {
  56. NSError *JSONEncodingError;
  57. NSData *data;
  58. if (JSON) {
  59. data = [NSJSONSerialization dataWithJSONObject:JSON
  60. options:NSJSONWritingPrettyPrinted
  61. error:&JSONEncodingError];
  62. }
  63. NSAssert(!JSONEncodingError, @"An error occurred encoding the JSON response.");
  64. [self respondWithData:data error:error];
  65. return data;
  66. }
  67. - (NSData *)respondWithJSONError:(NSDictionary *)JSONError {
  68. return [self respondWithJSON:JSONError
  69. error:[NSError errorWithDomain:kFakeErrorDomain code:0 userInfo:nil]];
  70. }
  71. - (NSData *)respondWithError:(NSError *)error {
  72. return [self respondWithJSON:nil error:error];
  73. }
  74. - (NSData *)respondWithJSON:(NSDictionary *)JSON {
  75. return [self respondWithJSON:JSON error:nil];
  76. }
  77. @end