Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <memory> | 5 #include <memory> |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "content/public/test/browser_test.h" | 9 #include "content/public/test/browser_test.h" |
| 10 #include "headless/public/domains/network.h" | |
| 10 #include "headless/public/domains/page.h" | 11 #include "headless/public/domains/page.h" |
| 11 #include "headless/public/headless_browser.h" | 12 #include "headless/public/headless_browser.h" |
| 12 #include "headless/public/headless_devtools_client.h" | 13 #include "headless/public/headless_devtools_client.h" |
| 13 #include "headless/public/headless_devtools_target.h" | 14 #include "headless/public/headless_devtools_target.h" |
| 14 #include "headless/public/headless_web_contents.h" | 15 #include "headless/public/headless_web_contents.h" |
| 15 #include "headless/test/headless_browser_test.h" | 16 #include "headless/test/headless_browser_test.h" |
| 16 #include "headless/test/test_protocol_handler.h" | 17 #include "headless/test/test_protocol_handler.h" |
| 17 #include "headless/test/test_url_request_job.h" | 18 #include "headless/test/test_url_request_job.h" |
| 18 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 19 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 19 #include "net/cookies/cookie_store.h" | 20 #include "net/cookies/cookie_store.h" |
| (...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 474 "document.cookie = 'shape=oblong', window.location.reload()") | 475 "document.cookie = 'shape=oblong', window.location.reload()") |
| 475 ->HasExceptionDetails()); | 476 ->HasExceptionDetails()); |
| 476 EXPECT_TRUE(WaitForLoad(web_contents)); | 477 EXPECT_TRUE(WaitForLoad(web_contents)); |
| 477 | 478 |
| 478 // We should have sent the cookie this time. | 479 // We should have sent the cookie this time. |
| 479 EXPECT_EQ(1u, sent_cookies.size()); | 480 EXPECT_EQ(1u, sent_cookies.size()); |
| 480 EXPECT_EQ("shape", sent_cookies[0].Name()); | 481 EXPECT_EQ("shape", sent_cookies[0].Name()); |
| 481 EXPECT_EQ("oblong", sent_cookies[0].Value()); | 482 EXPECT_EQ("oblong", sent_cookies[0].Value()); |
| 482 } | 483 } |
| 483 | 484 |
| 485 namespace { | |
| 486 | |
| 487 class CookieSetter { | |
| 488 public: | |
| 489 CookieSetter(HeadlessBrowserTest* browser_test, | |
| 490 HeadlessWebContents* web_contents, | |
| 491 std::unique_ptr<network::SetCookieParams> params) | |
| 492 : browser_test_(browser_test), | |
| 493 web_contents_(web_contents), | |
| 494 devtools_client_(HeadlessDevToolsClient::Create()) { | |
| 495 web_contents_->GetDevToolsTarget()->AttachClient(devtools_client_.get()); | |
| 496 devtools_client_->GetNetwork()->GetExperimental()->SetCookie( | |
| 497 std::move(params), | |
| 498 base::Bind(&CookieSetter::OnSetCookieResult, base::Unretained(this))); | |
| 499 } | |
| 500 | |
| 501 ~CookieSetter() { | |
| 502 web_contents_->GetDevToolsTarget()->DetachClient(devtools_client_.get()); | |
| 503 } | |
| 504 | |
| 505 void OnSetCookieResult(std::unique_ptr<network::SetCookieResult> result) { | |
| 506 result_ = std::move(result); | |
| 507 browser_test_->FinishAsynchronousTest(); | |
| 508 } | |
| 509 | |
| 510 std::unique_ptr<network::SetCookieResult> TakeResult() { | |
| 511 return std::move(result_); | |
| 512 } | |
| 513 | |
| 514 private: | |
| 515 HeadlessBrowserTest* browser_test_; // Not owned. | |
| 516 HeadlessWebContents* web_contents_; // Not owned. | |
| 517 std::unique_ptr<HeadlessDevToolsClient> devtools_client_; | |
| 518 | |
| 519 std::unique_ptr<network::SetCookieResult> result_; | |
| 520 | |
| 521 DISALLOW_COPY_AND_ASSIGN(CookieSetter); | |
| 522 }; | |
| 523 | |
| 524 } // namespace | |
| 525 | |
| 526 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, SetCookiesWithDevTools) { | |
| 527 net::CookieList sent_cookies; | |
| 528 ProtocolHandlerMap protocol_handlers; | |
| 529 protocol_handlers[url::kHttpsScheme] = | |
| 530 base::WrapUnique(new ProtocolHandlerWithCookies(&sent_cookies)); | |
| 531 | |
| 532 HeadlessBrowserContext* browser_context = | |
| 533 browser() | |
| 534 ->CreateBrowserContextBuilder() | |
| 535 .SetProtocolHandlers(std::move(protocol_handlers)) | |
| 536 .Build(); | |
| 537 | |
| 538 HeadlessWebContents* web_contents = | |
| 539 browser_context->CreateWebContentsBuilder() | |
| 540 .SetInitialURL(GURL("https://example.com/cookie.html")) | |
| 541 .Build(); | |
| 542 EXPECT_TRUE(WaitForLoad(web_contents)); | |
| 543 | |
| 544 // The first load has no cookies. | |
| 545 EXPECT_EQ(0u, sent_cookies.size()); | |
| 546 | |
| 547 // Set some cookies. | |
| 548 { | |
| 549 std::unique_ptr<network::SetCookieParams> params = | |
|
alex clarke (OOO till 29th)
2016/08/18 15:05:12
nit: set_cookie_params and below too?
Sami
2016/08/18 16:13:42
Done.
| |
| 550 network::SetCookieParams::Builder() | |
| 551 .SetUrl("https://example.com") | |
| 552 .SetName("shape") | |
| 553 .SetValue("oblong") | |
| 554 .Build(); | |
| 555 CookieSetter cookie_setter(this, web_contents, std::move(params)); | |
| 556 RunAsynchronousTest(); | |
| 557 std::unique_ptr<network::SetCookieResult> result = | |
| 558 cookie_setter.TakeResult(); | |
| 559 EXPECT_TRUE(result->GetSuccess()); | |
| 560 } | |
| 561 { | |
| 562 std::unique_ptr<network::SetCookieParams> params = | |
| 563 network::SetCookieParams::Builder() | |
| 564 .SetUrl("https://other.com") | |
| 565 .SetName("shape") | |
| 566 .SetValue("trapezoid") | |
| 567 .SetDomain("other.com") | |
| 568 .SetPath("") | |
|
alex clarke (OOO till 29th)
2016/08/18 15:05:12
Maybe add a comment explaining why you need to set
Sami
2016/08/18 16:13:42
Good point, done.
| |
| 569 .SetSecure(true) | |
| 570 .SetHttpOnly(true) | |
| 571 .SetSameSite(network::CookieSameSite::STRICT) | |
| 572 .SetExpirationDate(0) | |
| 573 .Build(); | |
| 574 CookieSetter cookie_setter(this, web_contents, std::move(params)); | |
| 575 RunAsynchronousTest(); | |
| 576 std::unique_ptr<network::SetCookieResult> result = | |
| 577 cookie_setter.TakeResult(); | |
| 578 EXPECT_TRUE(result->GetSuccess()); | |
| 579 } | |
| 580 | |
| 581 // Reload the page. | |
| 582 EXPECT_FALSE(EvaluateScript(web_contents, "window.location.reload();") | |
| 583 ->HasExceptionDetails()); | |
| 584 EXPECT_TRUE(WaitForLoad(web_contents)); | |
| 585 | |
| 586 // We should have sent the matching cookies this time. | |
| 587 EXPECT_EQ(1u, sent_cookies.size()); | |
| 588 EXPECT_EQ("shape", sent_cookies[0].Name()); | |
| 589 EXPECT_EQ("oblong", sent_cookies[0].Value()); | |
| 590 } | |
| 591 | |
| 484 } // namespace headless | 592 } // namespace headless |
| OLD | NEW |