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
content / test / navigation_simulator_unittest.cc [blame]
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/public/test/navigation_simulator.h"
#include <memory>
#include <string>
#include <tuple>
#include <utility>
#include "base/functional/bind.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/run_loop.h"
#include "base/test/test_simple_task_runner.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/navigation_throttle.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/test/test_navigation_throttle.h"
#include "content/public/test/test_navigation_throttle_inserter.h"
#include "content/test/task_runner_deferring_throttle.h"
#include "content/test/test_render_frame_host.h"
#include "content/test/test_render_view_host.h"
#include "content/test/test_web_contents.h"
#include "net/base/net_errors.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace content {
class NavigationSimulatorTest : public RenderViewHostImplTestHarness {};
class CancellingNavigationSimulatorTest
: public RenderViewHostImplTestHarness,
public WebContentsObserver,
public testing::WithParamInterface<
std::tuple<std::optional<TestNavigationThrottle::ThrottleMethod>,
TestNavigationThrottle::ResultSynchrony>> {
public:
CancellingNavigationSimulatorTest() {}
CancellingNavigationSimulatorTest(const CancellingNavigationSimulatorTest&) =
delete;
CancellingNavigationSimulatorTest& operator=(
const CancellingNavigationSimulatorTest&) = delete;
~CancellingNavigationSimulatorTest() override {}
void SetUp() override {
RenderViewHostImplTestHarness::SetUp();
contents()->GetPrimaryMainFrame()->InitializeRenderFrameIfNeeded();
Observe(RenderViewHostImplTestHarness::web_contents());
std::tie(cancel_time_, sync_) = GetParam();
simulator_ = NavigationSimulator::CreateRendererInitiated(
GURL("https://example.test"), main_rfh());
}
void TearDown() override {
EXPECT_TRUE(did_finish_navigation_);
RenderViewHostImplTestHarness::TearDown();
}
void DidStartNavigation(content::NavigationHandle* handle) override {
auto throttle = std::make_unique<TestNavigationThrottle>(handle);
throttle->SetCallback(
TestNavigationThrottle::WILL_FAIL_REQUEST,
base::BindRepeating(
&CancellingNavigationSimulatorTest::OnWillFailRequestCalled,
base::Unretained(this)));
if (cancel_time_.has_value()) {
throttle->SetResponse(cancel_time_.value(), sync_,
NavigationThrottle::CANCEL);
}
handle->RegisterThrottleForTesting(
std::unique_ptr<TestNavigationThrottle>(std::move(throttle)));
}
void DidFinishNavigation(content::NavigationHandle* handle) override {
did_finish_navigation_ = true;
}
void OnWillFailRequestCalled() { will_fail_request_called_ = true; }
std::optional<TestNavigationThrottle::ThrottleMethod> cancel_time_;
TestNavigationThrottle::ResultSynchrony sync_;
std::unique_ptr<NavigationSimulator> simulator_;
bool did_finish_navigation_ = false;
bool will_fail_request_called_ = false;
base::WeakPtrFactory<CancellingNavigationSimulatorTest> weak_ptr_factory_{
this};
};
class MethodCheckingNavigationSimulatorTest : public NavigationSimulatorTest,
public WebContentsObserver {
public:
MethodCheckingNavigationSimulatorTest() = default;
MethodCheckingNavigationSimulatorTest(
const MethodCheckingNavigationSimulatorTest&) = delete;
MethodCheckingNavigationSimulatorTest& operator=(
const MethodCheckingNavigationSimulatorTest&) = delete;
~MethodCheckingNavigationSimulatorTest() override = default;
void SetUp() override {
RenderViewHostImplTestHarness::SetUp();
contents()->GetPrimaryMainFrame()->InitializeRenderFrameIfNeeded();
Observe(RenderViewHostImplTestHarness::web_contents());
}
void DidFinishNavigation(content::NavigationHandle* handle) override {
did_finish_navigation_ = true;
is_post_ = handle->IsPost();
}
bool did_finish_navigation() { return did_finish_navigation_; }
bool is_post() { return is_post_; }
private:
// set upon DidFinishNavigation.
bool did_finish_navigation_ = false;
// Not valid until |did_finish_navigation_| is true;
bool is_post_ = false;
};
class ResponseHeadersCheckingNavigationSimulatorTest
: public NavigationSimulatorTest,
public WebContentsObserver {
public:
ResponseHeadersCheckingNavigationSimulatorTest() = default;
~ResponseHeadersCheckingNavigationSimulatorTest() override = default;
void SetUp() override {
RenderViewHostImplTestHarness::SetUp();
contents()->GetPrimaryMainFrame()->InitializeRenderFrameIfNeeded();
Observe(RenderViewHostImplTestHarness::web_contents());
}
void DidFinishNavigation(content::NavigationHandle* handle) override {
EXPECT_TRUE(handle->GetResponseHeaders()->HasHeaderValue("My-Test-Header",
"my-test-value"));
}
scoped_refptr<net::HttpResponseHeaders> response_headers_;
};
// Test that NavigationSimulator accurately commits about:blank if the browser
// requests a navigation to an empty URL.
TEST_F(NavigationSimulatorTest, EmptyURL) {
std::unique_ptr<NavigationSimulator> simulator =
NavigationSimulator::CreateBrowserInitiated(GURL(), contents());
simulator->Commit();
EXPECT_EQ(GURL(url::kAboutBlankURL), main_rfh()->GetLastCommittedURL());
}
TEST_F(NavigationSimulatorTest, AutoAdvanceOff) {
std::unique_ptr<NavigationSimulator> simulator =
NavigationSimulator::CreateRendererInitiated(
GURL("https://example.test/"), main_rfh());
simulator->SetAutoAdvance(false);
auto task_runner = base::MakeRefCounted<base::TestSimpleTaskRunner>();
auto* raw_runner = task_runner.get();
TestNavigationThrottleInserter throttle_inserter(
web_contents(),
base::BindRepeating(&TaskRunnerDeferringThrottle::Create,
std::move(task_runner), true /* defer_start */,
true /* defer_redirect */,
true /* defer_response */));
simulator->Start();
EXPECT_EQ(1u, raw_runner->NumPendingTasks());
EXPECT_TRUE(simulator->IsDeferred());
raw_runner->RunPendingTasks();
simulator->Wait();
simulator->Redirect(GURL("https://example.test/redirect"));
EXPECT_EQ(1u, raw_runner->NumPendingTasks());
EXPECT_TRUE(simulator->IsDeferred());
raw_runner->RunPendingTasks();
simulator->Wait();
simulator->ReadyToCommit();
EXPECT_EQ(1u, raw_runner->NumPendingTasks());
EXPECT_TRUE(simulator->IsDeferred());
raw_runner->RunPendingTasks();
simulator->Wait();
simulator->Commit();
}
TEST_F(MethodCheckingNavigationSimulatorTest, SetMethodPostWithRedirect) {
std::unique_ptr<NavigationSimulator> simulator =
NavigationSimulator::CreateRendererInitiated(
GURL("https://example.test/"), main_rfh());
simulator->SetMethod("POST");
simulator->Start();
simulator->Redirect(GURL("https://example.test/2.html"));
simulator->Commit();
ASSERT_TRUE(did_finish_navigation());
EXPECT_FALSE(is_post())
<< "If a POST request redirects, it should convert to a GET";
}
TEST_F(MethodCheckingNavigationSimulatorTest, SetMethodPost) {
std::unique_ptr<NavigationSimulator> simulator =
NavigationSimulator::CreateRendererInitiated(
GURL("https://example.test/"), main_rfh());
simulator->SetMethod("POST");
simulator->Start();
simulator->Commit();
ASSERT_TRUE(did_finish_navigation());
EXPECT_TRUE(is_post());
}
TEST_F(ResponseHeadersCheckingNavigationSimulatorTest, CheckResponseHeaders) {
std::unique_ptr<NavigationSimulator> simulator =
NavigationSimulator::CreateRendererInitiated(
GURL("https://example.test/"), main_rfh());
simulator->Start();
response_headers_ =
base::MakeRefCounted<net::HttpResponseHeaders>("HTTP/1.1 200 OK");
response_headers_->SetHeader("My-Test-Header", "my-test-value");
simulator->SetResponseHeaders(response_headers_);
simulator->ReadyToCommit();
simulator->Commit();
}
// Stress test the navigation simulator by having a navigation throttle cancel
// the navigation at various points in the flow, both synchronously and
// asynchronously.
TEST_P(CancellingNavigationSimulatorTest, Cancel) {
SCOPED_TRACE(::testing::Message()
<< "CancelTime: "
<< (cancel_time_.has_value() ? cancel_time_.value() : -1)
<< " ResultSynchrony: " << sync_);
simulator_->Start();
if (cancel_time_.has_value() &&
cancel_time_.value() == TestNavigationThrottle::WILL_START_REQUEST) {
EXPECT_EQ(NavigationThrottle::CANCEL,
simulator_->GetLastThrottleCheckResult());
// NavigationRequest::OnStartChecksComplete will post a task to finish the
// navigation, so pump the run loop here to ensure checks in TearDown()
// succeed.
base::RunLoop().RunUntilIdle();
return;
}
EXPECT_EQ(NavigationThrottle::PROCEED,
simulator_->GetLastThrottleCheckResult());
simulator_->Redirect(GURL("https://example.redirect"));
if (cancel_time_.has_value() &&
cancel_time_.value() == TestNavigationThrottle::WILL_REDIRECT_REQUEST) {
EXPECT_EQ(NavigationThrottle::CANCEL,
simulator_->GetLastThrottleCheckResult());
return;
}
EXPECT_EQ(NavigationThrottle::PROCEED,
simulator_->GetLastThrottleCheckResult());
simulator_->Commit();
if (cancel_time_.has_value() &&
cancel_time_.value() == TestNavigationThrottle::WILL_PROCESS_RESPONSE) {
EXPECT_EQ(NavigationThrottle::CANCEL,
simulator_->GetLastThrottleCheckResult());
return;
}
EXPECT_EQ(NavigationThrottle::PROCEED,
simulator_->GetLastThrottleCheckResult());
}
INSTANTIATE_TEST_SUITE_P(
CancelMethod,
CancellingNavigationSimulatorTest,
::testing::Combine(
::testing::Values(TestNavigationThrottle::WILL_START_REQUEST,
TestNavigationThrottle::WILL_REDIRECT_REQUEST,
TestNavigationThrottle::WILL_PROCESS_RESPONSE,
std::nullopt),
::testing::Values(TestNavigationThrottle::SYNCHRONOUS,
TestNavigationThrottle::ASYNCHRONOUS)));
// Create a version of the test class for parameterized testing.
using NavigationSimulatorTestCancelFail = CancellingNavigationSimulatorTest;
// Test canceling the simulated navigation.
TEST_P(NavigationSimulatorTestCancelFail, Fail) {
simulator_->Start();
simulator_->Fail(net::ERR_CERT_DATE_INVALID);
EXPECT_TRUE(will_fail_request_called_);
EXPECT_EQ(NavigationThrottle::CANCEL,
simulator_->GetLastThrottleCheckResult());
}
INSTANTIATE_TEST_SUITE_P(
Fail,
NavigationSimulatorTestCancelFail,
::testing::Combine(
::testing::Values(TestNavigationThrottle::WILL_FAIL_REQUEST),
::testing::Values(TestNavigationThrottle::SYNCHRONOUS,
TestNavigationThrottle::ASYNCHRONOUS)));
// Create a version of the test class for parameterized testing.
using NavigationSimulatorTestCancelFailErrAborted =
CancellingNavigationSimulatorTest;
// Test canceling the simulated navigation with net::ERR_ABORTED, which should
// not call WillFailRequest on the throttle.
TEST_P(NavigationSimulatorTestCancelFailErrAborted, Fail) {
simulator_->Start();
simulator_->Fail(net::ERR_ABORTED);
EXPECT_FALSE(will_fail_request_called_);
}
INSTANTIATE_TEST_SUITE_P(
Fail,
NavigationSimulatorTestCancelFailErrAborted,
::testing::Combine(
::testing::Values(TestNavigationThrottle::WILL_FAIL_REQUEST),
::testing::Values(TestNavigationThrottle::SYNCHRONOUS,
TestNavigationThrottle::ASYNCHRONOUS)));
} // namespace content