1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/navigation_interception/intercept_navigation_throttle.h"
#include <memory>
#include <vector>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/test/bind.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/navigation_throttle.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/test_navigation_throttle_inserter.h"
#include "content/public/test/test_renderer_host.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
using base::test::RunOnceCallback;
using base::test::RunOnceCallbackRepeatedly;
using content::NavigationThrottle;
using testing::_;
using testing::AllOf;
using testing::Eq;
using testing::Ne;
using testing::Not;
using testing::Property;
using testing::Return;
namespace navigation_interception {
namespace {
const char kTestUrl[] = "http://www.test.com/";
// The MS C++ compiler complains about not being able to resolve which url()
// method (const or non-const) to use if we use the Property matcher to check
// the return value of the NavigationParams::url() method.
// It is possible to suppress the error by specifying the types directly but
// that results in very ugly syntax, which is why these custom matchers are
// used instead.
MATCHER(NavigationHandleUrlIsTest, "") {
return arg->GetURL() == kTestUrl;
}
MATCHER(IsPost, "") {
return arg->IsPost();
}
} // namespace
// MockInterceptCallbackReceiver ----------------------------------------------
class MockInterceptCallbackReceiver {
public:
MOCK_METHOD3(
ShouldIgnoreNavigation,
void(content::NavigationHandle* handle,
bool should_run_async,
InterceptNavigationThrottle::ResultCallback result_callback));
};
// InterceptNavigationThrottleTest ------------------------------------
class InterceptNavigationThrottleTest
: public content::RenderViewHostTestHarness,
public testing::WithParamInterface<bool> {
public:
InterceptNavigationThrottleTest()
: mock_callback_receiver_(new MockInterceptCallbackReceiver()) {
if (GetParam()) {
scoped_feature_.InitAndEnableFeature(kAsyncCheck);
} else {
scoped_feature_.InitAndDisableFeature(kAsyncCheck);
}
}
void CreateAndAddThrottle(
InterceptNavigationThrottle::CheckCallback callback,
base::RepeatingClosure request_finish_closure,
content::NavigationThrottleRegistry& registry) {
std::unique_ptr<InterceptNavigationThrottle> throttle =
std::make_unique<InterceptNavigationThrottle>(
registry, callback,
navigation_interception::SynchronyMode::kAsync,
request_finish_closure);
throttle_ = throttle.get()->GetWeakPtrForTesting();
registry.AddThrottle(std::move(throttle));
}
std::unique_ptr<content::TestNavigationThrottleInserter>
CreateThrottleInserter() {
return std::make_unique<content::TestNavigationThrottleInserter>(
web_contents(),
base::BindRepeating(
&InterceptNavigationThrottleTest::CreateAndAddThrottle,
base::Unretained(this),
base::BindRepeating(
&MockInterceptCallbackReceiver::ShouldIgnoreNavigation,
base::Unretained(mock_callback_receiver_.get())),
request_finish_closure_.Get()));
}
NavigationThrottle::ThrottleCheckResult SimulateNavigation(
const GURL& url,
std::vector<GURL> redirect_chain,
bool is_post) {
auto throttle_inserter = CreateThrottleInserter();
std::unique_ptr<content::NavigationSimulator> simulator =
content::NavigationSimulator::CreateRendererInitiated(url, main_rfh());
auto failed = [](content::NavigationSimulator* sim) {
return sim->GetLastThrottleCheckResult().action() !=
NavigationThrottle::PROCEED;
};
if (is_post) {
simulator->SetMethod("POST");
}
simulator->Start();
if (failed(simulator.get())) {
return simulator->GetLastThrottleCheckResult();
}
for (const GURL& redirect_url : redirect_chain) {
simulator->Redirect(redirect_url);
if (failed(simulator.get())) {
return simulator->GetLastThrottleCheckResult();
}
}
simulator->Commit();
return simulator->GetLastThrottleCheckResult();
}
void OnCheckComplete(bool should_ignore) {
throttle_->OnCheckComplete(should_ignore);
}
base::test::ScopedFeatureList scoped_feature_;
std::unique_ptr<MockInterceptCallbackReceiver> mock_callback_receiver_;
base::MockRepeatingClosure request_finish_closure_;
base::WeakPtr<InterceptNavigationThrottle> throttle_;
};
TEST_P(InterceptNavigationThrottleTest, AsyncRequestCompletesWhenRequested) {
if (!GetParam()) {
GTEST_SKIP();
}
ON_CALL(request_finish_closure_, Run()).WillByDefault([this]() {
OnCheckComplete(false);
});
EXPECT_CALL(*mock_callback_receiver_,
ShouldIgnoreNavigation(NavigationHandleUrlIsTest(), _, _))
.Times(2);
EXPECT_CALL(request_finish_closure_, Run()).Times(2);
NavigationThrottle::ThrottleCheckResult result =
SimulateNavigation(GURL(kTestUrl), {GURL(kTestUrl)}, false);
EXPECT_EQ(NavigationThrottle::PROCEED, result);
}
TEST_P(InterceptNavigationThrottleTest, AsyncRequestDefersWhenRequested) {
if (!GetParam()) {
GTEST_SKIP();
}
EXPECT_CALL(*mock_callback_receiver_,
ShouldIgnoreNavigation(NavigationHandleUrlIsTest(), Eq(true), _))
.Times(2);
EXPECT_CALL(
*mock_callback_receiver_,
ShouldIgnoreNavigation(NavigationHandleUrlIsTest(), Eq(false), _));
EXPECT_CALL(request_finish_closure_, Run()).Times(2);
ON_CALL(*mock_callback_receiver_, ShouldIgnoreNavigation(_, Eq(false), _))
.WillByDefault(RunOnceCallback<2>(false));
auto throttle_inserter = CreateThrottleInserter();
std::unique_ptr<content::NavigationSimulator> simulator =
content::NavigationSimulator::CreateRendererInitiated(GURL(kTestUrl),
main_rfh());
simulator->Start();
EXPECT_EQ(NavigationThrottle::PROCEED,
simulator->GetLastThrottleCheckResult().action());
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindLambdaForTesting([&, this] {
EXPECT_TRUE(simulator->IsDeferred());
OnCheckComplete(false);
}));
simulator->Redirect(GURL(kTestUrl));
EXPECT_FALSE(simulator->IsDeferred());
EXPECT_EQ(NavigationThrottle::PROCEED,
simulator->GetLastThrottleCheckResult().action());
simulator->Redirect(GURL(kTestUrl));
EXPECT_FALSE(simulator->IsDeferred());
EXPECT_EQ(NavigationThrottle::PROCEED,
simulator->GetLastThrottleCheckResult().action());
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindLambdaForTesting([&, this] {
EXPECT_TRUE(simulator->IsDeferred());
OnCheckComplete(false);
}));
simulator->Commit();
EXPECT_EQ(NavigationThrottle::PROCEED,
simulator->GetLastThrottleCheckResult().action());
}
TEST_P(InterceptNavigationThrottleTest, AsyncRequestDefersTwice) {
if (!GetParam()) {
GTEST_SKIP();
}
EXPECT_CALL(*mock_callback_receiver_,
ShouldIgnoreNavigation(NavigationHandleUrlIsTest(), Eq(true), _))
.Times(1);
EXPECT_CALL(
*mock_callback_receiver_,
ShouldIgnoreNavigation(NavigationHandleUrlIsTest(), Eq(false), _));
EXPECT_CALL(request_finish_closure_, Run()).Times(1);
auto throttle_inserter = CreateThrottleInserter();
std::unique_ptr<content::NavigationSimulator> simulator =
content::NavigationSimulator::CreateRendererInitiated(GURL(kTestUrl),
main_rfh());
simulator->Start();
EXPECT_EQ(NavigationThrottle::PROCEED,
simulator->GetLastThrottleCheckResult().action());
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindLambdaForTesting([&, this] {
EXPECT_TRUE(simulator->IsDeferred());
OnCheckComplete(false);
EXPECT_TRUE(simulator->IsDeferred());
OnCheckComplete(false);
}));
simulator->Redirect(GURL(kTestUrl));
EXPECT_FALSE(simulator->IsDeferred());
EXPECT_EQ(NavigationThrottle::PROCEED,
simulator->GetLastThrottleCheckResult().action());
simulator->Commit();
EXPECT_EQ(NavigationThrottle::PROCEED,
simulator->GetLastThrottleCheckResult().action());
}
TEST_P(InterceptNavigationThrottleTest, RequestDefersWhenClientNotReady) {
if (GetParam()) {
GTEST_SKIP();
}
EXPECT_CALL(
*mock_callback_receiver_,
ShouldIgnoreNavigation(NavigationHandleUrlIsTest(), Eq(false), _));
EXPECT_CALL(request_finish_closure_, Run()).Times(0);
auto throttle_inserter = CreateThrottleInserter();
std::unique_ptr<content::NavigationSimulator> simulator =
content::NavigationSimulator::CreateRendererInitiated(GURL(kTestUrl),
main_rfh());
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindLambdaForTesting([&, this] {
EXPECT_TRUE(simulator->IsDeferred());
OnCheckComplete(false);
}));
simulator->Start();
EXPECT_EQ(NavigationThrottle::PROCEED,
simulator->GetLastThrottleCheckResult().action());
EXPECT_FALSE(simulator->IsDeferred());
simulator->Commit();
EXPECT_EQ(NavigationThrottle::PROCEED,
simulator->GetLastThrottleCheckResult().action());
}
TEST_P(InterceptNavigationThrottleTest,
RequestCompletesIfNavigationNotIgnored) {
ON_CALL(*mock_callback_receiver_, ShouldIgnoreNavigation(_, _, _))
.WillByDefault(RunOnceCallback<2>(false));
EXPECT_CALL(*mock_callback_receiver_,
ShouldIgnoreNavigation(NavigationHandleUrlIsTest(), _, _));
EXPECT_CALL(request_finish_closure_, Run()).Times(0);
NavigationThrottle::ThrottleCheckResult result =
SimulateNavigation(GURL(kTestUrl), {}, false);
EXPECT_EQ(NavigationThrottle::PROCEED, result);
}
TEST_P(InterceptNavigationThrottleTest, RequestCancelledIfNavigationIgnored) {
ON_CALL(*mock_callback_receiver_, ShouldIgnoreNavigation(_, _, _))
.WillByDefault(RunOnceCallback<2>(true));
EXPECT_CALL(*mock_callback_receiver_,
ShouldIgnoreNavigation(NavigationHandleUrlIsTest(), _, _));
EXPECT_CALL(request_finish_closure_, Run()).Times(0);
NavigationThrottle::ThrottleCheckResult result =
SimulateNavigation(GURL(kTestUrl), {}, false);
EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, result);
}
TEST_P(InterceptNavigationThrottleTest, CallbackIsPostFalseForGet) {
EXPECT_CALL(*mock_callback_receiver_,
ShouldIgnoreNavigation(
AllOf(NavigationHandleUrlIsTest(), Not(IsPost())), _, _))
.WillOnce(RunOnceCallback<2>(false));
EXPECT_CALL(request_finish_closure_, Run()).Times(0);
NavigationThrottle::ThrottleCheckResult result =
SimulateNavigation(GURL(kTestUrl), {}, false);
EXPECT_EQ(NavigationThrottle::PROCEED, result);
}
TEST_P(InterceptNavigationThrottleTest, CallbackIsPostTrueForPost) {
EXPECT_CALL(*mock_callback_receiver_,
ShouldIgnoreNavigation(
AllOf(NavigationHandleUrlIsTest(), IsPost()), _, _))
.WillOnce(RunOnceCallback<2>(false));
EXPECT_CALL(request_finish_closure_, Run()).Times(0);
NavigationThrottle::ThrottleCheckResult result =
SimulateNavigation(GURL(kTestUrl), {}, true);
EXPECT_EQ(NavigationThrottle::PROCEED, result);
}
TEST_P(InterceptNavigationThrottleTest,
CallbackIsPostFalseForPostConvertedToGetBy302) {
EXPECT_CALL(*mock_callback_receiver_,
ShouldIgnoreNavigation(
AllOf(NavigationHandleUrlIsTest(), IsPost()), _, _))
.WillOnce(RunOnceCallback<2>(false));
EXPECT_CALL(*mock_callback_receiver_,
ShouldIgnoreNavigation(
AllOf(NavigationHandleUrlIsTest(), Not(IsPost())), _, _))
.WillOnce(RunOnceCallback<2>(false));
EXPECT_CALL(request_finish_closure_, Run()).Times(0);
NavigationThrottle::ThrottleCheckResult result =
SimulateNavigation(GURL(kTestUrl), {GURL(kTestUrl)}, true);
EXPECT_EQ(NavigationThrottle::PROCEED, result);
}
// Ensure POST navigations are cancelled before the start.
TEST_P(InterceptNavigationThrottleTest, PostNavigationCancelledAtStart) {
ON_CALL(*mock_callback_receiver_, ShouldIgnoreNavigation(_, _, _))
.WillByDefault(RunOnceCallback<2>(true));
auto throttle_inserter = CreateThrottleInserter();
std::unique_ptr<content::NavigationSimulator> simulator =
content::NavigationSimulator::CreateRendererInitiated(GURL(kTestUrl),
main_rfh());
simulator->SetMethod("POST");
simulator->Start();
auto result = simulator->GetLastThrottleCheckResult();
EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, result);
}
INSTANTIATE_TEST_SUITE_P(All,
InterceptNavigationThrottleTest,
testing::Values(true, false));
} // namespace navigation_interception
|