Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / ios / net / http_response_headers_util_unittest.mm
blob27dfcb899c2aceed273f1a2680d13565d9c8eb96
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.
5 #include "ios/net/http_response_headers_util.h"
7 #import <Foundation/Foundation.h>
9 #include <algorithm>
11 #include "base/mac/scoped_nsobject.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/sys_string_conversions.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace net {
18 // Returns true if all the information in |http_response| is present in
19 // |http_response_headers|.
20 bool AreHeadersEqual(NSHTTPURLResponse* http_response,
21                      HttpResponseHeaders* http_response_headers) {
22   if (!http_response || !http_response_headers)
23     return false;
24   if (http_response.statusCode != http_response_headers->response_code())
25     return false;
26   __block bool all_headers_present = true;
27   [http_response.allHeaderFields
28       enumerateKeysAndObjectsUsingBlock:^(NSString* header_name,
29                                           NSString* header_value, BOOL* stop) {
30         std::string value;
31         http_response_headers->GetNormalizedHeader(
32             base::SysNSStringToUTF8(header_name), &value);
33         all_headers_present = (value == base::SysNSStringToUTF8(header_value));
34         *stop = !all_headers_present;
35       }];
36   return all_headers_present;
39 // Tests that HttpResponseHeaders created from NSHTTPURLResponses successfully
40 // copy over the status code and the header names and values.
41 TEST(HttpResponseHeadersUtilTest, CreateHeadersFromNSHTTPURLResponse) {
42   base::scoped_nsobject<NSHTTPURLResponse> http_response(
43       [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"test.com"]
44                                   statusCode:200
45                                  HTTPVersion:@"HTTP/1.1"
46                                 headerFields:@{
47                                   @"headerName1" : @"headerValue1",
48                                   @"headerName2" : @"headerValue2",
49                                   @"headerName3" : @"headerValue3",
50                                 }]);
51   scoped_refptr<HttpResponseHeaders> http_response_headers =
52       CreateHeadersFromNSHTTPURLResponse(http_response);
53   EXPECT_TRUE(
54       AreHeadersEqual(http_response.get(), http_response_headers.get()));
57 }  // namespace net.