http_response_headers_util_unittest.mm 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ios/net/http_response_headers_util.h"
  5. #import <Foundation/Foundation.h>
  6. #include <algorithm>
  7. #include "base/strings/string_number_conversions.h"
  8. #include "base/strings/sys_string_conversions.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. #include "testing/platform_test.h"
  11. #if !defined(__has_feature) || !__has_feature(objc_arc)
  12. #error "This file requires ARC support."
  13. #endif
  14. namespace net {
  15. // Returns true if all the information in |http_response| is present in
  16. // |http_response_headers|.
  17. bool AreHeadersEqual(NSHTTPURLResponse* http_response,
  18. HttpResponseHeaders* http_response_headers) {
  19. if (!http_response || !http_response_headers)
  20. return false;
  21. if (http_response.statusCode != http_response_headers->response_code())
  22. return false;
  23. __block bool all_headers_present = true;
  24. [http_response.allHeaderFields
  25. enumerateKeysAndObjectsUsingBlock:^(NSString* header_name,
  26. NSString* header_value, BOOL* stop) {
  27. std::string value;
  28. http_response_headers->GetNormalizedHeader(
  29. base::SysNSStringToUTF8(header_name), &value);
  30. all_headers_present = (value == base::SysNSStringToUTF8(header_value));
  31. *stop = !all_headers_present;
  32. }];
  33. return all_headers_present;
  34. }
  35. using HttpResponseHeadersUtilTest = PlatformTest;
  36. // Tests that HttpResponseHeaders created from NSHTTPURLResponses successfully
  37. // copy over the status code and the header names and values.
  38. TEST_F(HttpResponseHeadersUtilTest, CreateHeadersFromNSHTTPURLResponse) {
  39. NSHTTPURLResponse* http_response =
  40. [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"test.com"]
  41. statusCode:200
  42. HTTPVersion:@"HTTP/1.1"
  43. headerFields:@{
  44. @"headerName1" : @"headerValue1",
  45. @"headerName2" : @"headerValue2",
  46. @"headerName3" : @"headerValue3",
  47. }];
  48. scoped_refptr<HttpResponseHeaders> http_response_headers =
  49. CreateHeadersFromNSHTTPURLResponse(http_response);
  50. EXPECT_TRUE(AreHeadersEqual(http_response, http_response_headers.get()));
  51. }
  52. } // namespace net.