| 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 // Navigates the browser to server and client redirect pages and makes sure | 5 // Navigates the browser to server and client redirect pages and makes sure |
| 6 // that the correct redirects are reflected in the history database. Errors | 6 // that the correct redirects are reflected in the history database. Errors |
| 7 // here might indicate that WebKit changed the calls our glue layer gets in | 7 // here might indicate that WebKit changed the calls our glue layer gets in |
| 8 // the case of redirects. It may also mean problems with the history system. | 8 // the case of redirects. It may also mean problems with the history system. |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #include "chrome/browser/ui/browser.h" | 26 #include "chrome/browser/ui/browser.h" |
| 27 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 27 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 28 #include "chrome/browser/ui/view_ids.h" | 28 #include "chrome/browser/ui/view_ids.h" |
| 29 #include "chrome/test/base/in_process_browser_test.h" | 29 #include "chrome/test/base/in_process_browser_test.h" |
| 30 #include "chrome/test/base/ui_test_utils.h" | 30 #include "chrome/test/base/ui_test_utils.h" |
| 31 #include "components/history/core/browser/history_service.h" | 31 #include "components/history/core/browser/history_service.h" |
| 32 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
| 33 #include "content/public/test/browser_test_utils.h" | 33 #include "content/public/test/browser_test_utils.h" |
| 34 #include "content/public/test/test_navigation_observer.h" | 34 #include "content/public/test/test_navigation_observer.h" |
| 35 #include "net/base/filename_util.h" | 35 #include "net/base/filename_util.h" |
| 36 #include "net/test/spawned_test_server/spawned_test_server.h" | |
| 37 #include "ui/events/event_constants.h" | 36 #include "ui/events/event_constants.h" |
| 38 | 37 |
| 39 class RedirectTest : public InProcessBrowserTest { | 38 class RedirectTest : public InProcessBrowserTest { |
| 40 public: | 39 public: |
| 41 RedirectTest() {} | 40 RedirectTest() {} |
| 42 | 41 |
| 43 std::vector<GURL> GetRedirects(const GURL& url) { | 42 std::vector<GURL> GetRedirects(const GURL& url) { |
| 44 history::HistoryService* history_service = | 43 history::HistoryService* history_service = |
| 45 HistoryServiceFactory::GetForProfile( | 44 HistoryServiceFactory::GetForProfile( |
| 46 browser()->profile(), ServiceAccessType::EXPLICIT_ACCESS); | 45 browser()->profile(), ServiceAccessType::EXPLICIT_ACCESS); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 66 base::ThreadTaskRunnerHandle::Get()->PostTask( | 65 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 67 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 66 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 68 } | 67 } |
| 69 | 68 |
| 70 // Tracker for asynchronous history queries. | 69 // Tracker for asynchronous history queries. |
| 71 base::CancelableTaskTracker tracker_; | 70 base::CancelableTaskTracker tracker_; |
| 72 }; | 71 }; |
| 73 | 72 |
| 74 // Tests a single server redirect | 73 // Tests a single server redirect |
| 75 IN_PROC_BROWSER_TEST_F(RedirectTest, Server) { | 74 IN_PROC_BROWSER_TEST_F(RedirectTest, Server) { |
| 76 ASSERT_TRUE(test_server()->Start()); | 75 ASSERT_TRUE(embedded_test_server()->Start()); |
| 77 GURL final_url = test_server()->GetURL(std::string()); | 76 GURL final_url = embedded_test_server()->GetURL("/defaultresponse"); |
| 78 GURL first_url = test_server()->GetURL( | 77 GURL first_url = |
| 79 "server-redirect?" + final_url.spec()); | 78 embedded_test_server()->GetURL("/server-redirect?" + final_url.spec()); |
| 80 | 79 |
| 81 ui_test_utils::NavigateToURL(browser(), first_url); | 80 ui_test_utils::NavigateToURL(browser(), first_url); |
| 82 | 81 |
| 83 std::vector<GURL> redirects = GetRedirects(first_url); | 82 std::vector<GURL> redirects = GetRedirects(first_url); |
| 84 | 83 |
| 85 ASSERT_EQ(1U, redirects.size()); | 84 ASSERT_EQ(1U, redirects.size()); |
| 86 EXPECT_EQ(final_url.spec(), redirects[0].spec()); | 85 EXPECT_EQ(final_url.spec(), redirects[0].spec()); |
| 87 } | 86 } |
| 88 | 87 |
| 89 // Tests a single client redirect. | 88 // Tests a single client redirect. |
| 90 IN_PROC_BROWSER_TEST_F(RedirectTest, Client) { | 89 IN_PROC_BROWSER_TEST_F(RedirectTest, Client) { |
| 91 ASSERT_TRUE(test_server()->Start()); | 90 ASSERT_TRUE(embedded_test_server()->Start()); |
| 92 | 91 |
| 93 GURL final_url = test_server()->GetURL(std::string()); | 92 GURL final_url = embedded_test_server()->GetURL("/defaultresponse"); |
| 94 GURL first_url = test_server()->GetURL( | 93 GURL first_url = |
| 95 "client-redirect?" + final_url.spec()); | 94 embedded_test_server()->GetURL("/client-redirect?" + final_url.spec()); |
| 96 | 95 |
| 97 // The client redirect appears as two page visits in the browser. | 96 // The client redirect appears as two page visits in the browser. |
| 98 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | 97 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| 99 browser(), first_url, 2); | 98 browser(), first_url, 2); |
| 100 | 99 |
| 101 std::vector<GURL> redirects = GetRedirects(first_url); | 100 std::vector<GURL> redirects = GetRedirects(first_url); |
| 102 | 101 |
| 103 ASSERT_EQ(1U, redirects.size()); | 102 ASSERT_EQ(1U, redirects.size()); |
| 104 EXPECT_EQ(final_url.spec(), redirects[0].spec()); | 103 EXPECT_EQ(final_url.spec(), redirects[0].spec()); |
| 105 | 104 |
| 106 // The address bar should display the final URL. | 105 // The address bar should display the final URL. |
| 107 EXPECT_EQ(final_url, | 106 EXPECT_EQ(final_url, |
| 108 browser()->tab_strip_model()->GetActiveWebContents()->GetURL()); | 107 browser()->tab_strip_model()->GetActiveWebContents()->GetURL()); |
| 109 | 108 |
| 110 // Navigate one more time. | 109 // Navigate one more time. |
| 111 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | 110 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| 112 browser(), first_url, 2); | 111 browser(), first_url, 2); |
| 113 | 112 |
| 114 // The address bar should still display the final URL. | 113 // The address bar should still display the final URL. |
| 115 EXPECT_EQ(final_url, | 114 EXPECT_EQ(final_url, |
| 116 browser()->tab_strip_model()->GetActiveWebContents()->GetURL()); | 115 browser()->tab_strip_model()->GetActiveWebContents()->GetURL()); |
| 117 } | 116 } |
| 118 | 117 |
| 119 // http://code.google.com/p/chromium/issues/detail?id=62772 | 118 // http://code.google.com/p/chromium/issues/detail?id=62772 |
| 120 IN_PROC_BROWSER_TEST_F(RedirectTest, ClientEmptyReferer) { | 119 IN_PROC_BROWSER_TEST_F(RedirectTest, ClientEmptyReferer) { |
| 121 ASSERT_TRUE(test_server()->Start()); | 120 ASSERT_TRUE(embedded_test_server()->Start()); |
| 122 | 121 |
| 123 // Create the file contents, which will do a redirect to the | 122 // Create the file contents, which will do a redirect to the |
| 124 // test server. | 123 // test server. |
| 125 GURL final_url = test_server()->GetURL(std::string()); | 124 GURL final_url = embedded_test_server()->GetURL("/defaultresponse"); |
| 126 ASSERT_TRUE(final_url.is_valid()); | 125 ASSERT_TRUE(final_url.is_valid()); |
| 127 std::string file_redirect_contents = base::StringPrintf( | 126 std::string file_redirect_contents = base::StringPrintf( |
| 128 "<html>" | 127 "<html>" |
| 129 "<head></head>" | 128 "<head></head>" |
| 130 "<body onload=\"document.location='%s'\"></body>" | 129 "<body onload=\"document.location='%s'\"></body>" |
| 131 "</html>", | 130 "</html>", |
| 132 final_url.spec().c_str()); | 131 final_url.spec().c_str()); |
| 133 | 132 |
| 134 // Write the contents to a temporary file. | 133 // Write the contents to a temporary file. |
| 135 base::ScopedTempDir temp_directory; | 134 base::ScopedTempDir temp_directory; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 | 176 |
| 178 // There should be no redirects from first_url, because the anchor location | 177 // There should be no redirects from first_url, because the anchor location |
| 179 // change that occurs should not be flagged as a redirect and the meta-refresh | 178 // change that occurs should not be flagged as a redirect and the meta-refresh |
| 180 // won't have fired yet. | 179 // won't have fired yet. |
| 181 ASSERT_EQ(0U, redirects.size()); | 180 ASSERT_EQ(0U, redirects.size()); |
| 182 EXPECT_EQ("myanchor", web_contents->GetURL().ref()); | 181 EXPECT_EQ("myanchor", web_contents->GetURL().ref()); |
| 183 } | 182 } |
| 184 | 183 |
| 185 // Tests a client->server->server redirect | 184 // Tests a client->server->server redirect |
| 186 IN_PROC_BROWSER_TEST_F(RedirectTest, ClientServerServer) { | 185 IN_PROC_BROWSER_TEST_F(RedirectTest, ClientServerServer) { |
| 187 ASSERT_TRUE(test_server()->Start()); | 186 ASSERT_TRUE(embedded_test_server()->Start()); |
| 188 | 187 |
| 189 GURL final_url = test_server()->GetURL(std::string()); | 188 GURL final_url = embedded_test_server()->GetURL("/defaultresponse"); |
| 190 GURL next_to_last = test_server()->GetURL( | 189 GURL next_to_last = |
| 191 "server-redirect?" + final_url.spec()); | 190 embedded_test_server()->GetURL("/server-redirect?" + final_url.spec()); |
| 192 GURL second_url = test_server()->GetURL( | 191 GURL second_url = |
| 193 "server-redirect?" + next_to_last.spec()); | 192 embedded_test_server()->GetURL("/server-redirect?" + next_to_last.spec()); |
| 194 GURL first_url = test_server()->GetURL( | 193 GURL first_url = |
| 195 "client-redirect?" + second_url.spec()); | 194 embedded_test_server()->GetURL("/client-redirect?" + second_url.spec()); |
| 196 | 195 |
| 197 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | 196 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| 198 browser(), first_url, 2); | 197 browser(), first_url, 2); |
| 199 | 198 |
| 200 std::vector<GURL> redirects = GetRedirects(first_url); | 199 std::vector<GURL> redirects = GetRedirects(first_url); |
| 201 ASSERT_EQ(3U, redirects.size()); | 200 ASSERT_EQ(3U, redirects.size()); |
| 202 EXPECT_EQ(second_url.spec(), redirects[0].spec()); | 201 EXPECT_EQ(second_url.spec(), redirects[0].spec()); |
| 203 EXPECT_EQ(next_to_last.spec(), redirects[1].spec()); | 202 EXPECT_EQ(next_to_last.spec(), redirects[1].spec()); |
| 204 EXPECT_EQ(final_url.spec(), redirects[2].spec()); | 203 EXPECT_EQ(final_url.spec(), redirects[2].spec()); |
| 205 } | 204 } |
| 206 | 205 |
| 207 // Tests that the "#reference" gets preserved across server redirects. | 206 // Tests that the "#reference" gets preserved across server redirects. |
| 208 IN_PROC_BROWSER_TEST_F(RedirectTest, ServerReference) { | 207 IN_PROC_BROWSER_TEST_F(RedirectTest, ServerReference) { |
| 209 ASSERT_TRUE(test_server()->Start()); | 208 ASSERT_TRUE(embedded_test_server()->Start()); |
| 210 | 209 |
| 211 const std::string ref("reference"); | 210 const std::string ref("reference"); |
| 212 | 211 |
| 213 GURL final_url = test_server()->GetURL(std::string()); | 212 GURL final_url = embedded_test_server()->GetURL("/defaultresponse"); |
| 214 GURL initial_url = test_server()->GetURL( | 213 GURL initial_url = embedded_test_server()->GetURL( |
| 215 "server-redirect?" + final_url.spec() + "#" + ref); | 214 "/server-redirect?" + final_url.spec() + "#" + ref); |
| 216 | 215 |
| 217 ui_test_utils::NavigateToURL(browser(), initial_url); | 216 ui_test_utils::NavigateToURL(browser(), initial_url); |
| 218 | 217 |
| 219 EXPECT_EQ(ref, | 218 EXPECT_EQ(ref, |
| 220 browser()->tab_strip_model()->GetActiveWebContents()-> | 219 browser()->tab_strip_model()->GetActiveWebContents()-> |
| 221 GetURL().ref()); | 220 GetURL().ref()); |
| 222 } | 221 } |
| 223 | 222 |
| 224 // Test that redirect from http:// to file:// : | 223 // Test that redirect from http:// to file:// : |
| 225 // A) does not crash the browser or confuse the redirect chain, see bug 1080873 | 224 // A) does not crash the browser or confuse the redirect chain, see bug 1080873 |
| 226 // B) does not take place. | 225 // B) does not take place. |
| 227 // | 226 // |
| 228 // Flaky on XP and Vista, http://crbug.com/69390. | 227 // Flaky on XP and Vista, http://crbug.com/69390. |
| 229 IN_PROC_BROWSER_TEST_F(RedirectTest, NoHttpToFile) { | 228 IN_PROC_BROWSER_TEST_F(RedirectTest, NoHttpToFile) { |
| 230 ASSERT_TRUE(test_server()->Start()); | 229 ASSERT_TRUE(embedded_test_server()->Start()); |
| 231 GURL file_url = ui_test_utils::GetTestUrl( | 230 GURL file_url = ui_test_utils::GetTestUrl( |
| 232 base::FilePath(), base::FilePath().AppendASCII("http_to_file.html")); | 231 base::FilePath(), base::FilePath().AppendASCII("http_to_file.html")); |
| 233 | 232 |
| 234 GURL initial_url = test_server()->GetURL( | 233 GURL initial_url = |
| 235 "client-redirect?" + file_url.spec()); | 234 embedded_test_server()->GetURL("/client-redirect?" + file_url.spec()); |
| 236 | 235 |
| 237 ui_test_utils::NavigateToURL(browser(), initial_url); | 236 ui_test_utils::NavigateToURL(browser(), initial_url); |
| 238 // We make sure the title doesn't match the title from the file, because the | 237 // We make sure the title doesn't match the title from the file, because the |
| 239 // nav should not have taken place. | 238 // nav should not have taken place. |
| 240 EXPECT_NE(base::ASCIIToUTF16("File!"), | 239 EXPECT_NE(base::ASCIIToUTF16("File!"), |
| 241 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle()); | 240 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle()); |
| 242 } | 241 } |
| 243 | 242 |
| 244 // Ensures that non-user initiated location changes (within page) are | 243 // Ensures that non-user initiated location changes (within page) are |
| 245 // flagged as client redirects. See bug 1139823. | 244 // flagged as client redirects. See bug 1139823. |
| 246 IN_PROC_BROWSER_TEST_F(RedirectTest, ClientFragments) { | 245 IN_PROC_BROWSER_TEST_F(RedirectTest, ClientFragments) { |
| 247 ASSERT_TRUE(test_server()->Start()); | 246 ASSERT_TRUE(embedded_test_server()->Start()); |
| 248 GURL first_url = ui_test_utils::GetTestUrl( | 247 GURL first_url = ui_test_utils::GetTestUrl( |
| 249 base::FilePath(), base::FilePath().AppendASCII("ref_redirect.html")); | 248 base::FilePath(), base::FilePath().AppendASCII("ref_redirect.html")); |
| 250 ui_test_utils::NavigateToURL(browser(), first_url); | 249 ui_test_utils::NavigateToURL(browser(), first_url); |
| 251 std::vector<GURL> redirects = GetRedirects(first_url); | 250 std::vector<GURL> redirects = GetRedirects(first_url); |
| 252 EXPECT_EQ(1U, redirects.size()); | 251 EXPECT_EQ(1U, redirects.size()); |
| 253 EXPECT_EQ(first_url.spec() + "#myanchor", redirects[0].spec()); | 252 EXPECT_EQ(first_url.spec() + "#myanchor", redirects[0].spec()); |
| 254 } | 253 } |
| 255 | 254 |
| 256 // TODO(timsteele): This is disabled because our current testserver can't | 255 // TODO(timsteele): This is disabled because our current testserver can't |
| 257 // handle multiple requests in parallel, making it hang on the first request | 256 // handle multiple requests in parallel, making it hang on the first request |
| 258 // to /slow?60. It's unable to serve our second request for files/title2.html | 257 // to /slow?60. It's unable to serve our second request for /title2.html |
| 259 // until /slow? completes, which doesn't give the desired behavior. We could | 258 // until /slow? completes, which doesn't give the desired behavior. We could |
| 260 // alternatively load the second page from disk, but we would need to start | 259 // alternatively load the second page from disk, but we would need to start |
| 261 // the browser for this testcase with --process-per-tab, and I don't think | 260 // the browser for this testcase with --process-per-tab, and I don't think |
| 262 // we can do this at test-case-level granularity at the moment. | 261 // we can do this at test-case-level granularity at the moment. |
| 263 // http://crbug.com/45056 | 262 // http://crbug.com/45056 |
| 264 IN_PROC_BROWSER_TEST_F(RedirectTest, | 263 IN_PROC_BROWSER_TEST_F(RedirectTest, |
| 265 DISABLED_ClientCancelledByNewNavigationAfterProvisionalLoad) { | 264 DISABLED_ClientCancelledByNewNavigationAfterProvisionalLoad) { |
| 266 // We want to initiate a second navigation after the provisional load for | 265 // We want to initiate a second navigation after the provisional load for |
| 267 // the client redirect destination has started, but before this load is | 266 // the client redirect destination has started, but before this load is |
| 268 // committed. To achieve this, we tell the browser to load a slow page, | 267 // committed. To achieve this, we tell the browser to load a slow page, |
| 269 // which causes it to start a provisional load, and while it is waiting | 268 // which causes it to start a provisional load, and while it is waiting |
| 270 // for the response (which means it hasn't committed the load for the client | 269 // for the response (which means it hasn't committed the load for the client |
| 271 // redirect destination page yet), we issue a new navigation request. | 270 // redirect destination page yet), we issue a new navigation request. |
| 272 ASSERT_TRUE(test_server()->Start()); | 271 ASSERT_TRUE(embedded_test_server()->Start()); |
| 273 | 272 |
| 274 GURL final_url = test_server()->GetURL("files/title2.html"); | 273 GURL final_url = embedded_test_server()->GetURL("/title2.html"); |
| 275 GURL slow = test_server()->GetURL("slow?60"); | 274 GURL slow = embedded_test_server()->GetURL("/slow?60"); |
| 276 GURL first_url = test_server()->GetURL( | 275 GURL first_url = |
| 277 "client-redirect?" + slow.spec()); | 276 embedded_test_server()->GetURL("/client-redirect?" + slow.spec()); |
| 278 | 277 |
| 279 content::WebContents* web_contents = | 278 content::WebContents* web_contents = |
| 280 browser()->tab_strip_model()->GetActiveWebContents(); | 279 browser()->tab_strip_model()->GetActiveWebContents(); |
| 281 content::TestNavigationObserver observer(web_contents, 2); | 280 content::TestNavigationObserver observer(web_contents, 2); |
| 282 | 281 |
| 283 ui_test_utils::NavigateToURLWithDisposition( | 282 ui_test_utils::NavigateToURLWithDisposition( |
| 284 browser(), first_url, CURRENT_TAB, ui_test_utils::BROWSER_TEST_NONE); | 283 browser(), first_url, CURRENT_TAB, ui_test_utils::BROWSER_TEST_NONE); |
| 285 // We don't sleep here - the first navigation won't have been committed yet | 284 // We don't sleep here - the first navigation won't have been committed yet |
| 286 // because we told the server to wait a minute. This means the browser has | 285 // because we told the server to wait a minute. This means the browser has |
| 287 // started it's provisional load for the client redirect destination page but | 286 // started it's provisional load for the client redirect destination page but |
| 288 // hasn't completed. Our time is now! | 287 // hasn't completed. Our time is now! |
| 289 ui_test_utils::NavigateToURLWithDisposition( | 288 ui_test_utils::NavigateToURLWithDisposition( |
| 290 browser(), final_url, CURRENT_TAB, ui_test_utils::BROWSER_TEST_NONE); | 289 browser(), final_url, CURRENT_TAB, ui_test_utils::BROWSER_TEST_NONE); |
| 291 observer.Wait(); | 290 observer.Wait(); |
| 292 | 291 |
| 293 // Check to make sure the navigation did in fact take place and we are | 292 // Check to make sure the navigation did in fact take place and we are |
| 294 // at the expected page. | 293 // at the expected page. |
| 295 EXPECT_EQ(base::ASCIIToUTF16("Title Of Awesomeness"), | 294 EXPECT_EQ(base::ASCIIToUTF16("Title Of Awesomeness"), |
| 296 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle()); | 295 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle()); |
| 297 | 296 |
| 298 bool final_navigation_not_redirect = true; | 297 bool final_navigation_not_redirect = true; |
| 299 std::vector<GURL> redirects = GetRedirects(first_url); | 298 std::vector<GURL> redirects = GetRedirects(first_url); |
| 300 // Check to make sure our request for files/title2.html doesn't get flagged | 299 // Check to make sure our request for /title2.html doesn't get flagged |
| 301 // as a client redirect from the first (/client-redirect?) page. | 300 // as a client redirect from the first (/client-redirect?) page. |
| 302 for (std::vector<GURL>::iterator it = redirects.begin(); | 301 for (std::vector<GURL>::iterator it = redirects.begin(); |
| 303 it != redirects.end(); ++it) { | 302 it != redirects.end(); ++it) { |
| 304 if (final_url.spec() == it->spec()) { | 303 if (final_url.spec() == it->spec()) { |
| 305 final_navigation_not_redirect = false; | 304 final_navigation_not_redirect = false; |
| 306 break; | 305 break; |
| 307 } | 306 } |
| 308 } | 307 } |
| 309 EXPECT_TRUE(final_navigation_not_redirect); | 308 EXPECT_TRUE(final_navigation_not_redirect); |
| 310 } | 309 } |
| OLD | NEW |