FIRAuthBackendRPCImplementationTests.m 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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 <XCTest/XCTest.h>
  17. #import "FIRAuthErrorUtils.h"
  18. #import "FIRAuthInternalErrors.h"
  19. #import "FIRAuthBackend.h"
  20. #import "FIRAuthRPCRequest.h"
  21. #import "FIRAuthRPCResponse.h"
  22. #import "FIRFakeBackendRPCIssuer.h"
  23. /** @var kFakeRequestURL
  24. @brief Used as a fake URL for a fake RPC request. We don't test this here, since it's tested
  25. for the specific RPC requests in their various unit tests.
  26. */
  27. static NSString *const kFakeRequestURL = @"https://www.google.com/";
  28. /** @var kFakeErrorDomain
  29. @brief A value to use for fake @c NSErrors.
  30. */
  31. static NSString *const kFakeErrorDomain = @"fakeDomain";
  32. /** @var kFakeErrorCode
  33. @brief A value to use for fake @c NSErrors.
  34. */
  35. static const NSUInteger kFakeErrorCode = -1;
  36. /** @var kUnknownServerErrorMessage
  37. @brief A value to use for fake server errors with an unknown message.
  38. */
  39. static NSString *const kUnknownServerErrorMessage = @"UNKNOWN_MESSAGE";
  40. /** @var kErrorMessageCaptchaRequired
  41. @brief The error message in JSON responses from the server for CAPTCHA required.
  42. */
  43. static NSString *const kErrorMessageCaptchaRequired = @"CAPTCHA_REQUIRED";
  44. /** @var kErrorMessageCaptchaRequiredInvalidPassword
  45. @brief The error message in JSON responses from the server for CAPTCHA required with invalid
  46. password.
  47. */
  48. static NSString *const kErrorMessageCaptchaRequiredInvalidPassword =
  49. @"CAPTCHA_REQUIRED_INVALID_PASSWORD";
  50. /** @var kErrorMessageCaptchaCheckFailed
  51. @brief The error message in JSON responses from the server for CAPTCHA check failed.
  52. */
  53. static NSString *const kErrorMessageCaptchaCheckFailed = @"CAPTCHA_CHECK_FAILED";
  54. /** @var kErrorMessageEmailExists
  55. @brief The error message in JSON responses from the server for user's email already exists.
  56. */
  57. static NSString *const kErrorMessageEmailExists = @"EMAIL_EXISTS";
  58. /** @var kErrorMessageKey
  59. @brief The key for the error message in an error response.
  60. */
  61. static NSString *const kErrorMessageKey = @"message";
  62. /** @var kTestKey
  63. @brief A key to use for a successful response dictionary.
  64. */
  65. static NSString *const kTestKey = @"TestKey";
  66. /** @var kUserDisabledErrorMessage
  67. @brief This is the base error message the server will respond with if the user's account has
  68. been disabled.
  69. */
  70. static NSString *const kUserDisabledErrorMessage = @"USER_DISABLED";
  71. /** @var kFakeUserDisabledCustomErrorMessage
  72. @brief This is a fake custom error message the server can respond with if the user's account has
  73. been disabled.
  74. */
  75. static NSString *const kFakeUserDisabledCustomErrorMessage = @"The user has been disabled.";
  76. /** @var kServerErrorDetailMarker
  77. @brief This marker indicates that the server error message contains a detail error message which
  78. should be used instead of the hardcoded client error message.
  79. */
  80. static NSString *const kServerErrorDetailMarker = @" : ";
  81. /** @var kTestValue
  82. @brief A value to use for a successful response dictionary.
  83. */
  84. static NSString *const kTestValue = @"TestValue";
  85. /** @class FIRAuthBackendRPCImplementation
  86. @brief Exposes an otherwise private class to these tests. See the real implementation for
  87. documentation.
  88. */
  89. @interface FIRAuthBackendRPCImplementation : NSObject <FIRAuthBackendImplementation>
  90. /** @fn postWithRequest:response:callback:
  91. @brief Calls the RPC using HTTP POST.
  92. @remarks Possible error responses:
  93. @see FIRAuthInternalErrorCodeRPCRequestEncodingError
  94. @see FIRAuthInternalErrorCodeJSONSerializationError
  95. @see FIRAuthInternalErrorCodeNetworkError
  96. @see FIRAuthInternalErrorCodeUnexpectedErrorResponse
  97. @see FIRAuthInternalErrorCodeUnexpectedResponse
  98. @see FIRAuthInternalErrorCodeRPCResponseDecodingError
  99. @param request The request.
  100. @param response The empty response to be filled.
  101. @param callback The callback for both success and failure.
  102. */
  103. - (void)postWithRequest:(id<FIRAuthRPCRequest>)request
  104. response:(id<FIRAuthRPCResponse>)response
  105. callback:(void (^)(NSError *error))callback;
  106. @end
  107. /** @extension FIRAuthBackend
  108. @brief This class extension exposes the otherwise private @c implementation method. We use this
  109. here to directly call the @c postWithRequest:response:callback: method of
  110. @c FIRAuthBackendRPCImplementation in some of the tests.
  111. */
  112. @interface FIRAuthBackend ()
  113. /** @fn implementation
  114. @brief Exposes the otherwise private @c implementation method. We use this here to directly call
  115. the @c postWithRequest:response:callback: method of @c FIRAuthBackendRPCImplementation in
  116. some of the tests.
  117. */
  118. + (FIRAuthBackendRPCImplementation *)implementation;
  119. @end
  120. /** @class FIRFakeRequest
  121. @brief Allows us to fake a request with deterministic request bodies and encoding errors
  122. returned from the @c FIRAuthRPCRequest-specified @c unencodedHTTPRequestBodyWithError:
  123. method.
  124. */
  125. @interface FIRFakeRequest : NSObject <FIRAuthRPCRequest>
  126. /** @fn fakeRequest
  127. @brief A "normal" request which returns an encodable request object with no error.
  128. */
  129. + (nullable instancetype)fakeRequest;
  130. /** @fn fakeRequestWithEncodingError
  131. @brief A request which returns a fake error during the encoding process.
  132. */
  133. + (nullable instancetype)fakeRequestWithEncodingError:(NSError *)error;
  134. /** @fn fakeRequestWithUnserializableRequestBody
  135. @brief A request which returns a request object which can not be properly serialized by
  136. @c NSJSONSerialization.
  137. */
  138. + (nullable instancetype)fakeRequestWithUnserializableRequestBody;
  139. /** @fn fakeRequestWithNoBody
  140. @brief A request which returns a nil request body but no error.
  141. */
  142. + (nullable instancetype)fakeRequestWithNoBody;
  143. /** @fn init
  144. @brief Please use initWithRequestBody:encodingError:
  145. */
  146. - (nullable instancetype)init NS_UNAVAILABLE;
  147. /** @fn initWithRequestBody:encodingError:
  148. @brief Designated initializer.
  149. @param requestBody The fake request body to return when @c unencodedHTTPRequestBodyWithError: is
  150. invoked.
  151. @param encodingError The fake error to return when @c unencodedHTTPRequestBodyWithError is
  152. invoked.
  153. */
  154. - (nullable instancetype)initWithRequestBody:(nullable id)requestBody
  155. encodingError:(nullable NSError *)encodingError
  156. NS_DESIGNATED_INITIALIZER;
  157. @end
  158. @implementation FIRFakeRequest {
  159. /** @var _requestBody
  160. @brief The fake request body object we will return when @c unencodedHTTPRequestBodyWithError:
  161. is invoked.
  162. */
  163. id _Nullable _requestBody;
  164. /** @var _requestEncodingError
  165. @brief The fake error object we will return when @c unencodedHTTPRequestBodyWithError:
  166. is invoked.
  167. */
  168. NSError *_Nullable _requestEncodingError;
  169. }
  170. + (nullable instancetype)fakeRequest {
  171. return [[self alloc] initWithRequestBody:@{ } encodingError:nil];
  172. }
  173. + (nullable instancetype)fakeRequestWithEncodingError:(NSError *)error {
  174. return [[self alloc] initWithRequestBody:nil encodingError:error];
  175. }
  176. + (nullable instancetype)fakeRequestWithUnserializableRequestBody {
  177. return [[self alloc] initWithRequestBody:@{ @"unencodableValue" : self } encodingError:nil];
  178. }
  179. + (nullable instancetype)fakeRequestWithNoBody {
  180. return [[self alloc] initWithRequestBody:nil encodingError:nil];
  181. }
  182. - (nullable instancetype)initWithRequestBody:(nullable id)requestBody
  183. encodingError:(nullable NSError *)encodingError {
  184. self = [super init];
  185. if (self) {
  186. _requestBody = requestBody;
  187. _requestEncodingError = encodingError;
  188. }
  189. return self;
  190. }
  191. - (NSURL *)requestURL {
  192. return [NSURL URLWithString:kFakeRequestURL];
  193. }
  194. - (nullable id)unencodedHTTPRequestBodyWithError:(NSError *_Nullable *_Nullable)error {
  195. if (error) {
  196. *error = _requestEncodingError;
  197. }
  198. return _requestBody;
  199. }
  200. @end
  201. /** @class FIRFakeResponse
  202. @brief Allows us to inspect the dictionaries received by @c FIRAuthRPCResponse classes, and
  203. provide deterministic responses to the @c setWithDictionary:error:
  204. methods.
  205. */
  206. @interface FIRFakeResponse : NSObject <FIRAuthRPCResponse>
  207. /** @property receivedDictionary
  208. @brief The dictionary passed to the @c setWithDictionary:error: method.
  209. */
  210. @property(nonatomic, strong, readonly, nullable) NSDictionary *receivedDictionary;
  211. /** @fn fakeResponse
  212. @brief A "normal" sucessful response (no error, no expected kind.)
  213. */
  214. + (nullable instancetype)fakeResponse;
  215. /** @fn fakeResponseWithDecodingError
  216. @brief A response which returns a fake error during the decoding process.
  217. */
  218. + (nullable instancetype)fakeResponseWithDecodingError;
  219. /** @fn init
  220. @brief Please use initWithDecodingError:
  221. */
  222. - (nullable instancetype)init NS_UNAVAILABLE;
  223. - (nullable instancetype)initWithDecodingError:(nullable NSError *)decodingError
  224. NS_DESIGNATED_INITIALIZER;
  225. @end
  226. @implementation FIRFakeResponse {
  227. /** @var _responseDecodingError
  228. @brief The value to return for an error when the @c setWithDictionary:error: method is
  229. invoked.
  230. */
  231. NSError *_Nullable _responseDecodingError;
  232. }
  233. + (nullable instancetype)fakeResponse {
  234. return [[self alloc] initWithDecodingError:nil];
  235. }
  236. + (nullable instancetype)fakeResponseWithDecodingError {
  237. NSError *decodingError = [FIRAuthErrorUtils unexpectedErrorResponseWithDeserializedResponse:self];
  238. return [[self alloc] initWithDecodingError:decodingError];
  239. }
  240. - (nullable instancetype)initWithDecodingError:(nullable NSError *)decodingError {
  241. self = [super init];
  242. if (self) {
  243. _responseDecodingError = decodingError;
  244. }
  245. return self;
  246. }
  247. - (BOOL)setWithDictionary:(NSDictionary *)dictionary
  248. error:(NSError *_Nullable *_Nullable)error {
  249. if (_responseDecodingError) {
  250. if (error) {
  251. *error = _responseDecodingError;
  252. }
  253. return NO;
  254. }
  255. _receivedDictionary = dictionary;
  256. return YES;
  257. }
  258. @end
  259. /** @class FIRAuthBackendRPCImplementationTests
  260. @brief This set of unit tests is designed primarily to test the possible outcomes of the
  261. @c FIRAuthBackendRPCImplementation.postWithRequest:response:callback: method.
  262. */
  263. @interface FIRAuthBackendRPCImplementationTests : XCTestCase
  264. @end
  265. @implementation FIRAuthBackendRPCImplementationTests {
  266. /** @var _RPCIssuer
  267. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  268. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  269. */
  270. FIRFakeBackendRPCIssuer *_RPCIssuer;
  271. /** @var _RPCImplementation
  272. @brief This backend RPC implementation is used to make fake network requests for each test in
  273. the suite.
  274. */
  275. FIRAuthBackendRPCImplementation *_RPCImplementation;
  276. }
  277. - (void)setUp {
  278. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  279. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  280. _RPCIssuer = RPCIssuer;
  281. _RPCImplementation = [FIRAuthBackend implementation];
  282. }
  283. - (void)tearDown {
  284. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  285. _RPCIssuer = nil;
  286. _RPCImplementation = nil;
  287. }
  288. /** @fn testRequestEncodingError
  289. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  290. request passed returns an error during it's unencodedHTTPRequestBodyWithError: method.
  291. The error returned should be delivered to the caller without any change.
  292. */
  293. - (void)testRequestEncodingError {
  294. NSError *encodingError =
  295. [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:@{ }];
  296. FIRFakeRequest *request = [FIRFakeRequest fakeRequestWithEncodingError:encodingError];
  297. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  298. __block NSError *callbackError;
  299. __block BOOL callbackInvoked;
  300. [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
  301. callbackInvoked = YES;
  302. callbackError = error;
  303. }];
  304. // There is no need to call [_RPCIssuer respondWithError:...] in this test because a request
  305. // should never have been tried - and we we know that's the case when we test @c callbackInvoked.
  306. XCTAssert(callbackInvoked);
  307. XCTAssertNotNil(callbackError);
  308. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  309. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);
  310. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  311. XCTAssertNotNil(underlyingError);
  312. XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  313. XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeRPCRequestEncodingError);
  314. NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  315. XCTAssertNotNil(underlyingUnderlyingError);
  316. XCTAssertEqualObjects(underlyingUnderlyingError.domain, kFakeErrorDomain);
  317. XCTAssertEqual(underlyingUnderlyingError.code, kFakeErrorCode);
  318. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  319. XCTAssertNil(deserializedResponse);
  320. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  321. XCTAssertNil(dataResponse);
  322. }
  323. /** @fn testBodyDataSerializationError
  324. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  325. request returns an object which isn't serializable by @c NSJSONSerialization.
  326. The error from @c NSJSONSerialization should be returned as the underlyingError for an
  327. @c NSError with the code @c FIRAuthErrorCodeJSONSerializationError.
  328. */
  329. - (void)testBodyDataSerializationError {
  330. FIRFakeRequest *request = [FIRFakeRequest fakeRequestWithUnserializableRequestBody];
  331. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  332. __block NSError *callbackError;
  333. __block BOOL callbackInvoked;
  334. [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
  335. callbackInvoked = YES;
  336. callbackError = error;
  337. }];
  338. // There is no need to call [_RPCIssuer respondWithError:...] in this test because a request
  339. // should never have been tried - and we we know that's the case when we test @c callbackInvoked.
  340. XCTAssert(callbackInvoked);
  341. XCTAssertNotNil(callbackError);
  342. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  343. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);
  344. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  345. XCTAssertNotNil(underlyingError);
  346. XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  347. XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeJSONSerializationError);
  348. NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  349. XCTAssertNil(underlyingUnderlyingError);
  350. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  351. XCTAssertNil(deserializedResponse);
  352. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  353. XCTAssertNil(dataResponse);
  354. }
  355. /** @fn testNetworkError
  356. @brief This test checks to make sure a network error is properly wrapped and forwarded with the
  357. correct code (FIRAuthErrorCodeNetworkError).
  358. */
  359. - (void)testNetworkError {
  360. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  361. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  362. __block NSError *callbackError;
  363. __block BOOL callbackInvoked;
  364. [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
  365. callbackInvoked = YES;
  366. callbackError = error;
  367. }];
  368. // It shouldn't matter what the error domain/code/userInfo are, any junk values are suitable. The
  369. // implementation should treat any error with no response data as a network error.
  370. NSError *responseError = [NSError errorWithDomain:kFakeErrorDomain
  371. code:kFakeErrorCode
  372. userInfo:nil];
  373. [_RPCIssuer respondWithError:responseError];
  374. XCTAssert(callbackInvoked);
  375. XCTAssertNotNil(callbackError);
  376. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  377. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeNetworkError);
  378. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  379. XCTAssertNotNil(underlyingError);
  380. XCTAssertEqualObjects(underlyingError.domain, kFakeErrorDomain);
  381. XCTAssertEqual(underlyingError.code, kFakeErrorCode);
  382. NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  383. XCTAssertNil(underlyingUnderlyingError);
  384. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  385. XCTAssertNil(deserializedResponse);
  386. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  387. XCTAssertNil(dataResponse);
  388. }
  389. /** @fn testUnparsableErrorResponse
  390. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  391. response isn't deserializable by @c NSJSONSerialization and an error
  392. condition (with an associated error response message) was expected. We are expecting to
  393. receive the original network error wrapped in an @c NSError with the code
  394. @c FIRAuthErrorCodeUnexpectedHTTPResponse.
  395. */
  396. - (void)testUnparsableErrorResponse {
  397. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  398. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  399. __block NSError *callbackError;
  400. __block BOOL callbackInvoked;
  401. [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
  402. callbackInvoked = YES;
  403. callbackError = error;
  404. }];
  405. NSData *data =
  406. [@"<html><body>An error occurred.</body></html>" dataUsingEncoding:NSUTF8StringEncoding];
  407. NSError *error =
  408. [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:@{ }];
  409. [_RPCIssuer respondWithData:data error:error];
  410. XCTAssert(callbackInvoked);
  411. XCTAssertNotNil(callbackError);
  412. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  413. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);
  414. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  415. XCTAssertNotNil(underlyingError);
  416. XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  417. XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);
  418. NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  419. XCTAssertNotNil(underlyingUnderlyingError);
  420. XCTAssertEqualObjects(underlyingUnderlyingError.domain, kFakeErrorDomain);
  421. XCTAssertEqual(underlyingUnderlyingError.code, kFakeErrorCode);
  422. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  423. XCTAssertNil(deserializedResponse);
  424. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  425. XCTAssertNotNil(dataResponse);
  426. XCTAssertEqualObjects(dataResponse, data);
  427. }
  428. /** @fn testUnparsableSuccessResponse
  429. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  430. response isn't deserializable by @c NSJSONSerialization and no error
  431. condition was indicated. We are expecting to
  432. receive the @c NSJSONSerialization error wrapped in an @c NSError with the code
  433. @c FIRAuthErrorCodeUnexpectedServerResponse.
  434. */
  435. - (void)testUnparsableSuccessResponse {
  436. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  437. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  438. __block NSError *callbackError;
  439. __block BOOL callbackInvoked;
  440. [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
  441. callbackInvoked = YES;
  442. callbackError = error;
  443. }];
  444. NSData *data =
  445. [@"<xml>Some non-JSON value.</xml>" dataUsingEncoding:NSUTF8StringEncoding];
  446. [_RPCIssuer respondWithData:data error:nil];
  447. XCTAssert(callbackInvoked);
  448. XCTAssertNotNil(callbackError);
  449. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  450. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);
  451. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  452. XCTAssertNotNil(underlyingError);
  453. XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  454. XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedResponse);
  455. NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  456. XCTAssertNotNil(underlyingUnderlyingError);
  457. XCTAssertEqualObjects(underlyingUnderlyingError.domain, NSCocoaErrorDomain);
  458. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  459. XCTAssertNil(deserializedResponse);
  460. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  461. XCTAssertNotNil(dataResponse);
  462. XCTAssertEqualObjects(dataResponse, data);
  463. }
  464. /** @fn testNonDictionaryErrorResponse
  465. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  466. response deserialized by @c NSJSONSerialization is not a dictionary, and an error was
  467. expected. We are expecting to receive an @c NSError with the code
  468. @c FIRAuthErrorCodeUnexpectedErrorServerResponse with the decoded response in the
  469. @c NSError.userInfo dictionary associated with the key
  470. @c FIRAuthErrorUserInfoDecodedResponseKey.
  471. */
  472. - (void)testNonDictionaryErrorResponse {
  473. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  474. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  475. __block NSError *callbackError;
  476. __block BOOL callbackInvoked;
  477. [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
  478. callbackInvoked = YES;
  479. callbackError = error;
  480. }];
  481. // We are responding with a JSON-encoded string value representing an array - which is unexpected.
  482. // It should normally be a dictionary, and we need to check for this sort of thing. Because we can
  483. // successfully decode this value, however, we do return it in the error results. We check for
  484. // this array later in the test.
  485. NSData *data = [@"[]" dataUsingEncoding:NSUTF8StringEncoding];
  486. NSError *error =
  487. [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:@{ }];
  488. [_RPCIssuer respondWithData:data error:error];
  489. XCTAssert(callbackInvoked);
  490. XCTAssertNotNil(callbackError);
  491. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  492. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);
  493. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  494. XCTAssertNotNil(underlyingError);
  495. XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  496. XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);
  497. NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  498. XCTAssertNil(underlyingUnderlyingError);
  499. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  500. XCTAssertNotNil(deserializedResponse);
  501. XCTAssert([deserializedResponse isKindOfClass:[NSArray class]]);
  502. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  503. XCTAssertNil(dataResponse);
  504. }
  505. /** @fn testNonDictionarySuccessResponse
  506. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  507. response deserialized by @c NSJSONSerialization is not a dictionary, and no error was
  508. expected. We are expecting to receive an @c NSError with the code
  509. @c FIRAuthErrorCodeUnexpectedServerResponse with the decoded response in the
  510. @c NSError.userInfo dictionary associated with the key
  511. @c FIRAuthErrorUserInfoDecodedResponseKey.
  512. */
  513. - (void)testNonDictionarySuccessResponse {
  514. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  515. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  516. __block NSError *callbackError;
  517. __block BOOL callbackInvoked;
  518. [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
  519. callbackInvoked = YES;
  520. callbackError = error;
  521. }];
  522. // We are responding with a JSON-encoded string value representing an array - which is unexpected.
  523. // It should normally be a dictionary, and we need to check for this sort of thing. Because we can
  524. // successfully decode this value, however, we do return it in the error results. We check for
  525. // this array later in the test.
  526. NSData *data = [@"[]" dataUsingEncoding:NSUTF8StringEncoding];
  527. [_RPCIssuer respondWithData:data error:nil];
  528. XCTAssert(callbackInvoked);
  529. XCTAssertNotNil(callbackError);
  530. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  531. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);
  532. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  533. XCTAssertNotNil(underlyingError);
  534. XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  535. XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedResponse);
  536. NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  537. XCTAssertNil(underlyingUnderlyingError);
  538. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  539. XCTAssertNotNil(deserializedResponse);
  540. XCTAssert([deserializedResponse isKindOfClass:[NSArray class]]);
  541. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  542. XCTAssertNil(dataResponse);
  543. }
  544. /** @fn testCaptchaRequiredResponse
  545. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  546. we get an error message indicating captcha is required. The backend should not be returning
  547. this error to mobile clients. If it does, we should wrap it in an @c NSError with the code
  548. @c FIRAuthErrorCodeUnexpectedServerResponse with the decoded error message in the
  549. @c NSError.userInfo dictionary associated with the key
  550. @c FIRAuthErrorUserInfoDecodedErrorResponseKey.
  551. */
  552. - (void)testCaptchaRequiredResponse {
  553. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  554. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  555. __block NSError *callbackError;
  556. __block BOOL callbackInvoked;
  557. [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
  558. callbackInvoked = YES;
  559. callbackError = error;
  560. }];
  561. NSError *error =
  562. [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:@{ }];
  563. [_RPCIssuer respondWithServerErrorMessage:kErrorMessageCaptchaRequired error:error];
  564. XCTAssert(callbackInvoked);
  565. XCTAssertNotNil(callbackError);
  566. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  567. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);
  568. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  569. XCTAssertNotNil(underlyingError);
  570. XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  571. XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);
  572. NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  573. XCTAssertNil(underlyingUnderlyingError);
  574. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  575. XCTAssertNotNil(deserializedResponse);
  576. XCTAssert([deserializedResponse isKindOfClass:[NSDictionary class]]);
  577. XCTAssertNotNil(deserializedResponse[@"message"]);
  578. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  579. XCTAssertNil(dataResponse);
  580. }
  581. /** @fn testCaptchaCheckFailedResponse
  582. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  583. we get an error message indicating captcha check failed. The backend should not be returning
  584. this error to mobile clients. If it does, we should wrap it in an @c NSError with the code
  585. @c FIRAuthErrorCodeUnexpectedServerResponse with the decoded error message in the
  586. @c NSError.userInfo dictionary associated with the key
  587. @c FIRAuthErrorUserInfoDecodedErrorResponseKey.
  588. */
  589. - (void)testCaptchaCheckFailedResponse {
  590. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  591. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  592. __block NSError *callbackError;
  593. __block BOOL callbackInvoked;
  594. [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
  595. callbackInvoked = YES;
  596. callbackError = error;
  597. }];
  598. NSError *error =
  599. [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:@{ }];
  600. [_RPCIssuer respondWithServerErrorMessage:kErrorMessageCaptchaCheckFailed error:error];
  601. XCTAssert(callbackInvoked);
  602. XCTAssertNotNil(callbackError);
  603. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  604. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);
  605. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  606. XCTAssertNotNil(underlyingError);
  607. XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  608. XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);
  609. NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  610. XCTAssertNil(underlyingUnderlyingError);
  611. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  612. XCTAssertNotNil(deserializedResponse);
  613. XCTAssert([deserializedResponse isKindOfClass:[NSDictionary class]]);
  614. XCTAssertNotNil(deserializedResponse[@"message"]);
  615. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  616. XCTAssertNil(dataResponse);
  617. }
  618. /** @fn testCaptchaRequiredInvalidPasswordResponse
  619. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  620. we get an error message indicating captcha is required and an invalid password was entered.
  621. The backend should not be returning this error to mobile clients. If it does, we should wrap
  622. it in an @c NSError with the code
  623. @c FIRAuthErrorCodeUnexpectedServerResponse with the decoded error message in the
  624. @c NSError.userInfo dictionary associated with the key
  625. @c FIRAuthErrorUserInfoDecodedErrorResponseKey.
  626. */
  627. - (void)testCaptchaRequiredInvalidPasswordResponse {
  628. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  629. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  630. __block NSError *callbackError;
  631. __block BOOL callbackInvoked;
  632. [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
  633. callbackInvoked = YES;
  634. callbackError = error;
  635. }];
  636. NSError *error =
  637. [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:@{ }];
  638. [_RPCIssuer respondWithServerErrorMessage:kErrorMessageCaptchaRequiredInvalidPassword
  639. error:error];
  640. XCTAssert(callbackInvoked);
  641. XCTAssertNotNil(callbackError);
  642. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  643. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);
  644. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  645. XCTAssertNotNil(underlyingError);
  646. XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  647. XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);
  648. NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  649. XCTAssertNil(underlyingUnderlyingError);
  650. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  651. XCTAssertNotNil(deserializedResponse);
  652. XCTAssert([deserializedResponse isKindOfClass:[NSDictionary class]]);
  653. XCTAssertNotNil(deserializedResponse[@"message"]);
  654. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  655. XCTAssertNil(dataResponse);
  656. }
  657. /** @fn testDecodableErrorResponseWithUnknownMessage
  658. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  659. response deserialized by @c NSJSONSerialization represents a valid error response (and an
  660. error was indicated) but we didn't receive an error message we know about. We are expecting
  661. an @c NSError with the code @c FIRAuthErrorCodeUnexpectedServerResponse with the decoded
  662. error message in the @c NSError.userInfo dictionary associated with the key
  663. @c FIRAuthErrorUserInfoDecodedErrorResponseKey.
  664. */
  665. - (void)testDecodableErrorResponseWithUnknownMessage {
  666. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  667. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  668. __block NSError *callbackError;
  669. __block BOOL callbackInvoked;
  670. [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
  671. callbackInvoked = YES;
  672. callbackError = error;
  673. }];
  674. // We need to return a valid "error" response here, but we are going to intentionally use a bogus
  675. // error message.
  676. NSError *error =
  677. [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:@{ }];
  678. [_RPCIssuer respondWithServerErrorMessage:kUnknownServerErrorMessage error:error];
  679. XCTAssert(callbackInvoked);
  680. XCTAssertNotNil(callbackError);
  681. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  682. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);
  683. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  684. XCTAssertNotNil(underlyingError);
  685. XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  686. XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);
  687. NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  688. XCTAssertNil(underlyingUnderlyingError);
  689. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  690. XCTAssertNotNil(deserializedResponse);
  691. XCTAssert([deserializedResponse isKindOfClass:[NSDictionary class]]);
  692. XCTAssertNotNil(deserializedResponse[@"message"]);
  693. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  694. XCTAssertNil(dataResponse);
  695. }
  696. /** @fn testErrorResponseWithNoErrorMessage
  697. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  698. response deserialized by @c NSJSONSerialization is a dictionary, and an error was indicated,
  699. but no error information was present in the decoded response. We are expecting an @c NSError
  700. with the code @c FIRAuthErrorCodeUnexpectedServerResponse with the decoded
  701. response message in the @c NSError.userInfo dictionary associated with the key
  702. @c FIRAuthErrorUserInfoDecodedResponseKey.
  703. */
  704. - (void)testErrorResponseWithNoErrorMessage {
  705. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  706. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  707. __block NSError *callbackError;
  708. __block BOOL callbackInvoked;
  709. [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
  710. callbackInvoked = YES;
  711. callbackError = error;
  712. }];
  713. NSError *error =
  714. [NSError errorWithDomain:kFakeErrorDomain code:kFakeErrorCode userInfo:@{ }];
  715. [_RPCIssuer respondWithJSON:@{ } error:error];
  716. XCTAssert(callbackInvoked);
  717. XCTAssertNotNil(callbackError);
  718. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  719. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);
  720. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  721. XCTAssertNotNil(underlyingError);
  722. XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  723. XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);
  724. NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
  725. XCTAssertNil(underlyingUnderlyingError);
  726. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  727. XCTAssertNotNil(deserializedResponse);
  728. XCTAssert([deserializedResponse isKindOfClass:[NSDictionary class]]);
  729. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  730. XCTAssertNil(dataResponse);
  731. }
  732. /** @fn testClientErrorResponse
  733. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  734. response contains a client error specified by an error messsage sent from the backend.
  735. */
  736. - (void)testClientErrorResponse {
  737. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  738. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  739. __block NSError *callbackerror;
  740. __block BOOL callBackInvoked;
  741. [_RPCImplementation postWithRequest: request response:response callback:^(NSError *error) {
  742. callBackInvoked = YES;
  743. callbackerror = error;
  744. }];
  745. NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:nil];
  746. NSString *customErrorMessage =[NSString stringWithFormat:@"%@%@%@",
  747. kUserDisabledErrorMessage,
  748. kServerErrorDetailMarker,
  749. kFakeUserDisabledCustomErrorMessage];
  750. [_RPCIssuer respondWithServerErrorMessage:customErrorMessage error:error];
  751. XCTAssertNotNil(callbackerror, @"An error should be returned from callback.");
  752. XCTAssert(callBackInvoked);
  753. XCTAssertEqual(callbackerror.code, FIRAuthErrorCodeUserDisabled);
  754. NSString *customMessage = callbackerror.userInfo[NSLocalizedDescriptionKey];
  755. XCTAssertEqualObjects(customMessage, kFakeUserDisabledCustomErrorMessage);
  756. }
  757. /** @fn testUndecodableSuccessResponse
  758. @brief This test checks the behaviour of @c postWithRequest:response:callback: when the
  759. response isn't decodable by the response class but no error condition was expected. We are
  760. expecting to receive an @c NSError with the code
  761. @c FIRAuthErrorCodeUnexpectedServerResponse and the error from @c setWithDictionary:error:
  762. as the value of the underlyingError.
  763. */
  764. - (void)testUndecodableSuccessResponse {
  765. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  766. FIRFakeResponse *response = [FIRFakeResponse fakeResponseWithDecodingError];
  767. __block NSError *callbackError;
  768. __block BOOL callbackInvoked;
  769. [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
  770. callbackInvoked = YES;
  771. callbackError = error;
  772. }];
  773. // It doesn't matter what we respond with here, as long as it's not an error response. The fake
  774. // response will deterministicly simulate a decoding error regardless of the response value it was
  775. // given.
  776. [_RPCIssuer respondWithJSON:@{ }];
  777. XCTAssert(callbackInvoked);
  778. XCTAssertNotNil(callbackError);
  779. XCTAssertEqualObjects(callbackError.domain, FIRAuthErrorDomain);
  780. XCTAssertEqual(callbackError.code, FIRAuthErrorCodeInternalError);
  781. NSError *underlyingError = callbackError.userInfo[NSUnderlyingErrorKey];
  782. XCTAssertNotNil(underlyingError);
  783. XCTAssertEqualObjects(underlyingError.domain, FIRAuthInternalErrorDomain);
  784. XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeRPCResponseDecodingError);
  785. id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
  786. XCTAssertNotNil(deserializedResponse);
  787. XCTAssert([deserializedResponse isKindOfClass:[NSDictionary class]]);
  788. id dataResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDataKey];
  789. XCTAssertNil(dataResponse);
  790. }
  791. /** @fn testSuccessfulResponse
  792. @brief Tests that a decoded dictionary is handed to the response instance.
  793. */
  794. - (void)testSuccessfulResponse {
  795. FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
  796. FIRFakeResponse *response = [FIRFakeResponse fakeResponse];
  797. __block NSError *callbackError;
  798. __block BOOL callbackInvoked;
  799. [_RPCImplementation postWithRequest:request response:response callback:^(NSError *error) {
  800. callbackInvoked = YES;
  801. callbackError = error;
  802. }];
  803. [_RPCIssuer respondWithJSON:@{ kTestKey : kTestValue }];
  804. XCTAssert(callbackInvoked);
  805. XCTAssertNil(callbackError);
  806. XCTAssertNotNil(response.receivedDictionary);
  807. XCTAssertEqualObjects(response.receivedDictionary[kTestKey], kTestValue);
  808. }
  809. @end