1 // Copyright 2012 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/http/http_request_headers.h"
6
7 #include <memory>
8
9 #include "base/values.h"
10 #include "net/log/net_log_capture_mode.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace net {
14
15 namespace {
16
TEST(HttpRequestHeaders,HasHeader)17 TEST(HttpRequestHeaders, HasHeader) {
18 HttpRequestHeaders headers;
19 headers.SetHeader("Foo", "bar");
20 EXPECT_TRUE(headers.HasHeader("foo"));
21 EXPECT_TRUE(headers.HasHeader("Foo"));
22 EXPECT_FALSE(headers.HasHeader("Fo"));
23
24 const HttpRequestHeaders& headers_ref = headers;
25 EXPECT_TRUE(headers_ref.HasHeader("foo"));
26 EXPECT_TRUE(headers_ref.HasHeader("Foo"));
27 EXPECT_FALSE(headers_ref.HasHeader("Fo"));
28 }
29
TEST(HttpRequestHeaders,SetHeader)30 TEST(HttpRequestHeaders, SetHeader) {
31 HttpRequestHeaders headers;
32 headers.SetHeader("Foo", "bar");
33 EXPECT_EQ("Foo: bar\r\n\r\n", headers.ToString());
34 }
35
TEST(HttpRequestHeaders,SetMultipleHeaders)36 TEST(HttpRequestHeaders, SetMultipleHeaders) {
37 HttpRequestHeaders headers;
38 headers.SetHeader("Cookie-Monster", "Nom nom nom");
39 headers.SetHeader("Domo-Kun", "Loves Chrome");
40 EXPECT_EQ("Cookie-Monster: Nom nom nom\r\nDomo-Kun: Loves Chrome\r\n\r\n",
41 headers.ToString());
42 }
43
TEST(HttpRequestHeaders,SetHeaderTwice)44 TEST(HttpRequestHeaders, SetHeaderTwice) {
45 HttpRequestHeaders headers;
46 headers.SetHeader("Foo", "bar");
47 headers.SetHeader("Foo", "bar");
48 EXPECT_EQ("Foo: bar\r\n\r\n", headers.ToString());
49 }
50
TEST(HttpRequestHeaders,SetHeaderTwiceCaseInsensitive)51 TEST(HttpRequestHeaders, SetHeaderTwiceCaseInsensitive) {
52 HttpRequestHeaders headers;
53 headers.SetHeader("Foo", "bar");
54 headers.SetHeader("FoO", "Bar");
55 EXPECT_EQ("Foo: Bar\r\n\r\n", headers.ToString());
56 }
57
TEST(HttpRequestHeaders,SetHeaderTwiceSamePrefix)58 TEST(HttpRequestHeaders, SetHeaderTwiceSamePrefix) {
59 HttpRequestHeaders headers;
60 headers.SetHeader("FooBar", "baz");
61 headers.SetHeader("Foo", "qux");
62 EXPECT_EQ("FooBar: baz\r\nFoo: qux\r\n\r\n", headers.ToString());
63 const HttpRequestHeaders& headers_ref = headers;
64 EXPECT_EQ("FooBar: baz\r\nFoo: qux\r\n\r\n", headers_ref.ToString());
65 }
66
TEST(HttpRequestHeaders,SetEmptyHeader)67 TEST(HttpRequestHeaders, SetEmptyHeader) {
68 HttpRequestHeaders headers;
69 headers.SetHeader("Foo", "Bar");
70 headers.SetHeader("Bar", "");
71 EXPECT_EQ("Foo: Bar\r\nBar: \r\n\r\n", headers.ToString());
72 }
73
TEST(HttpRequestHeaders,SetHeaderIfMissing)74 TEST(HttpRequestHeaders, SetHeaderIfMissing) {
75 HttpRequestHeaders headers;
76 headers.SetHeaderIfMissing("Foo", "Bar");
77 EXPECT_EQ("Foo: Bar\r\n\r\n", headers.ToString());
78 headers.SetHeaderIfMissing("Foo", "Baz");
79 EXPECT_EQ("Foo: Bar\r\n\r\n", headers.ToString());
80 }
81
TEST(HttpRequestHeaders,RemoveHeader)82 TEST(HttpRequestHeaders, RemoveHeader) {
83 HttpRequestHeaders headers;
84 headers.SetHeader("Foo", "bar");
85 headers.RemoveHeader("Foo");
86 EXPECT_EQ("\r\n", headers.ToString());
87 }
88
TEST(HttpRequestHeaders,RemoveHeaderMissingHeader)89 TEST(HttpRequestHeaders, RemoveHeaderMissingHeader) {
90 HttpRequestHeaders headers;
91 headers.SetHeader("Foo", "bar");
92 headers.RemoveHeader("Bar");
93 EXPECT_EQ("Foo: bar\r\n\r\n", headers.ToString());
94 }
95
TEST(HttpRequestHeaders,RemoveHeaderCaseInsensitive)96 TEST(HttpRequestHeaders, RemoveHeaderCaseInsensitive) {
97 HttpRequestHeaders headers;
98 headers.SetHeader("Foo", "bar");
99 headers.SetHeader("All-Your-Base", "Belongs To Chrome");
100 headers.RemoveHeader("foo");
101 EXPECT_EQ("All-Your-Base: Belongs To Chrome\r\n\r\n", headers.ToString());
102 }
103
TEST(HttpRequestHeaders,AddHeaderFromString)104 TEST(HttpRequestHeaders, AddHeaderFromString) {
105 HttpRequestHeaders headers;
106 headers.AddHeaderFromString("Foo: bar");
107 EXPECT_EQ("Foo: bar\r\n\r\n", headers.ToString());
108 }
109
TEST(HttpRequestHeaders,AddHeaderFromStringNoLeadingWhitespace)110 TEST(HttpRequestHeaders, AddHeaderFromStringNoLeadingWhitespace) {
111 HttpRequestHeaders headers;
112 headers.AddHeaderFromString("Foo:bar");
113 EXPECT_EQ("Foo: bar\r\n\r\n", headers.ToString());
114 }
115
TEST(HttpRequestHeaders,AddHeaderFromStringMoreLeadingWhitespace)116 TEST(HttpRequestHeaders, AddHeaderFromStringMoreLeadingWhitespace) {
117 HttpRequestHeaders headers;
118 headers.AddHeaderFromString("Foo: \t \t bar");
119 EXPECT_EQ("Foo: bar\r\n\r\n", headers.ToString());
120 }
121
TEST(HttpRequestHeaders,AddHeaderFromStringTrailingWhitespace)122 TEST(HttpRequestHeaders, AddHeaderFromStringTrailingWhitespace) {
123 HttpRequestHeaders headers;
124 headers.AddHeaderFromString("Foo: bar \t \t ");
125 EXPECT_EQ("Foo: bar\r\n\r\n", headers.ToString());
126 }
127
TEST(HttpRequestHeaders,AddHeaderFromStringLeadingTrailingWhitespace)128 TEST(HttpRequestHeaders, AddHeaderFromStringLeadingTrailingWhitespace) {
129 HttpRequestHeaders headers;
130 headers.AddHeaderFromString("Foo: \t bar\t ");
131 EXPECT_EQ("Foo: bar\r\n\r\n", headers.ToString());
132 }
133
TEST(HttpRequestHeaders,AddHeaderFromStringWithEmptyValue)134 TEST(HttpRequestHeaders, AddHeaderFromStringWithEmptyValue) {
135 HttpRequestHeaders headers;
136 headers.AddHeaderFromString("Foo:");
137 EXPECT_EQ("Foo: \r\n\r\n", headers.ToString());
138 }
139
TEST(HttpRequestHeaders,AddHeaderFromStringWithWhitespaceValue)140 TEST(HttpRequestHeaders, AddHeaderFromStringWithWhitespaceValue) {
141 HttpRequestHeaders headers;
142 headers.AddHeaderFromString("Foo: ");
143 EXPECT_EQ("Foo: \r\n\r\n", headers.ToString());
144 }
145
TEST(HttpRequestHeaders,MergeFrom)146 TEST(HttpRequestHeaders, MergeFrom) {
147 HttpRequestHeaders headers;
148 headers.SetHeader("A", "A");
149 headers.SetHeader("B", "B");
150
151 HttpRequestHeaders headers2;
152 headers2.SetHeader("B", "b");
153 headers2.SetHeader("C", "c");
154 headers.MergeFrom(headers2);
155 EXPECT_EQ("A: A\r\nB: b\r\nC: c\r\n\r\n", headers.ToString());
156 }
157
TEST(HttpRequestHeaders,CopyFrom)158 TEST(HttpRequestHeaders, CopyFrom) {
159 HttpRequestHeaders headers;
160 headers.SetHeader("A", "A");
161 headers.SetHeader("B", "B");
162
163 HttpRequestHeaders headers2;
164 headers2.SetHeader("B", "b");
165 headers2.SetHeader("C", "c");
166 headers.CopyFrom(headers2);
167 EXPECT_EQ("B: b\r\nC: c\r\n\r\n", headers.ToString());
168 }
169
170 } // namespace
171
172 } // namespace net
173