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> set_cookie_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(set_cookie_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> set_cookie_params = |
| 550 network::SetCookieParams::Builder() |
| 551 .SetUrl("https://example.com") |
| 552 .SetName("shape") |
| 553 .SetValue("oblong") |
| 554 .Build(); |
| 555 CookieSetter cookie_setter(this, web_contents, |
| 556 std::move(set_cookie_params)); |
| 557 RunAsynchronousTest(); |
| 558 std::unique_ptr<network::SetCookieResult> result = |
| 559 cookie_setter.TakeResult(); |
| 560 EXPECT_TRUE(result->GetSuccess()); |
| 561 } |
| 562 { |
| 563 // Try setting all the fields so we notice if the protocol for any of them |
| 564 // changes. |
| 565 std::unique_ptr<network::SetCookieParams> set_cookie_params = |
| 566 network::SetCookieParams::Builder() |
| 567 .SetUrl("https://other.com") |
| 568 .SetName("shape") |
| 569 .SetValue("trapezoid") |
| 570 .SetDomain("other.com") |
| 571 .SetPath("") |
| 572 .SetSecure(true) |
| 573 .SetHttpOnly(true) |
| 574 .SetSameSite(network::CookieSameSite::STRICT) |
| 575 .SetExpirationDate(0) |
| 576 .Build(); |
| 577 CookieSetter cookie_setter(this, web_contents, |
| 578 std::move(set_cookie_params)); |
| 579 RunAsynchronousTest(); |
| 580 std::unique_ptr<network::SetCookieResult> result = |
| 581 cookie_setter.TakeResult(); |
| 582 EXPECT_TRUE(result->GetSuccess()); |
| 583 } |
| 584 |
| 585 // Reload the page. |
| 586 EXPECT_FALSE(EvaluateScript(web_contents, "window.location.reload();") |
| 587 ->HasExceptionDetails()); |
| 588 EXPECT_TRUE(WaitForLoad(web_contents)); |
| 589 |
| 590 // We should have sent the matching cookies this time. |
| 591 EXPECT_EQ(1u, sent_cookies.size()); |
| 592 EXPECT_EQ("shape", sent_cookies[0].Name()); |
| 593 EXPECT_EQ("oblong", sent_cookies[0].Value()); |
| 594 } |
| 595 |
484 } // namespace headless | 596 } // namespace headless |
OLD | NEW |