• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2015 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/base/apple/http_response_headers_util.h"
6
7#import <Foundation/Foundation.h>
8
9#include <algorithm>
10
11#include "base/strings/string_number_conversions.h"
12#include "base/strings/sys_string_conversions.h"
13#include "testing/gtest/include/gtest/gtest.h"
14#include "testing/platform_test.h"
15
16namespace net {
17
18// Returns true if all the information in |http_response| is present in
19// |http_response_headers|.
20bool 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
32                ->GetNormalizedHeader(base::SysNSStringToUTF8(header_name))
33                .value_or(std::string());
34        all_headers_present = (value == base::SysNSStringToUTF8(header_value));
35        *stop = !all_headers_present;
36      }];
37  return all_headers_present;
38}
39
40using HttpResponseHeadersUtilTest = PlatformTest;
41
42// Tests that HttpResponseHeaders created from NSHTTPURLResponses successfully
43// copy over the status code and the header names and values.
44TEST_F(HttpResponseHeadersUtilTest, CreateHeadersFromNSHTTPURLResponse) {
45  NSHTTPURLResponse* http_response =
46      [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"test.com"]
47                                  statusCode:200
48                                 HTTPVersion:@"HTTP/1.1"
49                                headerFields:@{
50                                  @"headerName1" : @"headerValue1",
51                                  @"headerName2" : @"headerValue2",
52                                  @"headerName3" : @"headerValue3",
53                                }];
54  scoped_refptr<HttpResponseHeaders> http_response_headers =
55      CreateHeadersFromNSHTTPURLResponse(http_response);
56  EXPECT_TRUE(AreHeadersEqual(http_response, http_response_headers.get()));
57}
58
59}  // namespace net.
60