FIRFakeBackendRPCIssuer.m 3.1 KB

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