FIRFakeBackendRPCIssuer.m 4.0 KB

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