summaryrefslogtreecommitdiffstats
path: root/ios/net/http_response_headers_util_unittest.mm
blob: 27dfcb899c2aceed273f1a2680d13565d9c8eb96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ios/net/http_response_headers_util.h"

#import <Foundation/Foundation.h>

#include <algorithm>

#include "base/mac/scoped_nsobject.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/sys_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

// Returns true if all the information in |http_response| is present in
// |http_response_headers|.
bool AreHeadersEqual(NSHTTPURLResponse* http_response,
                     HttpResponseHeaders* http_response_headers) {
  if (!http_response || !http_response_headers)
    return false;
  if (http_response.statusCode != http_response_headers->response_code())
    return false;
  __block bool all_headers_present = true;
  [http_response.allHeaderFields
      enumerateKeysAndObjectsUsingBlock:^(NSString* header_name,
                                          NSString* header_value, BOOL* stop) {
        std::string value;
        http_response_headers->GetNormalizedHeader(
            base::SysNSStringToUTF8(header_name), &value);
        all_headers_present = (value == base::SysNSStringToUTF8(header_value));
        *stop = !all_headers_present;
      }];
  return all_headers_present;
}

// Tests that HttpResponseHeaders created from NSHTTPURLResponses successfully
// copy over the status code and the header names and values.
TEST(HttpResponseHeadersUtilTest, CreateHeadersFromNSHTTPURLResponse) {
  base::scoped_nsobject<NSHTTPURLResponse> http_response(
      [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"test.com"]
                                  statusCode:200
                                 HTTPVersion:@"HTTP/1.1"
                                headerFields:@{
                                  @"headerName1" : @"headerValue1",
                                  @"headerName2" : @"headerValue2",
                                  @"headerName3" : @"headerValue3",
                                }]);
  scoped_refptr<HttpResponseHeaders> http_response_headers =
      CreateHeadersFromNSHTTPURLResponse(http_response);
  EXPECT_TRUE(
      AreHeadersEqual(http_response.get(), http_response_headers.get()));
}

}  // namespace net.