OLD | NEW |
---|---|
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 <string> | |
6 #include <utility> | |
7 #include <vector> | |
8 | |
5 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
6 #include "net/cookies/cookie_util.h" | 10 #include "net/cookies/cookie_util.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
8 | 12 |
13 namespace { | |
14 | |
15 struct RequestCookieParsingTest { | |
16 std::string str; | |
17 std::vector<std::pair<std::string, std::string> > parsed; | |
18 }; | |
19 | |
20 net::cookie_util::ParsedRequestCookies MakeParsedRequestCookies( | |
21 const std::vector<std::pair<std::string, std::string> >& data) { | |
22 net::cookie_util::ParsedRequestCookies parsed; | |
23 for (size_t i = 0; i < data.size(); i++) { | |
24 parsed.push_back(std::make_pair(base::StringPiece(data[i].first), | |
25 base::StringPiece(data[i].second))); | |
26 } | |
27 return parsed; | |
28 } | |
29 | |
30 void CheckParse( | |
31 const std::string& str, | |
32 const std::vector<std::pair<std::string, std::string> >& parsed_expected) { | |
33 net::cookie_util::ParsedRequestCookies parsed; | |
34 net::cookie_util::ParseRequestCookieLine(str, &parsed); | |
35 EXPECT_EQ(MakeParsedRequestCookies(parsed_expected), parsed); | |
36 } | |
37 | |
38 void CheckSerialize( | |
39 const std::vector<std::pair<std::string, std::string> >& parsed, | |
40 const std::string& str_expected) { | |
41 net::cookie_util::ParsedRequestCookies prc = | |
42 MakeParsedRequestCookies(parsed); | |
43 EXPECT_EQ(str_expected, net::cookie_util::SerializeRequestCookieLine(prc)); | |
44 } | |
45 | |
46 } // namespace | |
47 | |
9 TEST(CookieUtilTest, TestDomainIsHostOnly) { | 48 TEST(CookieUtilTest, TestDomainIsHostOnly) { |
10 const struct { | 49 const struct { |
11 const char* str; | 50 const char* str; |
12 const bool is_host_only; | 51 const bool is_host_only; |
13 } tests[] = { | 52 } tests[] = { |
14 { "", true }, | 53 { "", true }, |
15 { "www.google.com", true }, | 54 { "www.google.com", true }, |
16 { ".google.com", false } | 55 { ".google.com", false } |
17 }; | 56 }; |
18 | 57 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
102 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 141 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
103 parsed_time = net::cookie_util::ParseCookieTime(tests[i].str); | 142 parsed_time = net::cookie_util::ParseCookieTime(tests[i].str); |
104 if (!tests[i].valid) { | 143 if (!tests[i].valid) { |
105 EXPECT_FALSE(!parsed_time.is_null()) << tests[i].str; | 144 EXPECT_FALSE(!parsed_time.is_null()) << tests[i].str; |
106 continue; | 145 continue; |
107 } | 146 } |
108 EXPECT_TRUE(!parsed_time.is_null()) << tests[i].str; | 147 EXPECT_TRUE(!parsed_time.is_null()) << tests[i].str; |
109 EXPECT_EQ(tests[i].epoch, parsed_time.ToTimeT()) << tests[i].str; | 148 EXPECT_EQ(tests[i].epoch, parsed_time.ToTimeT()) << tests[i].str; |
110 } | 149 } |
111 } | 150 } |
151 | |
152 TEST(CookieUtilTest, TestRequestCookieParsing) { | |
153 std::vector<RequestCookieParsingTest> tests; | |
154 | |
155 // Simple case. | |
156 tests.push_back(RequestCookieParsingTest()); | |
157 tests.back().str = "key=value"; | |
158 tests.back().parsed.push_back(std::make_pair(std::string("key"), | |
159 std::string("value"))); | |
160 // Multiple key/value pairs. | |
161 tests.push_back(RequestCookieParsingTest()); | |
162 tests.back().str = "key1=value1; key2=value2"; | |
163 tests.back().parsed.push_back(std::make_pair(std::string("key1"), | |
164 std::string("value1"))); | |
165 tests.back().parsed.push_back(std::make_pair(std::string("key2"), | |
166 std::string("value2"))); | |
167 // Empty value. | |
168 tests.push_back(RequestCookieParsingTest()); | |
169 tests.back().str = "key=; otherkey=1234"; | |
170 tests.back().parsed.push_back(std::make_pair(std::string("key"), | |
171 std::string())); | |
172 tests.back().parsed.push_back(std::make_pair(std::string("otherkey"), | |
173 std::string("1234"))); | |
174 // Special characters (including equals signs) in value. | |
175 tests.push_back(RequestCookieParsingTest()); | |
176 tests.back().str = "key=; a2=s=(./&t=:&u=a#$; a3=+~"; | |
177 tests.back().parsed.push_back(std::make_pair(std::string("key"), | |
178 std::string())); | |
179 tests.back().parsed.push_back(std::make_pair(std::string("a2"), | |
180 std::string("s=(./&t=:&u=a#$"))); | |
181 tests.back().parsed.push_back(std::make_pair(std::string("a3"), | |
182 std::string("+~"))); | |
183 // Quoted value. | |
184 tests.push_back(RequestCookieParsingTest()); | |
185 tests.back().str = "key=\"abcdef\"; otherkey=1234"; | |
battre
2014/07/10 10:02:56
can you change this to "key=\"ab;cdef\"; otherkey=
Marc Treib
2014/07/10 11:35:45
Semicolons are not allowed in cookie values, even
| |
186 tests.back().parsed.push_back(std::make_pair(std::string("key"), | |
187 std::string("\"abcdef\""))); | |
188 tests.back().parsed.push_back(std::make_pair(std::string("otherkey"), | |
189 std::string("1234"))); | |
190 | |
191 for (size_t i = 0; i < tests.size(); i++) { | |
battre
2014/07/10 10:02:56
How about adding
SCOPED_TRACE(testing::Message() <
Marc Treib
2014/07/10 11:35:45
Done. Didn't know about that!
| |
192 CheckParse(tests[i].str, tests[i].parsed); | |
193 CheckSerialize(tests[i].parsed, tests[i].str); | |
194 } | |
195 } | |
OLD | NEW |