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