FIRFakeBackendRPCIssuer.m 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 "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)asyncPostToURL:(NSURL *)URL
  28. body:(NSData *)body
  29. contentType:(NSString *)contentType
  30. completionHandler:(FIRAuthBackendRPCIssuerCompletionHandler)handler {
  31. _requestURL = [URL copy];
  32. _requestData = body;
  33. NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:body options:0 error:nil];
  34. _decodedRequest = JSON;
  35. _contentType = contentType;
  36. _handler = handler;
  37. }
  38. - (void)respondWithData:(NSData *)data error:(NSError *)error {
  39. NSAssert(_handler, @"There is no pending RPC request.");
  40. NSAssert(data || error, @"At least one of: data or error should be been non-nil.");
  41. FIRAuthBackendRPCIssuerCompletionHandler handler = _handler;
  42. _handler = nil;
  43. handler(data, error);
  44. }
  45. - (NSData *)respondWithServerErrorMessage:(NSString *)errorMessage error:(NSError *)error {
  46. return [self respondWithJSON:@{ @"error" : @{ @"message" : errorMessage } } error:error];
  47. }
  48. - (NSData *)respondWithServerErrorMessage:(NSString *)errorMessage {
  49. NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:nil];
  50. return [self respondWithServerErrorMessage:errorMessage error:error];
  51. }
  52. - (NSData *)respondWithJSON:(NSDictionary *)JSON error:(NSError *)error {
  53. NSError *JSONEncodingError;
  54. NSData *data;
  55. if (JSON) {
  56. data = [NSJSONSerialization dataWithJSONObject:JSON
  57. options:NSJSONWritingPrettyPrinted
  58. error:&JSONEncodingError];
  59. }
  60. NSAssert(!JSONEncodingError, @"An error occurred encoding the JSON response.");
  61. [self respondWithData:data error:error];
  62. return data;
  63. }
  64. - (NSData *)respondWithJSONError:(NSDictionary *)JSONError {
  65. return [self respondWithJSON:JSONError
  66. error:[NSError errorWithDomain:kFakeErrorDomain code:0 userInfo:nil]];
  67. }
  68. - (NSData *)respondWithError:(NSError *)error {
  69. return [self respondWithJSON:nil error:error];
  70. }
  71. - (NSData *)respondWithJSON:(NSDictionary *)JSON {
  72. return [self respondWithJSON:JSON error:nil];
  73. }
  74. @end