Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(176)

Side by Side Diff: net/http/http_util_unittest.cc

Issue 187583002: Cleanup: have common HttpResponseHeaders routine to update with range (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <algorithm> 5 #include <algorithm>
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/strings/string_util.h" 8 #include "base/strings/string_util.h"
9 #include "net/http/http_response_headers.h"
9 #include "net/http/http_util.h" 10 #include "net/http/http_util.h"
10 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
11 12
12 using net::HttpUtil; 13 using net::HttpUtil;
13 14
14 namespace { 15 namespace {
15 class HttpUtilTest : public testing::Test {}; 16 class HttpUtilTest : public testing::Test {};
16 } 17 }
17 18
18 TEST(HttpUtilTest, IsSafeHeader) { 19 TEST(HttpUtilTest, IsSafeHeader) {
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 "user_agent", 94 "user_agent",
94 "viaa", 95 "viaa",
95 }; 96 };
96 for (size_t i = 0; i < arraysize(safe_headers); ++i) { 97 for (size_t i = 0; i < arraysize(safe_headers); ++i) {
97 EXPECT_TRUE(HttpUtil::IsSafeHeader(safe_headers[i])) << safe_headers[i]; 98 EXPECT_TRUE(HttpUtil::IsSafeHeader(safe_headers[i])) << safe_headers[i];
98 EXPECT_TRUE(HttpUtil::IsSafeHeader(StringToUpperASCII(std::string( 99 EXPECT_TRUE(HttpUtil::IsSafeHeader(StringToUpperASCII(std::string(
99 safe_headers[i])))) << safe_headers[i]; 100 safe_headers[i])))) << safe_headers[i];
100 } 101 }
101 } 102 }
102 103
104 TEST(HttpUtilTest, UpdateResponseHeaderWithRange) {
105 const struct {
106 const char* orig_headers;
107 const char* expected_headers;
108 } tests[] = {
109 { "HTTP/1.1 200 OK\n"
110 "Content-Length: 450\n",
111
112 "HTTP/1.1 206 Partial Content\n"
113 "Content-Range: bytes 3-5/450\n"
114 "Content-Length: 3\n",
115 },
116 { "HTTP/1.1 200 OK\n"
117 "Content-Length: 5\n",
118
119 "HTTP/1.1 206 Partial Content\n"
120 "Content-Range: bytes 3-5/5\n"
121 "Content-Length: 3\n",
122 },
123 };
124 const net::HttpByteRange invalid;
125 const net::HttpByteRange valid = net::HttpByteRange::Bounded(3, 5);
126
127 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
128 std::string orig_headers(tests[i].orig_headers);
129 std::replace(orig_headers.begin(), orig_headers.end(), '\n', '\0');
130 scoped_refptr<net::HttpResponseHeaders> parsed(
131 new net::HttpResponseHeaders(orig_headers + '\0'));
132 int64 content_size = parsed->GetContentLength();
133 std::string resulting_headers;
134
135 // With invalid range.
136 EXPECT_FALSE(HttpUtil::UpdateResponseHeadersWithRange(
137 invalid, content_size, true, parsed.get()));
138 parsed->GetNormalizedHeaders(&resulting_headers);
139 EXPECT_EQ(std::string(tests[i].orig_headers), resulting_headers);
140
141 // With valid (3,5) range.
142 EXPECT_TRUE(HttpUtil::UpdateResponseHeadersWithRange(
143 valid, content_size, true, parsed.get()));
144 parsed->GetNormalizedHeaders(&resulting_headers);
145 EXPECT_EQ(std::string(tests[i].expected_headers), resulting_headers);
146 }
147 }
148
103 TEST(HttpUtilTest, HasHeader) { 149 TEST(HttpUtilTest, HasHeader) {
104 static const struct { 150 static const struct {
105 const char* headers; 151 const char* headers;
106 const char* name; 152 const char* name;
107 bool expected_result; 153 bool expected_result;
108 } tests[] = { 154 } tests[] = {
109 { "", "foo", false }, 155 { "", "foo", false },
110 { "foo\r\nbar", "foo", false }, 156 { "foo\r\nbar", "foo", false },
111 { "ffoo: 1", "foo", false }, 157 { "ffoo: 1", "foo", false },
112 { "foo: 1", "foo", true }, 158 { "foo: 1", "foo", true },
(...skipping 951 matching lines...) Expand 10 before | Expand all | Expand 10 after
1064 TEST(HttpUtilTest, NameValuePairsIteratorMissingEndQuote) { 1110 TEST(HttpUtilTest, NameValuePairsIteratorMissingEndQuote) {
1065 std::string data = "name='value"; 1111 std::string data = "name='value";
1066 HttpUtil::NameValuePairsIterator parser(data.begin(), data.end(), ';'); 1112 HttpUtil::NameValuePairsIterator parser(data.begin(), data.end(), ';');
1067 EXPECT_TRUE(parser.valid()); 1113 EXPECT_TRUE(parser.valid());
1068 1114
1069 ASSERT_NO_FATAL_FAILURE( 1115 ASSERT_NO_FATAL_FAILURE(
1070 CheckNextNameValuePair(&parser, true, true, "name", "value")); 1116 CheckNextNameValuePair(&parser, true, true, "name", "value"));
1071 ASSERT_NO_FATAL_FAILURE(CheckNextNameValuePair( 1117 ASSERT_NO_FATAL_FAILURE(CheckNextNameValuePair(
1072 &parser, false, true, std::string(), std::string())); 1118 &parser, false, true, std::string(), std::string()));
1073 } 1119 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698