| 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 <utility> | 5 #include <utility> |
| 6 | 6 |
| 7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "content/public/browser/navigation_controller.h" | 10 #include "content/public/browser/navigation_controller.h" |
| 11 #include "content/public/browser/notification_service.h" | 11 #include "content/public/browser/notification_service.h" |
| 12 #include "content/public/browser/notification_types.h" | 12 #include "content/public/browser/notification_types.h" |
| 13 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
| 14 #include "content/public/common/url_constants.h" | 14 #include "content/public/common/url_constants.h" |
| 15 #include "content/public/test/browser_test_utils.h" | 15 #include "content/public/test/browser_test_utils.h" |
| 16 #include "content/public/test/content_browser_test.h" | 16 #include "content/public/test/content_browser_test.h" |
| 17 #include "content/public/test/content_browser_test_utils.h" | 17 #include "content/public/test/content_browser_test_utils.h" |
| 18 #include "content/public/test/test_navigation_observer.h" |
| 18 #include "content/public/test/test_utils.h" | 19 #include "content/public/test/test_utils.h" |
| 19 #include "content/shell/browser/shell.h" | 20 #include "content/shell/browser/shell.h" |
| 21 #include "net/dns/mock_host_resolver.h" |
| 20 #include "net/test/embedded_test_server/embedded_test_server.h" | 22 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 21 #include "net/test/embedded_test_server/http_request.h" | 23 #include "net/test/embedded_test_server/http_request.h" |
| 22 #include "net/test/embedded_test_server/http_response.h" | 24 #include "net/test/embedded_test_server/http_response.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 26 |
| 25 namespace content { | 27 namespace content { |
| 26 | 28 |
| 27 namespace { | 29 namespace { |
| 28 | 30 |
| 29 // Handles |request| by serving a response with title set to request contents. | 31 // Handles |request| by serving a response with title set to request contents. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 44 return std::move(http_response); | 46 return std::move(http_response); |
| 45 } | 47 } |
| 46 | 48 |
| 47 } // namespace | 49 } // namespace |
| 48 | 50 |
| 49 class SessionHistoryTest : public ContentBrowserTest { | 51 class SessionHistoryTest : public ContentBrowserTest { |
| 50 protected: | 52 protected: |
| 51 SessionHistoryTest() {} | 53 SessionHistoryTest() {} |
| 52 | 54 |
| 53 void SetUpOnMainThread() override { | 55 void SetUpOnMainThread() override { |
| 56 host_resolver()->AddRule("*", "127.0.0.1"); |
| 57 |
| 54 ASSERT_TRUE(embedded_test_server()->Start()); | 58 ASSERT_TRUE(embedded_test_server()->Start()); |
| 59 SetupCrossSiteRedirector(embedded_test_server()); |
| 55 embedded_test_server()->RegisterRequestHandler( | 60 embedded_test_server()->RegisterRequestHandler( |
| 56 base::Bind(&HandleEchoTitleRequest, "/echotitle")); | 61 base::Bind(&HandleEchoTitleRequest, "/echotitle")); |
| 57 | 62 |
| 58 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); | 63 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); |
| 59 } | 64 } |
| 60 | 65 |
| 61 // Simulate clicking a link. Only works on the frames.html testserver page. | 66 // Simulate clicking a link. Only works on the frames.html testserver page. |
| 62 void ClickLink(const std::string& node_id) { | 67 void ClickLink(const std::string& node_id) { |
| 63 GURL url("javascript:clickLink('" + node_id + "')"); | 68 GURL url("javascript:clickLink('" + node_id + "')"); |
| 64 NavigateToURL(shell(), url); | 69 NavigateToURL(shell(), url); |
| (...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 492 // Ensure history.length is properly truncated. | 497 // Ensure history.length is properly truncated. |
| 493 NavigateToURL(shell(), GetURL("title2.html")); | 498 NavigateToURL(shell(), GetURL("title2.html")); |
| 494 | 499 |
| 495 ASSERT_TRUE(ExecuteScriptAndExtractInt( | 500 ASSERT_TRUE(ExecuteScriptAndExtractInt( |
| 496 shell()->web_contents(), | 501 shell()->web_contents(), |
| 497 "domAutomationController.send(history.length)", | 502 "domAutomationController.send(history.length)", |
| 498 &length)); | 503 &length)); |
| 499 EXPECT_EQ(2, length); | 504 EXPECT_EQ(2, length); |
| 500 } | 505 } |
| 501 | 506 |
| 507 // Test that verifies that a cross-process transfer doesn't lose session |
| 508 // history state - https://crbug.com/613004. |
| 509 // |
| 510 // Trigerring a cross-process transfer via embedded_test_server requires use of |
| 511 // a HTTP redirect response (to preserve port number). Therefore the test ends |
| 512 // up accidentally testing redirection logic as well - in particular, the test |
| 513 // uses 307 (rather than 302) redirect to preserve the body of HTTP POST across |
| 514 // redirects (as mandated by https://tools.ietf.org/html/rfc7231#section-6.4.7). |
| 515 IN_PROC_BROWSER_TEST_F(SessionHistoryTest, GoBackToCrossSitePostWithRedirect) { |
| 516 GURL form_url(embedded_test_server()->GetURL( |
| 517 "a.com", "/session_history/form_that_posts_cross_site.html")); |
| 518 GURL redirect_target_url(embedded_test_server()->GetURL("x.com", "/echoall")); |
| 519 GURL page_to_go_back_from( |
| 520 embedded_test_server()->GetURL("c.com", "/title1.html")); |
| 521 |
| 522 // Navigate to the page with form that posts via 307 redirection to |
| 523 // |redirect_target_url| (cross-site from |form_url|). |
| 524 EXPECT_TRUE(NavigateToURL(shell(), form_url)); |
| 525 |
| 526 // Submit the form. |
| 527 TestNavigationObserver form_post_observer(shell()->web_contents(), 1); |
| 528 EXPECT_TRUE(ExecuteScript(shell()->web_contents(), |
| 529 "document.getElementById('form').submit();")); |
| 530 form_post_observer.Wait(); |
| 531 |
| 532 // Verify that we arrived at the expected, redirected location. |
| 533 EXPECT_EQ(redirect_target_url, |
| 534 shell()->web_contents()->GetLastCommittedURL()); |
| 535 |
| 536 // Verify that POST body got preserved by 307 redirect. This expectation |
| 537 // comes from: https://tools.ietf.org/html/rfc7231#section-6.4.7 |
| 538 std::string body; |
| 539 EXPECT_TRUE(ExecuteScriptAndExtractString( |
| 540 shell()->web_contents(), |
| 541 "window.domAutomationController.send(" |
| 542 "document.getElementsByTagName('pre')[0].innerText);", |
| 543 &body)); |
| 544 EXPECT_EQ("text=value\n", body); |
| 545 |
| 546 // Navigate to a page from yet another site. |
| 547 EXPECT_TRUE(NavigateToURL(shell(), page_to_go_back_from)); |
| 548 |
| 549 // Go back - this should resubmit form's post data. |
| 550 TestNavigationObserver back_nav_observer(shell()->web_contents(), 1); |
| 551 shell()->web_contents()->GetController().GoBack(); |
| 552 back_nav_observer.Wait(); |
| 553 |
| 554 // Again verify that we arrived at the expected, redirected location. |
| 555 EXPECT_EQ(redirect_target_url, |
| 556 shell()->web_contents()->GetLastCommittedURL()); |
| 557 |
| 558 // Again verify that POST body got preserved by 307 redirect. |
| 559 std::string body_after_back_navigation; |
| 560 EXPECT_TRUE(ExecuteScriptAndExtractString( |
| 561 shell()->web_contents(), |
| 562 "window.domAutomationController.send(" |
| 563 "document.getElementsByTagName('pre')[0].innerText);", |
| 564 &body_after_back_navigation)); |
| 565 EXPECT_EQ("text=value\n", body_after_back_navigation); |
| 566 } |
| 567 |
| 502 } // namespace content | 568 } // namespace content |
| OLD | NEW |