FIRFakeBackendRPCIssuer.m 3.8 KB

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