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
content / browser / preloading / prefetch / prefetch_match_resolver_unittest.cc [blame]
// Copyright 2024 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/browser/preloading/prefetch/prefetch_match_resolver.h"
#include <vector>
#include "content/browser/preloading/prefetch/prefetch_container.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace content {
namespace {
// Mock `PrefetchContainer` to test `CollectMatchCandidatesGeneric()`.
class MockContainer {
public:
struct Args {
blink::DocumentToken document_token;
GURL url;
PrefetchContainer::ServableState servable_state;
std::optional<net::HttpNoVarySearchData> no_vary_search_hint;
std::optional<net::HttpNoVarySearchData> no_vary_search_data;
};
explicit MockContainer(MockContainer::Args args)
: key_(PrefetchContainer::Key(args.document_token, args.url)),
servable_state_(args.servable_state),
no_vary_search_hint_(args.no_vary_search_hint),
no_vary_search_data_(args.no_vary_search_data),
prefetch_status_(PrefetchStatus::kPrefetchSuccessful) {}
~MockContainer() = default;
const GURL& GetURL() const { return key_.url(); }
PrefetchContainer::ServableState GetServableState(
base::TimeDelta cacheable_duration) const {
return servable_state_;
}
bool HasPrefetchStatus() const { return prefetch_status_.has_value(); }
PrefetchStatus GetPrefetchStatus() const {
DCHECK(prefetch_status_);
return prefetch_status_.value();
}
bool IsNoVarySearchHeaderMatch(const GURL& url) const {
const std::optional<net::HttpNoVarySearchData>& no_vary_search_data =
GetNoVarySearchData();
return no_vary_search_data &&
no_vary_search_data->AreEquivalent(url, GetURL());
}
bool ShouldWaitForNoVarySearchHeader(const GURL& url) const {
const std::optional<net::HttpNoVarySearchData>& no_vary_search_hint =
GetNoVarySearchHint();
// It's not trivial to implement `PrefetchContainer::GetNonRedirectHead()`.
// Use `servable_state_` instead.
bool simulate_get_non_redirect_head_is_null =
(servable_state_ != PrefetchContainer::ServableState::kServable);
return simulate_get_non_redirect_head_is_null && no_vary_search_hint &&
no_vary_search_hint->AreEquivalent(url, GetURL());
}
// We don't test on this property.
bool HasPrefetchBeenConsideredToServe() const { return false; }
// We don't test on this property.
bool IsDecoy() const { return false; }
void SetServingPageMetrics(base::WeakPtr<PrefetchServingPageMetricsContainer>
serving_page_metrics_container) {}
void UpdateServingPageMetrics() {}
const PrefetchContainer::Key& key() const { return key_; }
const std::optional<net::HttpNoVarySearchData>& GetNoVarySearchHint() const {
return no_vary_search_hint_;
}
const std::optional<net::HttpNoVarySearchData>& GetNoVarySearchData() const {
return no_vary_search_data_;
}
private:
PrefetchContainer::Key key_;
PrefetchContainer::ServableState servable_state_;
std::optional<net::HttpNoVarySearchData> no_vary_search_hint_;
std::optional<net::HttpNoVarySearchData> no_vary_search_data_;
std::optional<PrefetchStatus> prefetch_status_;
};
std::ostream& operator<<(std::ostream& ostream, const MockContainer&) {
return ostream;
}
class CollectMatchCandidatesTestHelper {
public:
CollectMatchCandidatesTestHelper() = default;
~CollectMatchCandidatesTestHelper() = default;
void Add(MockContainer::Args args) {
auto container = std::make_unique<MockContainer>(std::move(args));
owned_prefetches_[container->key()] = std::move(container);
}
std::vector<PrefetchContainer::Key> KeysOfCollectMatchCandidatesGeneric(
const PrefetchContainer::Key& navigated_key,
bool is_nav_prerender) {
std::vector<PrefetchContainer::Key> candidate_keys;
// We must bind the following value instead of using `std::get()` in `for`
// due to a lifetime issue before C++23:
// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2718r0.html
auto [candidates, _] = CollectMatchCandidatesGeneric(
owned_prefetches_, navigated_key, is_nav_prerender,
/*serving_page_metrics_container=*/nullptr);
for (const auto* container : candidates) {
candidate_keys.push_back(container->key());
}
return candidate_keys;
}
void Assert(const base::Location& location,
const PrefetchContainer::Key& navigated_key,
bool is_nav_prerender,
const std::vector<PrefetchContainer::Key>& candidate_keys) {
SCOPED_TRACE(::testing::Message()
<< "from \033[31m" << location.ToString() << "\033[39m");
EXPECT_THAT(
KeysOfCollectMatchCandidatesGeneric(navigated_key, is_nav_prerender),
testing::UnorderedElementsAreArray(candidate_keys));
}
private:
std::map<PrefetchContainer::Key, std::unique_ptr<MockContainer>>
owned_prefetches_;
};
TEST(CollectMatchCandidates, DistinguishesDocumentToken) {
using Key = PrefetchContainer::Key;
CollectMatchCandidatesTestHelper helper;
blink::DocumentToken document_token1;
blink::DocumentToken document_token2;
helper.Add({
.document_token = document_token1,
.url = GURL("https://a.example.com/"),
.servable_state = PrefetchContainer::ServableState::kServable,
});
helper.Add({
.document_token = document_token2,
.url = GURL("https://a.example.com/"),
.servable_state = PrefetchContainer::ServableState::kServable,
});
helper.Assert(FROM_HERE, Key(document_token1, GURL("https://a.example.com/")),
/*is_nav_prerender=*/false,
{Key(document_token1, GURL("https://a.example.com/"))});
helper.Assert(FROM_HERE, Key(std::nullopt, GURL("https://a.example.com/")),
/*is_nav_prerender=*/false, {});
}
TEST(CollectMatchCandidates, DistingushesUrl) {
using Key = PrefetchContainer::Key;
CollectMatchCandidatesTestHelper helper;
blink::DocumentToken document_token;
helper.Add({
.document_token = document_token,
.url = GURL("https://a.example.com/"),
.servable_state = PrefetchContainer::ServableState::kServable,
});
helper.Add({
.document_token = document_token,
.url = GURL("https://b.example.com/"),
.servable_state = PrefetchContainer::ServableState::kServable,
});
helper.Assert(FROM_HERE, Key(document_token, GURL("https://a.example.com/")),
/*is_nav_prerender=*/false,
{Key(document_token, GURL("https://a.example.com/"))});
helper.Assert(FROM_HERE, Key(document_token, GURL("https://c.example.com/")),
/*is_nav_prerender=*/false, {});
}
TEST(CollectMatchCandidates, RejectsNotServable) {
using Key = PrefetchContainer::Key;
CollectMatchCandidatesTestHelper helper;
blink::DocumentToken document_token;
helper.Add({
.document_token = document_token,
.url = GURL("https://servable.example.com/"),
.servable_state = PrefetchContainer::ServableState::kServable,
});
helper.Add({
.document_token = document_token,
.url = GURL("https://not-servable.example.com/"),
.servable_state = PrefetchContainer::ServableState::kNotServable,
});
helper.Add({
.document_token = document_token,
.url = GURL("https://should-block-until-head-received.example.com/"),
.servable_state =
PrefetchContainer::ServableState::kShouldBlockUntilHeadReceived,
});
helper.Assert(FROM_HERE,
Key(document_token, GURL("https://servable.example.com/")),
/*is_nav_prerender=*/false,
{Key(document_token, GURL("https://servable.example.com/"))});
helper.Assert(FROM_HERE,
Key(document_token, GURL("https://not-servable.example.com/")),
/*is_nav_prerender=*/false, {});
helper.Assert(
FROM_HERE,
Key(document_token,
GURL("https://should-block-until-head-received.example.com/")),
/*is_nav_prerender=*/false,
{Key(document_token,
GURL("https://should-block-until-head-received.example.com/"))});
}
TEST(CollectMatchCandidates,
IncludesShouldBlockUntilEligibilityGotIfIsLikelyAheadOfPrerender) {
using Key = PrefetchContainer::Key;
CollectMatchCandidatesTestHelper helper;
blink::DocumentToken document_token;
helper.Add({
.document_token = document_token,
.url = GURL("https://prerender.example.com/"),
.servable_state =
PrefetchContainer::ServableState::kShouldBlockUntilEligibilityGot,
});
helper.Add({
.document_token = document_token,
.url = GURL("https://not-prerender.example.com/"),
.servable_state =
PrefetchContainer::ServableState::kShouldBlockUntilEligibilityGot,
});
helper.Assert(FROM_HERE,
Key(document_token, GURL("https://prerender.example.com/")),
/*is_nav_prerender=*/true,
{Key(document_token, GURL("https://prerender.example.com/"))});
helper.Assert(FROM_HERE,
Key(document_token, GURL("https://not-prerender.example.com/")),
/*is_nav_prerender=*/false, {});
}
TEST(CollectMatchCandidates, ChecksNoVarySearchHintAndHeader) {
using Key = PrefetchContainer::Key;
CollectMatchCandidatesTestHelper helper;
blink::DocumentToken document_token;
helper.Add({
.document_token = document_token,
.url = GURL("https://a.example.com/"),
.servable_state = PrefetchContainer::ServableState::kServable,
.no_vary_search_hint = std::nullopt,
.no_vary_search_data = std::nullopt,
});
helper.Add({
.document_token = document_token,
.url = GURL("https://a.example.com/?distinguish=true"),
.servable_state = PrefetchContainer::ServableState::kServable,
.no_vary_search_hint = std::nullopt,
.no_vary_search_data = std::nullopt,
});
helper.Add({
.document_token = document_token,
.url = GURL("https://a.example.com/?ignore=onlyHeader"),
.servable_state = PrefetchContainer::ServableState::kServable,
.no_vary_search_hint = std::nullopt,
.no_vary_search_data =
net::HttpNoVarySearchData::CreateFromNoVaryParams({"ignore"}, true),
});
helper.Add({
.document_token = document_token,
.url = GURL("https://a.example.com/?ignore=onlyHint"),
.servable_state =
PrefetchContainer::ServableState::kShouldBlockUntilHeadReceived,
.no_vary_search_hint =
net::HttpNoVarySearchData::CreateFromNoVaryParams({"ignore"}, true),
.no_vary_search_data = std::nullopt,
});
helper.Add({
.document_token = document_token,
.url = GURL("https://a.example.com/?ignore=bothHintAndHeader"),
.servable_state =
PrefetchContainer::ServableState::kShouldBlockUntilHeadReceived,
.no_vary_search_hint =
net::HttpNoVarySearchData::CreateFromNoVaryParams({"ignore"}, true),
.no_vary_search_data =
net::HttpNoVarySearchData::CreateFromNoVaryParams({"ignore"}, true),
});
helper.Add({
.document_token = document_token,
.url = GURL("https://a.example.com/?distinguish=hintButContradictHeader"),
.servable_state = PrefetchContainer::ServableState::kServable,
.no_vary_search_hint = net::HttpNoVarySearchData::CreateFromNoVaryParams(
{"distinguish"}, true),
.no_vary_search_data = std::nullopt,
});
helper.Assert(
FROM_HERE, Key(document_token, GURL("https://a.example.com/")),
/*is_nav_prerender=*/false,
{
Key(document_token, GURL("https://a.example.com/")),
Key(document_token, GURL("https://a.example.com/?ignore=onlyHeader")),
Key(document_token, GURL("https://a.example.com/?ignore=onlyHint")),
Key(document_token,
GURL("https://a.example.com/?ignore=bothHintAndHeader")),
});
}
} // namespace
} // namespace content