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>
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"
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)
24 if (http_response.statusCode != http_response_headers->response_code())
26 __block bool all_headers_present = true;
27 [http_response.allHeaderFields
28 enumerateKeysAndObjectsUsingBlock:^(NSString* header_name,
29 NSString* header_value, BOOL* stop) {
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;
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"]
45 HTTPVersion:@"HTTP/1.1"
47 @"headerName1" : @"headerValue1",
48 @"headerName2" : @"headerValue2",
49 @"headerName3" : @"headerValue3",
51 scoped_refptr<HttpResponseHeaders> http_response_headers =
52 CreateHeadersFromNSHTTPURLResponse(http_response);
54 AreHeadersEqual(http_response.get(), http_response_headers.get()));