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

Side by Side Diff: net/cookies/parsed_cookie_unittest.cc

Issue 10697035: Add a mutable version of CookieMonster::ParsedCookie (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merged with ToT Created 8 years, 5 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
« net/cookies/parsed_cookie.cc ('K') | « net/cookies/parsed_cookie.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <string> 5 #include <string>
6 6
7 #include "net/cookies/parsed_cookie.h" 7 #include "net/cookies/parsed_cookie.h"
8 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 9
10 namespace net { 10 namespace net {
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 EXPECT_EQ("hello", 268 EXPECT_EQ("hello",
269 ParsedCookie::ParseValueString("hello\nworld")); 269 ParsedCookie::ParseValueString("hello\nworld"));
270 EXPECT_EQ("fs!!@", 270 EXPECT_EQ("fs!!@",
271 ParsedCookie::ParseValueString("fs!!@;helloworld")); 271 ParsedCookie::ParseValueString("fs!!@;helloworld"));
272 EXPECT_EQ("hello world\tgood", 272 EXPECT_EQ("hello world\tgood",
273 ParsedCookie::ParseValueString("hello world\tgood\rbye")); 273 ParsedCookie::ParseValueString("hello world\tgood\rbye"));
274 EXPECT_EQ("A=B=C", 274 EXPECT_EQ("A=B=C",
275 ParsedCookie::ParseValueString("A=B=C;D=E")); 275 ParsedCookie::ParseValueString("A=B=C;D=E"));
276 } 276 }
277 277
278 TEST(ParsedCookieTest, SerializeCookieLine) {
279 const char input[] = "ANCUUID=zohNumRKgI0oxyhSsV3Z7D ; "
280 "expires=Sun, 18-Apr-2027 21:06:29 GMT ; "
281 "path=/ ; ";
282 const char output[] = "ANCUUID=zohNumRKgI0oxyhSsV3Z7D; "
283 "expires=Sun, 18-Apr-2027 21:06:29 GMT; "
284 "path=/";
285 ParsedCookie pc(input);
286 EXPECT_EQ(output, pc.ToCookieLine());
287 }
288
289
290 TEST(ParsedCookieTest, SetNameAndValue) {
291 ParsedCookie empty("");
292 EXPECT_FALSE(empty.IsValid());
293 EXPECT_FALSE(empty.SetDomain("foobar.com"));
294 EXPECT_TRUE(empty.SetName("name"));
295 EXPECT_TRUE(empty.SetValue("value"));
296 EXPECT_EQ("name=value", empty.ToCookieLine());
297 EXPECT_TRUE(empty.IsValid());
298
299 // We don't test
300 // ParsedCookie invalid("@foo=bar");
301 // EXPECT_FALSE(invalid.IsValid());
302 // here because we are slightly more tolerant to invalid cookie names and
303 // values that are set by webservers. We only enforce a correct name and
304 // value if set via SetName() and SetValue().
305
306 ParsedCookie pc("name=value");
307 EXPECT_TRUE(pc.IsValid());
308
309 // Set invalid name / value.
310 EXPECT_FALSE(pc.SetName("@foobar"));
311 EXPECT_EQ("name=value", pc.ToCookieLine());
312 EXPECT_TRUE(pc.IsValid());
313
314 EXPECT_FALSE(pc.SetName(""));
315 EXPECT_EQ("name=value", pc.ToCookieLine());
316 EXPECT_TRUE(pc.IsValid());
317
318 EXPECT_FALSE(pc.SetValue("foo bar"));
319 EXPECT_EQ("name=value", pc.ToCookieLine());
320 EXPECT_TRUE(pc.IsValid());
321
322 EXPECT_FALSE(pc.SetValue("\"foobar"));
323 EXPECT_EQ("name=value", pc.ToCookieLine());
324 EXPECT_TRUE(pc.IsValid());
325
326 // Set valid name / value
327 EXPECT_TRUE(pc.SetName("test"));
328 EXPECT_EQ("test=value", pc.ToCookieLine());
329 EXPECT_TRUE(pc.IsValid());
330
331 EXPECT_TRUE(pc.SetValue("\"foobar\""));
332 EXPECT_EQ("test=\"foobar\"", pc.ToCookieLine());
333 EXPECT_TRUE(pc.IsValid());
334
335 EXPECT_TRUE(pc.SetValue(""));
336 EXPECT_EQ("test=", pc.ToCookieLine());
337 EXPECT_TRUE(pc.IsValid());
338 }
339
340 TEST(ParsedCookieTest, SetAttributes) {
341 ParsedCookie pc("name=value");
342 EXPECT_TRUE(pc.IsValid());
343
344 // Clear an unset attribute.
345 EXPECT_TRUE(pc.SetDomain(""));
346 EXPECT_FALSE(pc.HasDomain());
347 EXPECT_EQ("name=value", pc.ToCookieLine());
348 EXPECT_TRUE(pc.IsValid());
349
350 // Set a string containing an invalid character
351 EXPECT_FALSE(pc.SetDomain("foo;bar"));
352 EXPECT_FALSE(pc.HasDomain());
353 EXPECT_EQ("name=value", pc.ToCookieLine());
354 EXPECT_TRUE(pc.IsValid());
355
356 // Set all other attributes and check that they are appended in order.
357 EXPECT_TRUE(pc.SetDomain("domain.com"));
358 EXPECT_TRUE(pc.SetPath("/"));
359 EXPECT_TRUE(pc.SetMACKey("mackey"));
360 EXPECT_TRUE(pc.SetMACAlgorithm("\"macalgorithm\""));
361 EXPECT_TRUE(pc.SetExpires("Sun, 18-Apr-2027 21:06:29 GMT"));
362 EXPECT_TRUE(pc.SetMaxAge("12345"));
363 EXPECT_TRUE(pc.SetIsSecure(true));
364 EXPECT_TRUE(pc.SetIsHttpOnly(true));
365 EXPECT_EQ("name=value; domain=domain.com; path=/; mac-key=mackey; "
366 "mac-algorithm=\"macalgorithm\"; "
367 "expires=Sun, 18-Apr-2027 21:06:29 GMT; max-age=12345; secure; "
368 "httponly",
369 pc.ToCookieLine());
370 EXPECT_TRUE(pc.HasDomain());
371 EXPECT_TRUE(pc.HasPath());
372 EXPECT_TRUE(pc.HasMACKey());
373 EXPECT_TRUE(pc.HasMACAlgorithm());
374 EXPECT_TRUE(pc.HasExpires());
375 EXPECT_TRUE(pc.HasMaxAge());
376 EXPECT_TRUE(pc.IsSecure());
377 EXPECT_TRUE(pc.IsHttpOnly());
378
379 // Clear one attribute from the middle.
380 EXPECT_TRUE(pc.SetMACAlgorithm(""));
381 EXPECT_TRUE(pc.HasDomain());
382 EXPECT_TRUE(pc.HasPath());
383 EXPECT_TRUE(pc.HasMACKey());
384 EXPECT_FALSE(pc.HasMACAlgorithm());
385 EXPECT_TRUE(pc.HasExpires());
386 EXPECT_TRUE(pc.HasMaxAge());
387 EXPECT_TRUE(pc.IsSecure());
388 EXPECT_TRUE(pc.IsHttpOnly());
389 EXPECT_EQ("name=value; domain=domain.com; path=/; mac-key=mackey; "
390 "expires=Sun, 18-Apr-2027 21:06:29 GMT; max-age=12345; secure; "
391 "httponly",
392 pc.ToCookieLine());
393
394 // Clear the rest and change the name and value.
395 EXPECT_TRUE(pc.SetDomain(""));
396 EXPECT_TRUE(pc.SetPath(""));
397 EXPECT_TRUE(pc.SetMACKey(""));
398 EXPECT_TRUE(pc.SetMACAlgorithm(""));
399 EXPECT_TRUE(pc.SetExpires(""));
400 EXPECT_TRUE(pc.SetMaxAge(""));
401 EXPECT_TRUE(pc.SetIsSecure(false));
402 EXPECT_TRUE(pc.SetIsHttpOnly(false));
403 EXPECT_TRUE(pc.SetName("name2"));
404 EXPECT_TRUE(pc.SetValue("value2"));
405 EXPECT_FALSE(pc.HasDomain());
406 EXPECT_FALSE(pc.HasPath());
407 EXPECT_FALSE(pc.HasMACKey());
408 EXPECT_FALSE(pc.HasMACAlgorithm());
409 EXPECT_FALSE(pc.HasExpires());
410 EXPECT_FALSE(pc.HasMaxAge());
411 EXPECT_FALSE(pc.IsSecure());
412 EXPECT_FALSE(pc.IsHttpOnly());
413 EXPECT_EQ("name2=value2", pc.ToCookieLine());
414 }
415
278 } // namespace net 416 } // namespace net
OLDNEW
« net/cookies/parsed_cookie.cc ('K') | « net/cookies/parsed_cookie.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698