This source file includes following definitions.
- SetUpOnMainThread
- OnDownloadCreated
- GetMockURL
- CheckTitleTest
- GetPopupTitle
- GetCookies
- got_downloads
- 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
- 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
- NoContentResponseHandler
- 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
- HandleRedirectRequest
- IN_PROC_BROWSER_TEST_F
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "content/browser/download/download_manager_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "content/shell/browser/shell.h"
#include "content/shell/browser/shell_content_browser_client.h"
#include "content/shell/browser/shell_network_delegate.h"
#include "content/test/net/url_request_failed_job.h"
#include "content/test/net/url_request_mock_http_job.h"
#include "net/base/net_errors.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
using base::ASCIIToUTF16;
namespace content {
class ResourceDispatcherHostBrowserTest : public ContentBrowserTest,
public DownloadManager::Observer {
public:
ResourceDispatcherHostBrowserTest() : got_downloads_(false) {}
protected:
virtual void SetUpOnMainThread() OVERRIDE {
base::FilePath path = GetTestFilePath("", "");
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&URLRequestMockHTTPJob::AddUrlHandler, path));
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&URLRequestFailedJob::AddUrlHandler));
}
virtual void OnDownloadCreated(
DownloadManager* manager,
DownloadItem* item) OVERRIDE {
if (!got_downloads_)
got_downloads_ = !!manager->InProgressCount();
}
GURL GetMockURL(const std::string& file) {
return URLRequestMockHTTPJob::GetMockUrl(
base::FilePath().AppendASCII(file));
}
void CheckTitleTest(const GURL& url,
const std::string& expected_title) {
base::string16 expected_title16(ASCIIToUTF16(expected_title));
TitleWatcher title_watcher(shell()->web_contents(), expected_title16);
NavigateToURL(shell(), url);
EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle());
}
bool GetPopupTitle(const GURL& url, base::string16* title) {
NavigateToURL(shell(), url);
ShellAddedObserver new_shell_observer;
if (!ExecuteScript(shell()->web_contents(), "OpenPopup();"))
return false;
Shell* new_shell = new_shell_observer.GetShell();
*title = new_shell->web_contents()->GetTitle();
return true;
}
std::string GetCookies(const GURL& url) {
return content::GetCookies(
shell()->web_contents()->GetBrowserContext(), url);
}
bool got_downloads() const { return got_downloads_; }
private:
bool got_downloads_;
};
IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, DynamicTitle1) {
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
GURL url(embedded_test_server()->GetURL("/dynamic1.html"));
base::string16 title;
ASSERT_TRUE(GetPopupTitle(url, &title));
EXPECT_TRUE(StartsWith(title, ASCIIToUTF16("My Popup Title"), true))
<< "Actual title: " << title;
}
IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, DynamicTitle2) {
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
GURL url(embedded_test_server()->GetURL("/dynamic2.html"));
base::string16 title;
ASSERT_TRUE(GetPopupTitle(url, &title));
EXPECT_TRUE(StartsWith(title, ASCIIToUTF16("My Dynamic Title"), true))
<< "Actual title: " << title;
}
IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest,
SniffHTMLWithNoContentType) {
CheckTitleTest(GetMockURL("content-sniffer-test0.html"),
"Content Sniffer Test 0");
}
IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest,
RespectNoSniffDirective) {
CheckTitleTest(GetMockURL("nosniff-test.html"),
"mock.http/nosniff-test.html");
}
IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest,
DoNotSniffHTMLFromTextPlain) {
CheckTitleTest(GetMockURL("content-sniffer-test1.html"),
"mock.http/content-sniffer-test1.html");
}
IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest,
DoNotSniffHTMLFromImageGIF) {
CheckTitleTest(GetMockURL("content-sniffer-test2.html"),
"mock.http/content-sniffer-test2.html");
}
IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest,
SniffNoContentTypeNoData) {
BrowserContext::GetDownloadManager(
shell()->web_contents()->GetBrowserContext())->AddObserver(this);
CheckTitleTest(GetMockURL("content-sniffer-test3.html"),
"Content Sniffer Test 3");
EXPECT_EQ(1u, Shell::windows().size());
ASSERT_FALSE(got_downloads());
}
IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest,
ContentDispositionEmpty) {
CheckTitleTest(GetMockURL("content-disposition-empty.html"), "success");
}
IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest,
ContentDispositionInline) {
CheckTitleTest(GetMockURL("content-disposition-inline.html"), "success");
}
IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, SyncXMLHttpRequest) {
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
NavigateToURL(
shell(), embedded_test_server()->GetURL("/sync_xmlhttprequest.html"));
bool success = false;
EXPECT_TRUE(ExecuteScriptAndExtractBool(
shell()->web_contents(),
"window.domAutomationController.send(DidSyncRequestSucceed());",
&success));
EXPECT_TRUE(success);
}
IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest,
SyncXMLHttpRequest_Disallowed) {
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
NavigateToURL(
shell(),
embedded_test_server()->GetURL("/sync_xmlhttprequest_disallowed.html"));
bool success = false;
EXPECT_TRUE(ExecuteScriptAndExtractBool(
shell()->web_contents(),
"window.domAutomationController.send(DidSucceed());",
&success));
EXPECT_TRUE(success);
}
#if defined(OS_MACOSX)
#define MAYBE_SyncXMLHttpRequest_DuringUnload \
DISABLED_SyncXMLHttpRequest_DuringUnload
#else
#define MAYBE_SyncXMLHttpRequest_DuringUnload SyncXMLHttpRequest_DuringUnload
#endif
IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest,
MAYBE_SyncXMLHttpRequest_DuringUnload) {
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
BrowserContext::GetDownloadManager(
shell()->web_contents()->GetBrowserContext())->AddObserver(this);
CheckTitleTest(
embedded_test_server()->GetURL("/sync_xmlhttprequest_during_unload.html"),
"sync xhr on unload");
CheckTitleTest(
embedded_test_server()->GetURL("/title2.html"), "Title Of Awesomeness");
ASSERT_FALSE(got_downloads());
}
IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest,
CrossSiteOnunloadCookie) {
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
GURL url = embedded_test_server()->GetURL("/onunload_cookie.html");
CheckTitleTest(url, "set cookie on unload");
CheckTitleTest(GetMockURL("content-sniffer-test0.html"),
"Content Sniffer Test 0");
EXPECT_EQ("onunloadCookie=foo", GetCookies(url));
}
IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest,
DISABLED_CrossSiteImmediateLoadOnunloadCookie) {
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
GURL url = embedded_test_server()->GetURL("/onunload_cookie.html");
CheckTitleTest(url, "set cookie on unload");
NavigateToURL(shell(), GURL(kAboutBlankURL));
EXPECT_EQ("onunloadCookie=foo", GetCookies(url));
}
namespace {
scoped_ptr<net::test_server::HttpResponse> NoContentResponseHandler(
const std::string& path,
const net::test_server::HttpRequest& request) {
if (!StartsWithASCII(path, request.relative_url, true))
return scoped_ptr<net::test_server::HttpResponse>();
scoped_ptr<net::test_server::BasicHttpResponse> http_response(
new net::test_server::BasicHttpResponse);
http_response->set_code(net::HTTP_NO_CONTENT);
return http_response.PassAs<net::test_server::HttpResponse>();
}
}
IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest,
CrossSiteNoUnloadOn204) {
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
GURL url = embedded_test_server()->GetURL("/onunload_cookie.html");
CheckTitleTest(url, "set cookie on unload");
const char kNoContentPath[] = "/nocontent";
embedded_test_server()->RegisterRequestHandler(
base::Bind(&NoContentResponseHandler, kNoContentPath));
NavigateToURL(shell(), embedded_test_server()->GetURL(kNoContentPath));
EXPECT_EQ("", GetCookies(url));
}
#if !defined(OS_MACOSX)
IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, CrossSiteAfterCrash) {
NavigateToURL(shell(), GURL("about:blank"));
RenderProcessHostWatcher crash_observer(
shell()->web_contents(),
RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
NavigateToURL(shell(), GURL(kChromeUICrashURL));
crash_observer.Wait();
CheckTitleTest(GetMockURL("content-sniffer-test0.html"),
"Content Sniffer Test 0");
}
#endif
IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest,
CrossSiteNavigationNonBuffered) {
CheckTitleTest(GetMockURL("content-sniffer-test0.html"),
"Content Sniffer Test 0");
GURL url = GetTestUrl("", "title2.html");
CheckTitleTest(url, "Title Of Awesomeness");
}
IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest,
CrossSiteNavigationErrorPage) {
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
GURL url(embedded_test_server()->GetURL("/onunload_cookie.html"));
CheckTitleTest(url, "set cookie on unload");
GURL failed_url = URLRequestFailedJob::GetMockHttpUrl(
net::ERR_NAME_NOT_RESOLVED);
NavigateToURL(shell(), failed_url);
EXPECT_NE(ASCIIToUTF16("set cookie on unload"),
shell()->web_contents()->GetTitle());
EXPECT_EQ("onunloadCookie=foo", GetCookies(url));
base::string16 expected_title16(ASCIIToUTF16("Title Of Awesomeness"));
TitleWatcher title_watcher(shell()->web_contents(), expected_title16);
bool success;
GURL test_url(embedded_test_server()->GetURL("/title2.html"));
std::string redirect_script = "window.location='" +
test_url.possibly_invalid_spec() + "';" +
"window.domAutomationController.send(true);";
EXPECT_TRUE(ExecuteScriptAndExtractBool(
shell()->web_contents(),
redirect_script,
&success));
EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle());
}
IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest,
CrossSiteNavigationErrorPage2) {
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
GURL url(embedded_test_server()->GetURL("/title2.html"));
CheckTitleTest(url, "Title Of Awesomeness");
GURL failed_url = URLRequestFailedJob::GetMockHttpUrl(
net::ERR_NAME_NOT_RESOLVED);
NavigateToURL(shell(), failed_url);
EXPECT_NE(ASCIIToUTF16("Title Of Awesomeness"),
shell()->web_contents()->GetTitle());
NavigateToURL(shell(), failed_url);
EXPECT_NE(ASCIIToUTF16("Title Of Awesomeness"),
shell()->web_contents()->GetTitle());
}
IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest,
CrossOriginRedirectBlocked) {
CheckTitleTest(GetMockURL("cross-origin-redirect-blocked.html"),
"Title Of More Awesomeness");
}
IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest,
CrossSiteFailedRequest) {
NavigateToURL(shell(), GetTestUrl("", "simple_page.html"));
GURL broken_url("chrome://theme");
NavigateToURL(shell(), broken_url);
}
namespace {
scoped_ptr<net::test_server::HttpResponse> HandleRedirectRequest(
const std::string& request_path,
const net::test_server::HttpRequest& request) {
if (!StartsWithASCII(request.relative_url, request_path, true))
return scoped_ptr<net::test_server::HttpResponse>();
scoped_ptr<net::test_server::BasicHttpResponse> http_response(
new net::test_server::BasicHttpResponse);
http_response->set_code(net::HTTP_FOUND);
http_response->AddCustomHeader(
"Location", request.relative_url.substr(request_path.length()));
return http_response.PassAs<net::test_server::HttpResponse>();
}
}
IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, CookiePolicy) {
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
embedded_test_server()->RegisterRequestHandler(
base::Bind(&HandleRedirectRequest, "/redirect?"));
std::string set_cookie_url(base::StringPrintf(
"http://localhost:%d/set_cookie.html", embedded_test_server()->port()));
GURL url(embedded_test_server()->GetURL("/redirect?" + set_cookie_url));
ShellContentBrowserClient::SetSwapProcessesForRedirect(true);
ShellNetworkDelegate::SetAcceptAllCookies(false);
CheckTitleTest(url, "cookie set");
}
}