Commit 05a16884 authored by Matthew Cary's avatar Matthew Cary Committed by Commit Bot

Prerender: Add test confirming cookie behavior.

The behavior of NoStatePrefetch and cookies can be confusing. This adds
NoStatePrefetchBrowserTest.PrefetchCookie which documents and validates
that cookies are set on all prefetches.

Change-Id: I9c707989a07102286ae2d3e5cd19f2cbf7e29520
Reviewed-on: https://chromium-review.googlesource.com/691663
Commit-Queue: Matthew Cary <mattcary@chromium.org>
Reviewed-by: default avatarAlexandr Ilin <alexilin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#505359}
parent f836097b
......@@ -13,6 +13,7 @@
#include "base/strings/utf_string_conversions.h"
#include "base/task_scheduler/post_task.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/threading/platform_thread.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/history/history_test_utils.h"
#include "chrome/browser/prerender/prerender_handle.h"
......@@ -35,8 +36,11 @@
#include "content/public/test/browser_test_utils.h"
#include "net/base/escape.h"
#include "net/base/load_flags.h"
#include "net/cookies/cookie_store.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/request_handler_util.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
......@@ -44,6 +48,38 @@ using prerender::test_utils::DestructionWaiter;
using prerender::test_utils::RequestCounter;
using prerender::test_utils::TestPrerender;
namespace {
// Helper method that checks for NoStatePrefetchBrowserTest.PrefetchCookies and
// notifies the RunLoop when they're found.
void CheckCookiesForPrefetchCookieTest(base::RunLoop* loop,
GURL url,
net::CookieStore* cookie_store,
const net::CookieList& cookies) {
bool found_chocolate = false;
bool found_oatmeal = false;
for (const auto& c : cookies) {
if (c.Name() == "chocolate-chip") {
EXPECT_EQ("the-best", c.Value());
found_chocolate = true;
}
if (c.Name() == "oatmeal") {
EXPECT_EQ("sublime", c.Value());
found_oatmeal = true;
}
}
if (found_oatmeal && found_chocolate) {
loop->Quit();
} else {
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(250));
cookie_store->GetAllCookiesForURLAsync(
url, base::BindOnce(CheckCookiesForPrefetchCookieTest, loop, url,
cookie_store));
}
}
} // namespace
namespace prerender {
// These URLs used for test resources must be relative with the exception of
......@@ -65,6 +101,7 @@ const char kPrefetchScript2[] = "prerender/prefetch2.js";
const char kServiceWorkerLoader[] = "prerender/service_worker.html";
const char kPrefetchSubresourceRedirectPage[] =
"prerender/prefetch_subresource_redirect.html";
const char kPrefetchCookiePage[] = "prerender/cookie.html";
class NoStatePrefetchBrowserTest
: public test_utils::PrerenderInProcessBrowserTest,
......@@ -226,6 +263,33 @@ IN_PROC_BROWSER_TEST_P(NoStatePrefetchBrowserTest, PrefetchSimple) {
test_prerender->WaitForLoads(0);
}
// Check cookie loading for prefetched pages.
IN_PROC_BROWSER_TEST_P(NoStatePrefetchBrowserTest, PrefetchCookie) {
GURL url = src_server()->GetURL(MakeAbsolute(kPrefetchCookiePage));
std::unique_ptr<TestPrerender> test_prerender =
PrefetchFromURL(url, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED);
content::StoragePartition* storage_partition =
content::BrowserContext::GetStoragePartitionForSite(
current_browser()->profile(), url, false);
base::RunLoop loop;
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::BindOnce(
[](content::StoragePartition* storage, base::RunLoop* loop,
GURL url) {
net::CookieStore* cookie_store = storage->GetURLRequestContext()
->GetURLRequestContext()
->cookie_store();
cookie_store->GetAllCookiesForURLAsync(
url, base::BindOnce(CheckCookiesForPrefetchCookieTest, loop,
url, cookie_store));
},
storage_partition, &loop, url));
loop.Run();
// Will timeout if cookies aren't found.
}
// Check that the LOAD_PREFETCH flag is set.
IN_PROC_BROWSER_TEST_P(NoStatePrefetchBrowserTest, PrefetchLoadFlag) {
RequestCounter main_counter;
......
<html>
<!--
This page checks that cookies are set when prefetching happens.
-->
<head>
<title>Prefetch Page</title>
<script src="cookie.js" type="text/javascript"></script>
</head>
<body></body>
</html>
HTTP/1.1 200 OK
Content-Type: text/html
Set-Cookie: chocolate-chip=the-best
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var theScriptHasExecuted = true;
HTTP/1.1 200 OK
Content-Type: text/javascript
Set-Cookie: oatmeal=sublime
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment