FIRInstallationsHTTPErrorTests.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright 2019 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 "FIRInstallationsHTTPError.h"
  18. #import "FIRKeyedArchivingUtils.h"
  19. @interface FIRInstallationsHTTPErrorTests : XCTestCase
  20. @end
  21. @implementation FIRInstallationsHTTPErrorTests
  22. - (void)testInit {
  23. NSHTTPURLResponse *HTTPResponse = [self createHTTPResponse];
  24. NSData *responseData = [self createResponseData];
  25. FIRInstallationsHTTPError *error =
  26. [[FIRInstallationsHTTPError alloc] initWithHTTPResponse:HTTPResponse data:responseData];
  27. XCTAssertNotNil(error);
  28. XCTAssertEqualObjects(error.HTTPResponse, HTTPResponse);
  29. XCTAssertEqualObjects(error.data, responseData);
  30. }
  31. - (void)testUserInfoContainsResponseData {
  32. NSHTTPURLResponse *HTTPResponse = [self createHTTPResponse];
  33. NSData *responseData = [self createResponseData];
  34. FIRInstallationsHTTPError *error =
  35. [[FIRInstallationsHTTPError alloc] initWithHTTPResponse:HTTPResponse data:responseData];
  36. NSString *failureReason = error.userInfo[NSLocalizedFailureReasonErrorKey];
  37. XCTAssertNotNil(failureReason);
  38. // Validate HTTPResponse content.
  39. XCTAssertTrue([failureReason containsString:HTTPResponse.URL.absoluteString]);
  40. XCTAssertTrue([failureReason containsString:@(HTTPResponse.statusCode).stringValue]);
  41. XCTAssertTrue([failureReason containsString:@(HTTPResponse.statusCode).stringValue]);
  42. XCTAssertTrue([failureReason containsString:@"header1"]);
  43. XCTAssertTrue([failureReason containsString:@"value1"]);
  44. // Validate response data content.
  45. XCTAssertTrue([failureReason containsString:@"invalid request"]);
  46. XCTAssertTrue([failureReason containsString:@"Invalid parameters"]);
  47. }
  48. - (NSHTTPURLResponse *)createHTTPResponse {
  49. return [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"https://example.com"]
  50. statusCode:403
  51. HTTPVersion:@"1.1"
  52. headerFields:@{@"header1" : @"value1"}];
  53. }
  54. - (NSData *)createResponseData {
  55. NSDictionary *response = @{@"invalid request" : @"Invalid parameters"};
  56. NSData *responseData = [NSJSONSerialization dataWithJSONObject:response options:0 error:nil];
  57. XCTAssertNotNil(responseData);
  58. return responseData;
  59. }
  60. - (void)testCopying {
  61. NSHTTPURLResponse *HTTPResponse = [self createHTTPResponse];
  62. NSData *responseData = [self createResponseData];
  63. FIRInstallationsHTTPError *error =
  64. [[FIRInstallationsHTTPError alloc] initWithHTTPResponse:HTTPResponse data:responseData];
  65. FIRInstallationsHTTPError *clone = [error copy];
  66. XCTAssertEqualObjects(error, clone);
  67. XCTAssertEqualObjects(error.HTTPResponse, clone.HTTPResponse);
  68. XCTAssertEqualObjects(error.data, clone.data);
  69. }
  70. - (void)testCoding {
  71. NSHTTPURLResponse *HTTPResponse = [self createHTTPResponse];
  72. NSData *responseData = [self createResponseData];
  73. FIRInstallationsHTTPError *error =
  74. [[FIRInstallationsHTTPError alloc] initWithHTTPResponse:HTTPResponse data:responseData];
  75. NSError *codingError;
  76. NSData *archive = [FIRKeyedArchivingUtils archivedDataWithRootObject:error error:&codingError];
  77. XCTAssertNotNil(archive, @"Error: %@", codingError);
  78. FIRInstallationsHTTPError *unarchivedError =
  79. [FIRKeyedArchivingUtils unarchivedObjectOfClass:[FIRInstallationsHTTPError class]
  80. fromData:archive
  81. error:&codingError];
  82. XCTAssertNotNil(unarchivedError, @"Error: %@", codingError);
  83. // The error will not be equal because two different instances of NSHTTPURLResponse with the same
  84. // content are not equal. Let's check the content below.
  85. XCTAssertEqual(error.HTTPResponse.statusCode, unarchivedError.HTTPResponse.statusCode);
  86. XCTAssertEqualObjects(error.HTTPResponse.allHeaderFields,
  87. unarchivedError.HTTPResponse.allHeaderFields);
  88. XCTAssertEqualObjects(error.HTTPResponse.URL, unarchivedError.HTTPResponse.URL);
  89. XCTAssertEqualObjects(error.data, unarchivedError.data);
  90. }
  91. @end