1 // Copyright (c) 2012 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 "net/http/http_request_headers.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/values.h"
9 #include "testing/gtest/include/gtest/gtest.h"
15 TEST(HttpRequestHeaders
, HasHeader
) {
16 HttpRequestHeaders headers
;
17 headers
.SetHeader("Foo", "bar");
18 EXPECT_TRUE(headers
.HasHeader("foo"));
19 EXPECT_TRUE(headers
.HasHeader("Foo"));
20 EXPECT_FALSE(headers
.HasHeader("Fo"));
22 const HttpRequestHeaders
& headers_ref
= headers
;
23 EXPECT_TRUE(headers_ref
.HasHeader("foo"));
24 EXPECT_TRUE(headers_ref
.HasHeader("Foo"));
25 EXPECT_FALSE(headers_ref
.HasHeader("Fo"));
28 TEST(HttpRequestHeaders
, SetHeader
) {
29 HttpRequestHeaders headers
;
30 headers
.SetHeader("Foo", "bar");
31 EXPECT_EQ("Foo: bar\r\n\r\n", headers
.ToString());
34 TEST(HttpRequestHeaders
, SetMultipleHeaders
) {
35 HttpRequestHeaders headers
;
36 headers
.SetHeader("Cookie-Monster", "Nom nom nom");
37 headers
.SetHeader("Domo-Kun", "Loves Chrome");
38 EXPECT_EQ("Cookie-Monster: Nom nom nom\r\nDomo-Kun: Loves Chrome\r\n\r\n",
42 TEST(HttpRequestHeaders
, SetHeaderTwice
) {
43 HttpRequestHeaders headers
;
44 headers
.SetHeader("Foo", "bar");
45 headers
.SetHeader("Foo", "bar");
46 EXPECT_EQ("Foo: bar\r\n\r\n", headers
.ToString());
49 TEST(HttpRequestHeaders
, SetHeaderTwiceCaseInsensitive
) {
50 HttpRequestHeaders headers
;
51 headers
.SetHeader("Foo", "bar");
52 headers
.SetHeader("FoO", "Bar");
53 EXPECT_EQ("Foo: Bar\r\n\r\n", headers
.ToString());
56 TEST(HttpRequestHeaders
, SetHeaderTwiceSamePrefix
) {
57 HttpRequestHeaders headers
;
58 headers
.SetHeader("FooBar", "smokes");
59 headers
.SetHeader("Foo", "crack");
60 EXPECT_EQ("FooBar: smokes\r\nFoo: crack\r\n\r\n", headers
.ToString());
61 const HttpRequestHeaders
& headers_ref
= headers
;
62 EXPECT_EQ("FooBar: smokes\r\nFoo: crack\r\n\r\n", headers_ref
.ToString());
65 TEST(HttpRequestHeaders
, SetEmptyHeader
) {
66 HttpRequestHeaders headers
;
67 headers
.SetHeader("Foo", "Bar");
68 headers
.SetHeader("Bar", "");
69 EXPECT_EQ("Foo: Bar\r\nBar:\r\n\r\n", headers
.ToString());
72 TEST(HttpRequestHeaders
, SetHeaderIfMissing
) {
73 HttpRequestHeaders headers
;
74 headers
.SetHeaderIfMissing("Foo", "Bar");
75 EXPECT_EQ("Foo: Bar\r\n\r\n", headers
.ToString());
76 headers
.SetHeaderIfMissing("Foo", "Baz");
77 EXPECT_EQ("Foo: Bar\r\n\r\n", headers
.ToString());
80 TEST(HttpRequestHeaders
, RemoveHeader
) {
81 HttpRequestHeaders headers
;
82 headers
.SetHeader("Foo", "bar");
83 headers
.RemoveHeader("Foo");
84 EXPECT_EQ("\r\n", headers
.ToString());
87 TEST(HttpRequestHeaders
, RemoveHeaderMissingHeader
) {
88 HttpRequestHeaders headers
;
89 headers
.SetHeader("Foo", "bar");
90 headers
.RemoveHeader("Bar");
91 EXPECT_EQ("Foo: bar\r\n\r\n", headers
.ToString());
94 TEST(HttpRequestHeaders
, RemoveHeaderCaseInsensitive
) {
95 HttpRequestHeaders headers
;
96 headers
.SetHeader("Foo", "bar");
97 headers
.SetHeader("All-Your-Base", "Belongs To Chrome");
98 headers
.RemoveHeader("foo");
99 EXPECT_EQ("All-Your-Base: Belongs To Chrome\r\n\r\n", headers
.ToString());
102 TEST(HttpRequestHeaders
, AddHeaderFromString
) {
103 HttpRequestHeaders headers
;
104 headers
.AddHeaderFromString("Foo: bar");
105 EXPECT_EQ("Foo: bar\r\n\r\n", headers
.ToString());
108 TEST(HttpRequestHeaders
, AddHeaderFromStringNoLeadingWhitespace
) {
109 HttpRequestHeaders headers
;
110 headers
.AddHeaderFromString("Foo:bar");
111 EXPECT_EQ("Foo: bar\r\n\r\n", headers
.ToString());
114 TEST(HttpRequestHeaders
, AddHeaderFromStringMoreLeadingWhitespace
) {
115 HttpRequestHeaders headers
;
116 headers
.AddHeaderFromString("Foo: \t \t bar");
117 EXPECT_EQ("Foo: bar\r\n\r\n", headers
.ToString());
120 TEST(HttpRequestHeaders
, AddHeaderFromStringTrailingWhitespace
) {
121 HttpRequestHeaders headers
;
122 headers
.AddHeaderFromString("Foo: bar \t \t ");
123 EXPECT_EQ("Foo: bar\r\n\r\n", headers
.ToString());
126 TEST(HttpRequestHeaders
, AddHeaderFromStringLeadingTrailingWhitespace
) {
127 HttpRequestHeaders headers
;
128 headers
.AddHeaderFromString("Foo: \t bar\t ");
129 EXPECT_EQ("Foo: bar\r\n\r\n", headers
.ToString());
132 TEST(HttpRequestHeaders
, AddHeaderFromStringWithEmptyValue
) {
133 HttpRequestHeaders headers
;
134 headers
.AddHeaderFromString("Foo:");
135 EXPECT_EQ("Foo:\r\n\r\n", headers
.ToString());
138 TEST(HttpRequestHeaders
, AddHeaderFromStringWithWhitespaceValue
) {
139 HttpRequestHeaders headers
;
140 headers
.AddHeaderFromString("Foo: ");
141 EXPECT_EQ("Foo:\r\n\r\n", headers
.ToString());
144 TEST(HttpRequestHeaders
, MergeFrom
) {
145 HttpRequestHeaders headers
;
146 headers
.SetHeader("A", "A");
147 headers
.SetHeader("B", "B");
149 HttpRequestHeaders headers2
;
150 headers2
.SetHeader("B", "b");
151 headers2
.SetHeader("C", "c");
152 headers
.MergeFrom(headers2
);
153 EXPECT_EQ("A: A\r\nB: b\r\nC: c\r\n\r\n", headers
.ToString());
156 TEST(HttpRequestHeaders
, CopyFrom
) {
157 HttpRequestHeaders headers
;
158 headers
.SetHeader("A", "A");
159 headers
.SetHeader("B", "B");
161 HttpRequestHeaders headers2
;
162 headers2
.SetHeader("B", "b");
163 headers2
.SetHeader("C", "c");
164 headers
.CopyFrom(headers2
);
165 EXPECT_EQ("B: b\r\nC: c\r\n\r\n", headers
.ToString());
168 TEST(HttpRequestHeaders
, ToNetLogParamAndBackAgain
) {
169 HttpRequestHeaders headers
;
170 headers
.SetHeader("B", "b");
171 headers
.SetHeader("A", "a");
172 std::string
request_line("GET /stuff");
174 scoped_ptr
<base::Value
> event_param(headers
.NetLogCallback(
175 &request_line
, NetLogCaptureMode::IncludeCookiesAndCredentials()));
176 HttpRequestHeaders headers2
;
177 std::string request_line2
;
179 ASSERT_TRUE(HttpRequestHeaders::FromNetLogParam(event_param
.get(),
182 EXPECT_EQ(request_line
, request_line2
);
183 EXPECT_EQ("B: b\r\nA: a\r\n\r\n", headers2
.ToString());