This source file includes following definitions.
- GetRedirects
- OnRedirectQueryComplete
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
#include "base/bind.h"
#include "base/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string16.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/test_timeouts.h"
#include "base/threading/platform_thread.h"
#include "chrome/browser/history/history_service.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/view_ids.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "net/base/net_util.h"
#include "net/test/spawned_test_server/spawned_test_server.h"
#include "ui/events/event_constants.h"
class RedirectTest : public InProcessBrowserTest {
public:
RedirectTest() {}
std::vector<GURL> GetRedirects(const GURL& url) {
HistoryService* history_service =
HistoryServiceFactory::GetForProfile(browser()->profile(),
Profile::EXPLICIT_ACCESS);
std::vector<GURL> rv;
history_service->QueryRedirectsFrom(
url, &consumer_,
base::Bind(&RedirectTest::OnRedirectQueryComplete,
base::Unretained(this), &rv));
content::RunMessageLoop();
return rv;
}
protected:
void OnRedirectQueryComplete(
std::vector<GURL>* rv,
HistoryService::Handle request_handle,
GURL from_url,
bool success,
history::RedirectList* redirects) {
for (size_t i = 0; i < redirects->size(); ++i)
rv->push_back(redirects->at(i));
base::MessageLoop::current()->PostTask(FROM_HERE,
base::MessageLoop::QuitClosure());
}
CancelableRequestConsumer consumer_;
};
IN_PROC_BROWSER_TEST_F(RedirectTest, Server) {
ASSERT_TRUE(test_server()->Start());
GURL final_url = test_server()->GetURL(std::string());
GURL first_url = test_server()->GetURL(
"server-redirect?" + final_url.spec());
ui_test_utils::NavigateToURL(browser(), first_url);
std::vector<GURL> redirects = GetRedirects(first_url);
ASSERT_EQ(1U, redirects.size());
EXPECT_EQ(final_url.spec(), redirects[0].spec());
}
IN_PROC_BROWSER_TEST_F(RedirectTest, Client) {
ASSERT_TRUE(test_server()->Start());
GURL final_url = test_server()->GetURL(std::string());
GURL first_url = test_server()->GetURL(
"client-redirect?" + final_url.spec());
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
browser(), first_url, 2);
std::vector<GURL> redirects = GetRedirects(first_url);
ASSERT_EQ(1U, redirects.size());
EXPECT_EQ(final_url.spec(), redirects[0].spec());
EXPECT_EQ(final_url,
browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
browser(), first_url, 2);
EXPECT_EQ(final_url,
browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
}
IN_PROC_BROWSER_TEST_F(RedirectTest, ClientEmptyReferer) {
ASSERT_TRUE(test_server()->Start());
GURL final_url = test_server()->GetURL(std::string());
ASSERT_TRUE(final_url.is_valid());
std::string file_redirect_contents = base::StringPrintf(
"<html>"
"<head></head>"
"<body onload=\"document.location='%s'\"></body>"
"</html>",
final_url.spec().c_str());
base::ScopedTempDir temp_directory;
ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
base::FilePath temp_file;
ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_directory.path(),
&temp_file));
ASSERT_EQ(static_cast<int>(file_redirect_contents.size()),
base::WriteFile(temp_file,
file_redirect_contents.data(),
file_redirect_contents.size()));
GURL first_url = net::FilePathToFileURL(temp_file);
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
browser(), first_url, 2);
std::vector<GURL> redirects = GetRedirects(first_url);
ASSERT_EQ(1U, redirects.size());
EXPECT_EQ(final_url.spec(), redirects[0].spec());
}
IN_PROC_BROWSER_TEST_F(RedirectTest, ClientCancelled) {
GURL first_url = ui_test_utils::GetTestUrl(
base::FilePath(),
base::FilePath().AppendASCII("cancelled_redirect_test.html"));
ui_test_utils::NavigateToURL(browser(), first_url);
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
content::TestNavigationObserver navigation_observer(web_contents);
content::SimulateMouseClick(web_contents, 0,
blink::WebMouseEvent::ButtonLeft);
navigation_observer.Wait();
std::vector<GURL> redirects = GetRedirects(first_url);
ASSERT_EQ(0U, redirects.size());
EXPECT_EQ("myanchor", web_contents->GetURL().ref());
}
IN_PROC_BROWSER_TEST_F(RedirectTest, ClientServerServer) {
ASSERT_TRUE(test_server()->Start());
GURL final_url = test_server()->GetURL(std::string());
GURL next_to_last = test_server()->GetURL(
"server-redirect?" + final_url.spec());
GURL second_url = test_server()->GetURL(
"server-redirect?" + next_to_last.spec());
GURL first_url = test_server()->GetURL(
"client-redirect?" + second_url.spec());
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
browser(), first_url, 2);
std::vector<GURL> redirects = GetRedirects(first_url);
ASSERT_EQ(3U, redirects.size());
EXPECT_EQ(second_url.spec(), redirects[0].spec());
EXPECT_EQ(next_to_last.spec(), redirects[1].spec());
EXPECT_EQ(final_url.spec(), redirects[2].spec());
}
IN_PROC_BROWSER_TEST_F(RedirectTest, ServerReference) {
ASSERT_TRUE(test_server()->Start());
const std::string ref("reference");
GURL final_url = test_server()->GetURL(std::string());
GURL initial_url = test_server()->GetURL(
"server-redirect?" + final_url.spec() + "#" + ref);
ui_test_utils::NavigateToURL(browser(), initial_url);
EXPECT_EQ(ref,
browser()->tab_strip_model()->GetActiveWebContents()->
GetURL().ref());
}
IN_PROC_BROWSER_TEST_F(RedirectTest, NoHttpToFile) {
ASSERT_TRUE(test_server()->Start());
GURL file_url = ui_test_utils::GetTestUrl(
base::FilePath(), base::FilePath().AppendASCII("http_to_file.html"));
GURL initial_url = test_server()->GetURL(
"client-redirect?" + file_url.spec());
ui_test_utils::NavigateToURL(browser(), initial_url);
EXPECT_NE(base::ASCIIToUTF16("File!"),
browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
}
IN_PROC_BROWSER_TEST_F(RedirectTest, ClientFragments) {
ASSERT_TRUE(test_server()->Start());
GURL first_url = ui_test_utils::GetTestUrl(
base::FilePath(), base::FilePath().AppendASCII("ref_redirect.html"));
ui_test_utils::NavigateToURL(browser(), first_url);
std::vector<GURL> redirects = GetRedirects(first_url);
EXPECT_EQ(1U, redirects.size());
EXPECT_EQ(first_url.spec() + "#myanchor", redirects[0].spec());
}
IN_PROC_BROWSER_TEST_F(RedirectTest,
DISABLED_ClientCancelledByNewNavigationAfterProvisionalLoad) {
ASSERT_TRUE(test_server()->Start());
GURL final_url = test_server()->GetURL("files/title2.html");
GURL slow = test_server()->GetURL("slow?60");
GURL first_url = test_server()->GetURL(
"client-redirect?" + slow.spec());
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
content::TestNavigationObserver observer(web_contents, 2);
ui_test_utils::NavigateToURLWithDisposition(
browser(), first_url, CURRENT_TAB, ui_test_utils::BROWSER_TEST_NONE);
ui_test_utils::NavigateToURLWithDisposition(
browser(), final_url, CURRENT_TAB, ui_test_utils::BROWSER_TEST_NONE);
observer.Wait();
EXPECT_EQ(base::ASCIIToUTF16("Title Of Awesomeness"),
browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
bool final_navigation_not_redirect = true;
std::vector<GURL> redirects = GetRedirects(first_url);
for (std::vector<GURL>::iterator it = redirects.begin();
it != redirects.end(); ++it) {
if (final_url.spec() == it->spec()) {
final_navigation_not_redirect = false;
break;
}
}
EXPECT_TRUE(final_navigation_not_redirect);
}