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

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

Issue 1215933004: New new versions of Starts/EndsWith and SplitString in net (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@starts_with
Patch Set: Created 5 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
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 "net/cookies/cookie_store_unittest.h" 5 #include "net/cookies/cookie_store_unittest.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 const std::string& coded_priority_str, 429 const std::string& coded_priority_str,
430 size_t expected_low_count, 430 size_t expected_low_count,
431 size_t expected_medium_count, 431 size_t expected_medium_count,
432 size_t expected_high_count) { 432 size_t expected_high_count) {
433 DeleteAll(cm); 433 DeleteAll(cm);
434 int next_cookie_id = 0; 434 int next_cookie_id = 0;
435 std::vector<CookiePriority> priority_list; 435 std::vector<CookiePriority> priority_list;
436 std::vector<int> id_list[3]; // Indexed by CookiePriority. 436 std::vector<int> id_list[3]; // Indexed by CookiePriority.
437 437
438 // Parse |coded_priority_str| and add cookies. 438 // Parse |coded_priority_str| and add cookies.
439 std::vector<std::string> priority_tok_list; 439 for (const std::string& token :
440 base::SplitString(coded_priority_str, ' ', &priority_tok_list); 440 base::SplitString(coded_priority_str, " ", base::TRIM_WHITESPACE,
441 for (std::vector<std::string>::iterator it = priority_tok_list.begin(); 441 base::SPLIT_WANT_ALL)) {
Ryan Sleevi 2015/07/06 08:52:01 nit: While you're here, this (and 467) are safe to
brettw 2015/07/06 16:52:34 I have no idea what you're asking me to clean up h
Ryan Sleevi 2015/07/06 16:58:12 Sorry, meant const base::StringPiece& token : bas
442 it != priority_tok_list.end(); ++it) { 442 DCHECK(!token.empty());
443 size_t len = it->length();
444 DCHECK_NE(len, 0U);
445 // Take last character as priority. 443 // Take last character as priority.
446 CookiePriority priority = CharToPriority((*it)[len - 1]); 444 CookiePriority priority = CharToPriority(token[token.length() - 1]);
447 std::string priority_str = CookiePriorityToString(priority); 445 std::string priority_str = CookiePriorityToString(priority);
448 // The rest of the string (possibly empty) specifies repetition. 446 // The rest of the string (possibly empty) specifies repetition.
449 int rep = 1; 447 int rep = 1;
450 if (!it->empty()) { 448 if (!token.empty()) {
451 bool result = base::StringToInt( 449 bool result = base::StringToInt(
452 base::StringPiece(it->begin(), it->end() - 1), &rep); 450 base::StringPiece(token.begin(), token.end() - 1), &rep);
453 DCHECK(result); 451 DCHECK(result);
454 } 452 }
455 for (; rep > 0; --rep, ++next_cookie_id) { 453 for (; rep > 0; --rep, ++next_cookie_id) {
456 std::string cookie = base::StringPrintf( 454 std::string cookie = base::StringPrintf(
457 "a%d=b;priority=%s", next_cookie_id, priority_str.c_str()); 455 "a%d=b;priority=%s", next_cookie_id, priority_str.c_str());
458 EXPECT_TRUE(SetCookie(cm, url_google_, cookie)); 456 EXPECT_TRUE(SetCookie(cm, url_google_, cookie));
459 priority_list.push_back(priority); 457 priority_list.push_back(priority);
460 id_list[priority].push_back(next_cookie_id); 458 id_list[priority].push_back(next_cookie_id);
461 } 459 }
462 } 460 }
463 461
464 int num_cookies = static_cast<int>(priority_list.size()); 462 int num_cookies = static_cast<int>(priority_list.size());
465 std::vector<int> surviving_id_list[3]; // Indexed by CookiePriority. 463 std::vector<int> surviving_id_list[3]; // Indexed by CookiePriority.
466 464
467 // Parse the list of cookies 465 // Parse the list of cookies
468 std::string cookie_str = this->GetCookies(cm, url_google_); 466 std::string cookie_str = this->GetCookies(cm, url_google_);
469 std::vector<std::string> cookie_tok_list; 467 for (const std::string& token : base::SplitString(
470 base::SplitString(cookie_str, ';', &cookie_tok_list); 468 cookie_str, ";", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
471 for (std::vector<std::string>::iterator it = cookie_tok_list.begin();
472 it != cookie_tok_list.end(); ++it) {
473 // Assuming *it is "a#=b", so extract and parse "#" portion. 469 // Assuming *it is "a#=b", so extract and parse "#" portion.
474 int id = -1; 470 int id = -1;
475 bool result = base::StringToInt( 471 bool result = base::StringToInt(
476 base::StringPiece(it->begin() + 1, it->end() - 2), &id); 472 base::StringPiece(token.begin() + 1, token.end() - 2), &id);
477 DCHECK(result); 473 DCHECK(result);
478 DCHECK_GE(id, 0); 474 DCHECK_GE(id, 0);
479 DCHECK_LT(id, num_cookies); 475 DCHECK_LT(id, num_cookies);
480 surviving_id_list[priority_list[id]].push_back(id); 476 surviving_id_list[priority_list[id]].push_back(id);
481 } 477 }
482 478
483 // Validate each priority. 479 // Validate each priority.
484 size_t expected_count[3] = { 480 size_t expected_count[3] = {
485 expected_low_count, expected_medium_count, expected_high_count}; 481 expected_low_count, expected_medium_count, expected_high_count};
486 for (int i = 0; i < 3; ++i) { 482 for (int i = 0; i < 3; ++i) {
(...skipping 2478 matching lines...) Expand 10 before | Expand all | Expand 10 after
2965 monster()->AddCallbackForCookie( 2961 monster()->AddCallbackForCookie(
2966 test_url_, "abc", 2962 test_url_, "abc",
2967 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); 2963 base::Bind(&RecordCookieChanges, &cookies1, nullptr)));
2968 SetCookie(monster(), test_url_, "abc=def"); 2964 SetCookie(monster(), test_url_, "abc=def");
2969 base::MessageLoop::current()->RunUntilIdle(); 2965 base::MessageLoop::current()->RunUntilIdle();
2970 EXPECT_EQ(1U, cookies0.size()); 2966 EXPECT_EQ(1U, cookies0.size());
2971 EXPECT_EQ(1U, cookies0.size()); 2967 EXPECT_EQ(1U, cookies0.size());
2972 } 2968 }
2973 2969
2974 } // namespace net 2970 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698