Roll src/third_party/WebKit 605a979:06cb9e9 (svn 202556:202558)
[chromium-blink-merge.git] / components / url_matcher / url_matcher_unittest.cc
blob328c633c311207d04610f29744eab87c61b0d791
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/url_matcher/url_matcher.h"
7 #include "base/strings/string_util.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "url/gurl.h"
11 namespace url_matcher {
14 // URLMatcherCondition
17 TEST(URLMatcherConditionTest, Constructors) {
18 StringPattern pattern("example.com", 1);
19 URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, &pattern);
20 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m1.criterion());
21 EXPECT_EQ(&pattern, m1.string_pattern());
23 URLMatcherCondition m2;
24 m2 = m1;
25 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m2.criterion());
26 EXPECT_EQ(&pattern, m2.string_pattern());
28 URLMatcherCondition m3(m1);
29 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m3.criterion());
30 EXPECT_EQ(&pattern, m3.string_pattern());
33 TEST(URLMatcherSchemeFilter, TestMatching) {
34 URLMatcherSchemeFilter filter1("https");
35 std::vector<std::string> filter2_content;
36 filter2_content.push_back("http");
37 filter2_content.push_back("https");
38 URLMatcherSchemeFilter filter2(filter2_content);
40 GURL matching_url("https://www.foobar.com");
41 GURL non_matching_url("http://www.foobar.com");
42 EXPECT_TRUE(filter1.IsMatch(matching_url));
43 EXPECT_FALSE(filter1.IsMatch(non_matching_url));
44 EXPECT_TRUE(filter2.IsMatch(matching_url));
45 EXPECT_TRUE(filter2.IsMatch(non_matching_url));
48 TEST(URLMatcherPortFilter, TestMatching) {
49 std::vector<URLMatcherPortFilter::Range> ranges;
50 ranges.push_back(URLMatcherPortFilter::CreateRange(80, 90));
51 ranges.push_back(URLMatcherPortFilter::CreateRange(8080));
52 URLMatcherPortFilter filter(ranges);
53 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com")));
54 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:80")));
55 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:81")));
56 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:90")));
57 EXPECT_TRUE(filter.IsMatch(GURL("http://www.example.com:8080")));
58 EXPECT_FALSE(filter.IsMatch(GURL("http://www.example.com:79")));
59 EXPECT_FALSE(filter.IsMatch(GURL("http://www.example.com:91")));
60 EXPECT_FALSE(filter.IsMatch(GURL("https://www.example.com")));
63 TEST(URLMatcherConditionTest, IsFullURLCondition) {
64 StringPattern pattern("example.com", 1);
65 EXPECT_FALSE(URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX,
66 &pattern).IsFullURLCondition());
68 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::HOST_CONTAINS,
69 &pattern).IsFullURLCondition());
70 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::PATH_CONTAINS,
71 &pattern).IsFullURLCondition());
72 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::QUERY_CONTAINS,
73 &pattern).IsFullURLCondition());
75 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_PREFIX,
76 &pattern).IsFullURLCondition());
77 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_SUFFIX,
78 &pattern).IsFullURLCondition());
79 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_CONTAINS,
80 &pattern).IsFullURLCondition());
81 EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_EQUALS,
82 &pattern).IsFullURLCondition());
85 TEST(URLMatcherConditionTest, IsMatch) {
86 GURL url1("http://www.example.com/www.foobar.com/index.html");
87 GURL url2("http://www.foobar.com/example.com/index.html");
89 StringPattern pattern("example.com", 1);
90 URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, &pattern);
92 std::set<StringPattern::ID> matching_patterns;
94 // matches = {0} --> matcher did not indicate that m1 was a match.
95 matching_patterns.insert(0);
96 EXPECT_FALSE(m1.IsMatch(matching_patterns, url1));
98 // matches = {0, 1} --> matcher did indicate that m1 was a match.
99 matching_patterns.insert(1);
100 EXPECT_TRUE(m1.IsMatch(matching_patterns, url1));
102 // For m2 we use a HOST_CONTAINS test, which requires a post-validation
103 // whether the match reported by the SubstringSetMatcher occurs really
104 // in the correct url component.
105 URLMatcherCondition m2(URLMatcherCondition::HOST_CONTAINS, &pattern);
106 EXPECT_TRUE(m2.IsMatch(matching_patterns, url1));
107 EXPECT_FALSE(m2.IsMatch(matching_patterns, url2));
110 TEST(URLMatcherConditionTest, Comparison) {
111 StringPattern p1("foobar.com", 1);
112 StringPattern p2("foobar.com", 2);
113 // The first component of each test is expected to be < than the second.
114 URLMatcherCondition test_smaller[][2] = {
115 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1),
116 URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, &p1)},
117 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1),
118 URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p2)},
119 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, NULL),
120 URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p2)},
121 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1),
122 URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, NULL)},
124 for (size_t i = 0; i < arraysize(test_smaller); ++i) {
125 EXPECT_TRUE(test_smaller[i][0] < test_smaller[i][1])
126 << "Test " << i << " of test_smaller failed";
127 EXPECT_FALSE(test_smaller[i][1] < test_smaller[i][0])
128 << "Test " << i << " of test_smaller failed";
130 URLMatcherCondition test_equal[][2] = {
131 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1),
132 URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1)},
133 {URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, NULL),
134 URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, NULL)},
136 for (size_t i = 0; i < arraysize(test_equal); ++i) {
137 EXPECT_FALSE(test_equal[i][0] < test_equal[i][1])
138 << "Test " << i << " of test_equal failed";
139 EXPECT_FALSE(test_equal[i][1] < test_equal[i][0])
140 << "Test " << i << " of test_equal failed";
145 // URLMatcherConditionFactory
148 namespace {
150 bool Matches(const URLMatcherCondition& condition, std::string text) {
151 return text.find(condition.string_pattern()->pattern()) !=
152 std::string::npos;
155 } // namespace
157 TEST(URLMatcherConditionFactoryTest, GURLCharacterSet) {
158 // GURL guarantees that neither domain, nor path, nor query may contain
159 // non ASCII-7 characters. We test this here, because a change to this
160 // guarantee breaks this implementation horribly.
161 GURL url("http://www.föö.com/föö?föö#föö");
162 EXPECT_TRUE(base::IsStringASCII(url.host()));
163 EXPECT_TRUE(base::IsStringASCII(url.path()));
164 EXPECT_TRUE(base::IsStringASCII(url.query()));
165 EXPECT_FALSE(base::IsStringASCII(url.ref()));
168 TEST(URLMatcherConditionFactoryTest, Criteria) {
169 URLMatcherConditionFactory factory;
170 EXPECT_EQ(URLMatcherCondition::HOST_PREFIX,
171 factory.CreateHostPrefixCondition("foo").criterion());
172 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX,
173 factory.CreateHostSuffixCondition("foo").criterion());
174 EXPECT_EQ(URLMatcherCondition::HOST_CONTAINS,
175 factory.CreateHostContainsCondition("foo").criterion());
176 EXPECT_EQ(URLMatcherCondition::HOST_EQUALS,
177 factory.CreateHostEqualsCondition("foo").criterion());
178 EXPECT_EQ(URLMatcherCondition::PATH_PREFIX,
179 factory.CreatePathPrefixCondition("foo").criterion());
180 EXPECT_EQ(URLMatcherCondition::PATH_SUFFIX,
181 factory.CreatePathSuffixCondition("foo").criterion());
182 EXPECT_EQ(URLMatcherCondition::PATH_CONTAINS,
183 factory.CreatePathContainsCondition("foo").criterion());
184 EXPECT_EQ(URLMatcherCondition::PATH_EQUALS,
185 factory.CreatePathEqualsCondition("foo").criterion());
186 EXPECT_EQ(URLMatcherCondition::QUERY_PREFIX,
187 factory.CreateQueryPrefixCondition("foo").criterion());
188 EXPECT_EQ(URLMatcherCondition::QUERY_SUFFIX,
189 factory.CreateQuerySuffixCondition("foo").criterion());
190 EXPECT_EQ(URLMatcherCondition::QUERY_CONTAINS,
191 factory.CreateQueryContainsCondition("foo").criterion());
192 EXPECT_EQ(URLMatcherCondition::QUERY_EQUALS,
193 factory.CreateQueryEqualsCondition("foo").criterion());
194 EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX_PATH_PREFIX,
195 factory.CreateHostSuffixPathPrefixCondition("foo",
196 "bar").criterion());
197 EXPECT_EQ(URLMatcherCondition::HOST_EQUALS_PATH_PREFIX,
198 factory.CreateHostEqualsPathPrefixCondition("foo",
199 "bar").criterion());
200 EXPECT_EQ(URLMatcherCondition::URL_PREFIX,
201 factory.CreateURLPrefixCondition("foo").criterion());
202 EXPECT_EQ(URLMatcherCondition::URL_SUFFIX,
203 factory.CreateURLSuffixCondition("foo").criterion());
204 EXPECT_EQ(URLMatcherCondition::URL_CONTAINS,
205 factory.CreateURLContainsCondition("foo").criterion());
206 EXPECT_EQ(URLMatcherCondition::URL_EQUALS,
207 factory.CreateURLEqualsCondition("foo").criterion());
208 EXPECT_EQ(URLMatcherCondition::URL_MATCHES,
209 factory.CreateURLMatchesCondition("foo").criterion());
212 TEST(URLMatcherConditionFactoryTest, TestSingletonProperty) {
213 URLMatcherConditionFactory factory;
214 URLMatcherCondition c1 = factory.CreateHostEqualsCondition("www.google.com");
215 URLMatcherCondition c2 = factory.CreateHostEqualsCondition("www.google.com");
216 EXPECT_EQ(c1.criterion(), c2.criterion());
217 EXPECT_EQ(c1.string_pattern(), c2.string_pattern());
218 URLMatcherCondition c3 = factory.CreateHostEqualsCondition("www.google.de");
219 EXPECT_EQ(c2.criterion(), c3.criterion());
220 EXPECT_NE(c2.string_pattern(), c3.string_pattern());
221 EXPECT_NE(c2.string_pattern()->id(), c3.string_pattern()->id());
222 EXPECT_NE(c2.string_pattern()->pattern(),
223 c3.string_pattern()->pattern());
224 URLMatcherCondition c4 = factory.CreateURLMatchesCondition("www.google.com");
225 URLMatcherCondition c5 = factory.CreateURLContainsCondition("www.google.com");
226 // Regex patterns and substring patterns do not share IDs.
227 EXPECT_EQ(c5.string_pattern()->pattern(), c4.string_pattern()->pattern());
228 EXPECT_NE(c5.string_pattern(), c4.string_pattern());
229 EXPECT_NE(c5.string_pattern()->id(), c4.string_pattern()->id());
231 // Check that all StringPattern singletons are freed if we call
232 // ForgetUnusedPatterns.
233 StringPattern::ID old_id_1 = c1.string_pattern()->id();
234 StringPattern::ID old_id_4 = c4.string_pattern()->id();
235 factory.ForgetUnusedPatterns(std::set<StringPattern::ID>());
236 EXPECT_TRUE(factory.IsEmpty());
237 URLMatcherCondition c6 = factory.CreateHostEqualsCondition("www.google.com");
238 EXPECT_NE(old_id_1, c6.string_pattern()->id());
239 URLMatcherCondition c7 = factory.CreateURLMatchesCondition("www.google.com");
240 EXPECT_NE(old_id_4, c7.string_pattern()->id());
243 TEST(URLMatcherConditionFactoryTest, TestComponentSearches) {
244 URLMatcherConditionFactory factory;
245 GURL gurl("https://www.google.com:1234/webhp?sourceid=chrome-instant&ie=UTF-8"
246 "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome");
247 std::string url = factory.CanonicalizeURLForComponentSearches(gurl);
248 GURL gurl2("https://www.google.com.:1234/webhp?sourceid=chrome-instant"
249 "&ie=UTF-8&ion=1#hl=en&output=search&sclient=psy-ab"
250 "&q=chrome%20is%20awesome");
251 std::string url2 = factory.CanonicalizeURLForComponentSearches(gurl2);
253 // Test host component.
254 EXPECT_TRUE(Matches(factory.CreateHostPrefixCondition(std::string()), url));
255 EXPECT_TRUE(Matches(factory.CreateHostPrefixCondition("www.goog"), url));
256 EXPECT_TRUE(
257 Matches(factory.CreateHostPrefixCondition("www.google.com"), url));
258 EXPECT_TRUE(
259 Matches(factory.CreateHostPrefixCondition(".www.google.com"), url));
260 EXPECT_FALSE(Matches(factory.CreateHostPrefixCondition("google.com"), url));
261 EXPECT_FALSE(
262 Matches(factory.CreateHostPrefixCondition("www.google.com/"), url));
263 EXPECT_FALSE(Matches(factory.CreateHostPrefixCondition("webhp"), url));
265 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition(std::string()), url));
266 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition(std::string()), url2));
267 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition("com"), url));
268 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition("com"), url2));
269 EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition(".com"), url));
270 EXPECT_TRUE(
271 Matches(factory.CreateHostSuffixCondition("www.google.com"), url));
272 EXPECT_TRUE(
273 Matches(factory.CreateHostSuffixCondition(".www.google.com"), url));
274 EXPECT_TRUE(
275 Matches(factory.CreateHostSuffixCondition(".www.google.com"), url2));
276 EXPECT_TRUE(
277 Matches(factory.CreateHostSuffixCondition(".www.google.com."), url));
278 EXPECT_FALSE(Matches(factory.CreateHostSuffixCondition("www"), url));
279 EXPECT_FALSE(
280 Matches(factory.CreateHostSuffixCondition("www.google.com/"), url));
281 EXPECT_FALSE(Matches(factory.CreateHostSuffixCondition("webhp"), url));
283 EXPECT_FALSE(Matches(factory.CreateHostEqualsCondition(std::string()), url));
284 EXPECT_FALSE(Matches(factory.CreateHostEqualsCondition("www"), url));
285 EXPECT_TRUE(
286 Matches(factory.CreateHostEqualsCondition("www.google.com"), url));
287 EXPECT_TRUE(
288 Matches(factory.CreateHostEqualsCondition("www.google.com"), url2));
289 EXPECT_FALSE(
290 Matches(factory.CreateHostEqualsCondition("www.google.com/"), url));
291 EXPECT_TRUE(
292 Matches(factory.CreateHostEqualsCondition(".www.google.com."), url));
293 EXPECT_TRUE(
294 Matches(factory.CreateHostEqualsCondition(".www.google.com."), url2));
296 // Test path component.
297 EXPECT_TRUE(Matches(factory.CreatePathPrefixCondition(std::string()), url));
298 EXPECT_TRUE(Matches(factory.CreatePathPrefixCondition("/web"), url));
299 EXPECT_TRUE(Matches(factory.CreatePathPrefixCondition("/webhp"), url));
300 EXPECT_FALSE(Matches(factory.CreatePathPrefixCondition("webhp"), url));
301 EXPECT_FALSE(Matches(factory.CreatePathPrefixCondition("/webhp?"), url));
302 EXPECT_FALSE(Matches(factory.CreatePathPrefixCondition("?sourceid"), url));
304 EXPECT_TRUE(Matches(factory.CreatePathSuffixCondition(std::string()), url));
305 EXPECT_TRUE(Matches(factory.CreatePathSuffixCondition("webhp"), url));
306 EXPECT_TRUE(Matches(factory.CreatePathSuffixCondition("/webhp"), url));
307 EXPECT_FALSE(Matches(factory.CreatePathSuffixCondition("/web"), url));
308 EXPECT_FALSE(Matches(factory.CreatePathSuffixCondition("/webhp?"), url));
310 EXPECT_TRUE(Matches(factory.CreatePathEqualsCondition("/webhp"), url));
311 EXPECT_FALSE(Matches(factory.CreatePathEqualsCondition("webhp"), url));
312 EXPECT_FALSE(Matches(factory.CreatePathEqualsCondition("/webhp?"), url));
313 EXPECT_FALSE(
314 Matches(factory.CreatePathEqualsCondition("www.google.com"), url));
317 // Test query component.
318 EXPECT_TRUE(Matches(factory.CreateQueryPrefixCondition(std::string()), url));
319 EXPECT_TRUE(Matches(factory.CreateQueryPrefixCondition("sourceid"), url));
320 // The '?' at the beginning is just ignored.
321 EXPECT_TRUE(Matches(factory.CreateQueryPrefixCondition("?sourceid"), url));
323 EXPECT_TRUE(Matches(factory.CreateQuerySuffixCondition(std::string()), url));
324 EXPECT_TRUE(Matches(factory.CreateQuerySuffixCondition("ion=1"), url));
325 EXPECT_FALSE(Matches(factory.CreateQuerySuffixCondition("www"), url));
326 // "Suffix" condition + pattern starting with '?' = "equals" condition.
327 EXPECT_FALSE(Matches(factory.CreateQuerySuffixCondition(
328 "?sourceid=chrome-instant&ie=UTF-8&ion="), url));
329 EXPECT_TRUE(Matches(factory.CreateQuerySuffixCondition(
330 "?sourceid=chrome-instant&ie=UTF-8&ion=1"), url));
332 EXPECT_FALSE(Matches(factory.CreateQueryEqualsCondition(
333 "?sourceid=chrome-instant&ie=UTF-8&ion="), url));
334 EXPECT_FALSE(Matches(factory.CreateQueryEqualsCondition(
335 "sourceid=chrome-instant&ie=UTF-8&ion="), url));
336 EXPECT_TRUE(Matches(factory.CreateQueryEqualsCondition(
337 "sourceid=chrome-instant&ie=UTF-8&ion=1"), url));
338 // The '?' at the beginning is just ignored.
339 EXPECT_TRUE(Matches(factory.CreateQueryEqualsCondition(
340 "?sourceid=chrome-instant&ie=UTF-8&ion=1"), url));
341 EXPECT_FALSE(
342 Matches(factory.CreateQueryEqualsCondition("www.google.com"), url));
345 // Test adjacent components
346 EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition(
347 "google.com", "/webhp"), url));
348 EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition(
349 "google.com", "/webhp"), url2));
350 EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition(
351 "google.com.", "/webhp"), url));
352 EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition(
353 "google.com.", "/webhp"), url2));
354 EXPECT_TRUE(Matches(
355 factory.CreateHostSuffixPathPrefixCondition(std::string(), "/webhp"),
356 url));
357 EXPECT_TRUE(Matches(
358 factory.CreateHostSuffixPathPrefixCondition("google.com", std::string()),
359 url));
360 EXPECT_FALSE(Matches(
361 factory.CreateHostSuffixPathPrefixCondition("www", std::string()), url));
363 EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition(
364 "www.google.com", "/webhp"), url));
365 EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition(
366 "www.google.com", "/webhp"), url2));
367 EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition(
368 ".www.google.com.", "/webhp"), url));
369 EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition(
370 ".www.google.com.", "/webhp"), url2));
371 EXPECT_FALSE(Matches(
372 factory.CreateHostEqualsPathPrefixCondition(std::string(), "/webhp"),
373 url));
374 EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition(
375 "www.google.com", std::string()),
376 url));
377 EXPECT_FALSE(Matches(
378 factory.CreateHostEqualsPathPrefixCondition("google.com", std::string()),
379 url));
382 TEST(URLMatcherConditionFactoryTest, TestFullSearches) {
383 // The Port 443 is stripped because it is the default port for https.
384 GURL gurl("https://www.google.com:443/webhp?sourceid=chrome-instant&ie=UTF-8"
385 "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome");
386 URLMatcherConditionFactory factory;
387 std::string url = factory.CanonicalizeURLForFullSearches(gurl);
389 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(std::string()), url));
390 EXPECT_TRUE(
391 Matches(factory.CreateURLPrefixCondition("https://www.goog"), url));
392 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(
393 "https://www.google.com"), url));
394 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(
395 "https://www.google.com/webhp?"), url));
396 EXPECT_FALSE(Matches(factory.CreateURLPrefixCondition(
397 "http://www.google.com"), url));
398 EXPECT_FALSE(Matches(factory.CreateURLPrefixCondition("webhp"), url));
400 EXPECT_TRUE(Matches(factory.CreateURLSuffixCondition(std::string()), url));
401 EXPECT_TRUE(Matches(factory.CreateURLSuffixCondition("ion=1"), url));
402 EXPECT_FALSE(Matches(factory.CreateURLSuffixCondition("www"), url));
404 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition(std::string()), url));
405 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("www.goog"), url));
406 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("webhp"), url));
407 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("?"), url));
408 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("sourceid"), url));
409 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("ion=1"), url));
410 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition(".www.goog"), url));
411 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition("foobar"), url));
412 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition("search"), url));
413 EXPECT_FALSE(Matches(factory.CreateURLContainsCondition(":443"), url));
415 EXPECT_TRUE(Matches(factory.CreateURLEqualsCondition(
416 "https://www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8&ion=1"),
417 url));
418 EXPECT_FALSE(
419 Matches(factory.CreateURLEqualsCondition("https://www.google.com"), url));
421 // Same as above but this time with a non-standard port.
422 gurl = GURL("https://www.google.com:1234/webhp?sourceid=chrome-instant&"
423 "ie=UTF-8&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20"
424 "awesome");
425 url = factory.CanonicalizeURLForFullSearches(gurl);
426 EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(
427 "https://www.google.com:1234/webhp?"), url));
428 EXPECT_TRUE(Matches(factory.CreateURLContainsCondition(":1234"), url));
432 // URLMatcherConditionSet
435 TEST(URLMatcherConditionSetTest, Constructor) {
436 URLMatcherConditionFactory factory;
437 URLMatcherCondition m1 = factory.CreateHostSuffixCondition("example.com");
438 URLMatcherCondition m2 = factory.CreatePathContainsCondition("foo");
440 std::set<URLMatcherCondition> conditions;
441 conditions.insert(m1);
442 conditions.insert(m2);
444 scoped_refptr<URLMatcherConditionSet> condition_set(
445 new URLMatcherConditionSet(1, conditions));
446 EXPECT_EQ(1, condition_set->id());
447 EXPECT_EQ(2u, condition_set->conditions().size());
450 TEST(URLMatcherConditionSetTest, Matching) {
451 GURL url1("http://www.example.com/foo?bar=1");
452 GURL url2("http://foo.example.com/index.html");
453 GURL url3("http://www.example.com:80/foo?bar=1");
454 GURL url4("http://www.example.com:8080/foo?bar=1");
456 URLMatcherConditionFactory factory;
457 URLMatcherCondition m1 = factory.CreateHostSuffixCondition("example.com");
458 URLMatcherCondition m2 = factory.CreatePathContainsCondition("foo");
460 std::set<URLMatcherCondition> conditions;
461 conditions.insert(m1);
462 conditions.insert(m2);
464 scoped_refptr<URLMatcherConditionSet> condition_set(
465 new URLMatcherConditionSet(1, conditions));
466 EXPECT_EQ(1, condition_set->id());
467 EXPECT_EQ(2u, condition_set->conditions().size());
469 std::set<StringPattern::ID> matching_patterns;
470 matching_patterns.insert(m1.string_pattern()->id());
471 EXPECT_FALSE(condition_set->IsMatch(matching_patterns, url1));
473 matching_patterns.insert(m2.string_pattern()->id());
474 EXPECT_TRUE(condition_set->IsMatch(matching_patterns, url1));
475 EXPECT_FALSE(condition_set->IsMatch(matching_patterns, url2));
477 // Test scheme filters.
478 scoped_refptr<URLMatcherConditionSet> condition_set2(
479 new URLMatcherConditionSet(1,
480 conditions,
481 scoped_ptr<URLMatcherSchemeFilter>(
482 new URLMatcherSchemeFilter("https")),
483 scoped_ptr<URLMatcherPortFilter>()));
484 EXPECT_FALSE(condition_set2->IsMatch(matching_patterns, url1));
485 scoped_refptr<URLMatcherConditionSet> condition_set3(
486 new URLMatcherConditionSet(1,
487 conditions,
488 scoped_ptr<URLMatcherSchemeFilter>(
489 new URLMatcherSchemeFilter("http")),
490 scoped_ptr<URLMatcherPortFilter>()));
491 EXPECT_TRUE(condition_set3->IsMatch(matching_patterns, url1));
493 // Test port filters.
494 std::vector<URLMatcherPortFilter::Range> ranges;
495 ranges.push_back(URLMatcherPortFilter::CreateRange(80));
496 scoped_ptr<URLMatcherPortFilter> filter(new URLMatcherPortFilter(ranges));
497 scoped_refptr<URLMatcherConditionSet> condition_set4(
498 new URLMatcherConditionSet(
499 1, conditions, scoped_ptr<URLMatcherSchemeFilter>(), filter.Pass()));
500 EXPECT_TRUE(condition_set4->IsMatch(matching_patterns, url1));
501 EXPECT_TRUE(condition_set4->IsMatch(matching_patterns, url3));
502 EXPECT_FALSE(condition_set4->IsMatch(matching_patterns, url4));
504 // Test regex patterns.
505 matching_patterns.clear();
506 URLMatcherCondition r1 = factory.CreateURLMatchesCondition("/fo?oo");
507 std::set<URLMatcherCondition> regex_conditions;
508 regex_conditions.insert(r1);
509 scoped_refptr<URLMatcherConditionSet> condition_set5(
510 new URLMatcherConditionSet(1, regex_conditions));
511 EXPECT_FALSE(condition_set5->IsMatch(matching_patterns, url1));
512 matching_patterns.insert(r1.string_pattern()->id());
513 EXPECT_TRUE(condition_set5->IsMatch(matching_patterns, url1));
515 regex_conditions.insert(m1);
516 scoped_refptr<URLMatcherConditionSet> condition_set6(
517 new URLMatcherConditionSet(1, regex_conditions));
518 EXPECT_FALSE(condition_set6->IsMatch(matching_patterns, url1));
519 matching_patterns.insert(m1.string_pattern()->id());
520 EXPECT_TRUE(condition_set6->IsMatch(matching_patterns, url1));
522 matching_patterns.clear();
523 regex_conditions.clear();
524 URLMatcherCondition r2 = factory.CreateOriginAndPathMatchesCondition("b[a]r");
525 regex_conditions.insert(r2);
526 scoped_refptr<URLMatcherConditionSet> condition_set7(
527 new URLMatcherConditionSet(1, regex_conditions));
528 EXPECT_FALSE(condition_set7->IsMatch(matching_patterns, url1));
529 matching_patterns.insert(r2.string_pattern()->id());
530 EXPECT_TRUE(condition_set7->IsMatch(matching_patterns, url1));
533 namespace {
535 bool IsQueryMatch(
536 const std::string& url_query,
537 const std::string& key,
538 URLQueryElementMatcherCondition::QueryElementType query_element_type,
539 const std::string& value,
540 URLQueryElementMatcherCondition::QueryValueMatchType query_value_match_type,
541 URLQueryElementMatcherCondition::Type match_type) {
542 URLMatcherConditionFactory factory;
544 URLMatcherCondition m1 = factory.CreateHostSuffixCondition("example.com");
545 URLMatcherCondition m2 = factory.CreatePathContainsCondition("foo");
546 URLMatcherConditionSet::Conditions conditions;
547 conditions.insert(m1);
548 conditions.insert(m2);
550 URLQueryElementMatcherCondition q1(key,
551 value,
552 query_value_match_type,
553 query_element_type,
554 match_type,
555 &factory);
556 URLMatcherConditionSet::QueryConditions query_conditions;
557 query_conditions.insert(q1);
559 scoped_ptr<URLMatcherSchemeFilter> scheme_filter;
560 scoped_ptr<URLMatcherPortFilter> port_filter;
562 scoped_refptr<URLMatcherConditionSet> condition_set(
563 new URLMatcherConditionSet(1,
564 conditions,
565 query_conditions,
566 scheme_filter.Pass(),
567 port_filter.Pass()));
569 GURL url("http://www.example.com/foo?" + url_query);
571 URLMatcher matcher;
572 URLMatcherConditionSet::Vector vector;
573 vector.push_back(condition_set);
574 matcher.AddConditionSets(vector);
576 return matcher.MatchURL(url).size() == 1;
579 } // namespace
581 TEST(URLMatcherConditionSetTest, QueryMatching) {
582 EXPECT_TRUE(
583 IsQueryMatch("a=foo&b=foo&a=barr",
584 "a",
585 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
586 "bar",
587 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
588 URLQueryElementMatcherCondition::MATCH_ANY));
589 EXPECT_FALSE(
590 IsQueryMatch("a=foo&b=foo&a=barr",
591 "a",
592 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
593 "bar",
594 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
595 URLQueryElementMatcherCondition::MATCH_ANY));
596 EXPECT_TRUE(
597 IsQueryMatch("a=foo&b=foo&a=barr",
598 "a",
599 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
600 "bar",
601 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
602 URLQueryElementMatcherCondition::MATCH_ANY));
603 EXPECT_FALSE(
604 IsQueryMatch("a=foo&b=foo&a=barr",
605 "a",
606 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
607 "bar",
608 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
609 URLQueryElementMatcherCondition::MATCH_ANY));
610 EXPECT_TRUE(
611 IsQueryMatch("a&b=foo&a=barr",
612 "a",
613 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
614 "bar",
615 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
616 URLQueryElementMatcherCondition::MATCH_ANY));
617 EXPECT_FALSE(
618 IsQueryMatch("a=foo&b=foo&a=barr",
619 "a",
620 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
621 "bar",
622 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
623 URLQueryElementMatcherCondition::MATCH_ANY));
625 EXPECT_FALSE(
626 IsQueryMatch("a=foo&b=foo&a=bar",
627 "a",
628 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
629 "bar",
630 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
631 URLQueryElementMatcherCondition::MATCH_ALL));
632 EXPECT_TRUE(
633 IsQueryMatch("a=bar&b=foo&a=bar",
634 "a",
635 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
636 "bar",
637 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
638 URLQueryElementMatcherCondition::MATCH_ALL));
639 EXPECT_TRUE(
640 IsQueryMatch("a=bar&b=foo&a=bar",
641 "b",
642 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
643 "foo",
644 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
645 URLQueryElementMatcherCondition::MATCH_ALL));
646 EXPECT_FALSE(
647 IsQueryMatch("a=bar&b=foo&a=bar",
648 "b",
649 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
650 "goo",
651 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
652 URLQueryElementMatcherCondition::MATCH_ALL));
653 EXPECT_FALSE(
654 IsQueryMatch("a=bar&b=foo&a=bar",
655 "c",
656 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
657 "goo",
658 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
659 URLQueryElementMatcherCondition::MATCH_ALL));
660 EXPECT_TRUE(
661 IsQueryMatch("a=foo1&b=foo&a=foo2",
662 "a",
663 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
664 "foo",
665 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
666 URLQueryElementMatcherCondition::MATCH_ALL));
667 EXPECT_FALSE(
668 IsQueryMatch("a=foo1&b=foo&a=fo02",
669 "a",
670 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
671 "foo",
672 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
673 URLQueryElementMatcherCondition::MATCH_ALL));
674 EXPECT_TRUE(
675 IsQueryMatch("a&b=foo&a",
676 "a",
677 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
678 "foo",
679 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
680 URLQueryElementMatcherCondition::MATCH_ALL));
681 EXPECT_TRUE(
682 IsQueryMatch("alt&b=foo",
683 "a",
684 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
685 "foo",
686 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
687 URLQueryElementMatcherCondition::MATCH_ALL));
688 EXPECT_TRUE(
689 IsQueryMatch("b=foo&a",
690 "a",
691 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
692 "foo",
693 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
694 URLQueryElementMatcherCondition::MATCH_ALL));
695 EXPECT_FALSE(
696 IsQueryMatch("b=foo",
697 "a",
698 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
699 "foo",
700 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
701 URLQueryElementMatcherCondition::MATCH_ALL));
702 EXPECT_TRUE(
703 IsQueryMatch("b=foo&a",
704 "a",
705 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
706 "foo",
707 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
708 URLQueryElementMatcherCondition::MATCH_ALL));
710 EXPECT_TRUE(
711 IsQueryMatch("a=foo&b=foo&a=bar",
712 "a",
713 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
714 "foo",
715 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
716 URLQueryElementMatcherCondition::MATCH_FIRST));
717 EXPECT_FALSE(
718 IsQueryMatch("a=foo&b=foo&a=bar",
719 "a",
720 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
721 "bar",
722 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
723 URLQueryElementMatcherCondition::MATCH_FIRST));
724 EXPECT_TRUE(
725 IsQueryMatch("a=foo1&b=foo&a=bar",
726 "a",
727 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
728 "foo",
729 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
730 URLQueryElementMatcherCondition::MATCH_FIRST));
731 EXPECT_FALSE(
732 IsQueryMatch("a=foo1&b=foo&a=bar",
733 "a",
734 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
735 "foo",
736 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
737 URLQueryElementMatcherCondition::MATCH_FIRST));
738 EXPECT_TRUE(
739 IsQueryMatch("a&b=foo&a=bar",
740 "a",
741 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
742 "foo",
743 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
744 URLQueryElementMatcherCondition::MATCH_FIRST));
745 EXPECT_TRUE(
746 IsQueryMatch("alt&b=foo&a=bar",
747 "a",
748 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
749 "foo",
750 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
751 URLQueryElementMatcherCondition::MATCH_FIRST));
752 EXPECT_FALSE(
753 IsQueryMatch("alt&b=foo&a=bar",
754 "a",
755 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
756 "foo",
757 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
758 URLQueryElementMatcherCondition::MATCH_FIRST));
760 EXPECT_FALSE(
761 IsQueryMatch("a=foo&b=foo&a=bar",
762 "a",
763 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
764 "foo",
765 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
766 URLQueryElementMatcherCondition::MATCH_LAST));
767 EXPECT_TRUE(
768 IsQueryMatch("a=foo&b=foo&a=bar",
769 "a",
770 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
771 "bar",
772 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
773 URLQueryElementMatcherCondition::MATCH_LAST));
774 EXPECT_FALSE(
775 IsQueryMatch("a=foo1&b=foo&a=bar",
776 "a",
777 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
778 "foo",
779 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
780 URLQueryElementMatcherCondition::MATCH_LAST));
781 EXPECT_TRUE(
782 IsQueryMatch("a=foo1&b=foo&a=bar1",
783 "a",
784 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE,
785 "bar",
786 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
787 URLQueryElementMatcherCondition::MATCH_LAST));
788 EXPECT_FALSE(
789 IsQueryMatch("a&b=foo&a=bar",
790 "a",
791 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
792 "foo",
793 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
794 URLQueryElementMatcherCondition::MATCH_LAST));
795 EXPECT_TRUE(
796 IsQueryMatch("b=foo&alt",
797 "a",
798 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
799 "foo",
800 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX,
801 URLQueryElementMatcherCondition::MATCH_LAST));
802 EXPECT_FALSE(
803 IsQueryMatch("b=foo&alt",
804 "a",
805 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY,
806 "foo",
807 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT,
808 URLQueryElementMatcherCondition::MATCH_LAST));
812 // URLMatcher
815 TEST(URLMatcherTest, FullTest) {
816 GURL url1("http://www.example.com/foo?bar=1");
817 GURL url2("http://foo.example.com/index.html");
819 URLMatcher matcher;
820 URLMatcherConditionFactory* factory = matcher.condition_factory();
822 // First insert.
823 URLMatcherConditionSet::Conditions conditions1;
824 conditions1.insert(factory->CreateHostSuffixCondition("example.com"));
825 conditions1.insert(factory->CreatePathContainsCondition("foo"));
827 const int kConditionSetId1 = 1;
828 URLMatcherConditionSet::Vector insert1;
829 insert1.push_back(make_scoped_refptr(
830 new URLMatcherConditionSet(kConditionSetId1, conditions1)));
831 matcher.AddConditionSets(insert1);
832 EXPECT_EQ(1u, matcher.MatchURL(url1).size());
833 EXPECT_EQ(0u, matcher.MatchURL(url2).size());
835 // Second insert.
836 URLMatcherConditionSet::Conditions conditions2;
837 conditions2.insert(factory->CreateHostSuffixCondition("example.com"));
839 const int kConditionSetId2 = 2;
840 URLMatcherConditionSet::Vector insert2;
841 insert2.push_back(make_scoped_refptr(
842 new URLMatcherConditionSet(kConditionSetId2, conditions2)));
843 matcher.AddConditionSets(insert2);
844 EXPECT_EQ(2u, matcher.MatchURL(url1).size());
845 EXPECT_EQ(1u, matcher.MatchURL(url2).size());
847 // This should be the cached singleton.
848 int patternId1 = factory->CreateHostSuffixCondition(
849 "example.com").string_pattern()->id();
851 // Third insert.
852 URLMatcherConditionSet::Conditions conditions3;
853 conditions3.insert(factory->CreateHostSuffixCondition("example.com"));
854 conditions3.insert(factory->CreateURLMatchesCondition("x.*[0-9]"));
856 const int kConditionSetId3 = 3;
857 URLMatcherConditionSet::Vector insert3;
858 insert3.push_back(make_scoped_refptr(
859 new URLMatcherConditionSet(kConditionSetId3, conditions3)));
860 matcher.AddConditionSets(insert3);
861 EXPECT_EQ(3u, matcher.MatchURL(url1).size());
862 EXPECT_EQ(1u, matcher.MatchURL(url2).size());
864 // Removal of third insert.
865 std::vector<URLMatcherConditionSet::ID> remove3;
866 remove3.push_back(kConditionSetId3);
867 matcher.RemoveConditionSets(remove3);
868 EXPECT_EQ(2u, matcher.MatchURL(url1).size());
869 EXPECT_EQ(1u, matcher.MatchURL(url2).size());
871 // Removal of second insert.
872 std::vector<URLMatcherConditionSet::ID> remove2;
873 remove2.push_back(kConditionSetId2);
874 matcher.RemoveConditionSets(remove2);
875 EXPECT_EQ(1u, matcher.MatchURL(url1).size());
876 EXPECT_EQ(0u, matcher.MatchURL(url2).size());
878 // Removal of first insert.
879 std::vector<URLMatcherConditionSet::ID> remove1;
880 remove1.push_back(kConditionSetId1);
881 matcher.RemoveConditionSets(remove1);
882 EXPECT_EQ(0u, matcher.MatchURL(url1).size());
883 EXPECT_EQ(0u, matcher.MatchURL(url2).size());
885 EXPECT_TRUE(matcher.IsEmpty());
887 // The cached singleton in matcher.condition_factory_ should be destroyed to
888 // free memory.
889 int patternId2 = factory->CreateHostSuffixCondition(
890 "example.com").string_pattern()->id();
891 // If patternId1 and patternId2 are different that indicates that
892 // matcher.condition_factory_ does not leak memory by holding onto
893 // unused patterns.
894 EXPECT_NE(patternId1, patternId2);
897 TEST(URLMatcherTest, TestComponentsImplyContains) {
898 // Due to a different implementation of component (prefix, suffix and equals)
899 // and *Contains conditions we need to check that when a pattern matches a
900 // given part of a URL as equal, prefix or suffix, it also matches it in the
901 // "contains" test.
902 GURL url("https://www.google.com:1234/webhp?test=val&a=b");
904 URLMatcher matcher;
905 URLMatcherConditionFactory* factory = matcher.condition_factory();
907 URLMatcherConditionSet::Conditions conditions;
909 // First insert all the matching equals => contains pairs.
910 conditions.insert(factory->CreateHostEqualsCondition("www.google.com"));
911 conditions.insert(factory->CreateHostContainsCondition("www.google.com"));
913 conditions.insert(factory->CreateHostPrefixCondition("www."));
914 conditions.insert(factory->CreateHostContainsCondition("www."));
916 conditions.insert(factory->CreateHostSuffixCondition("com"));
917 conditions.insert(factory->CreateHostContainsCondition("com"));
919 conditions.insert(factory->CreatePathEqualsCondition("/webhp"));
920 conditions.insert(factory->CreatePathContainsCondition("/webhp"));
922 conditions.insert(factory->CreatePathPrefixCondition("/we"));
923 conditions.insert(factory->CreatePathContainsCondition("/we"));
925 conditions.insert(factory->CreatePathSuffixCondition("hp"));
926 conditions.insert(factory->CreatePathContainsCondition("hp"));
928 conditions.insert(factory->CreateQueryEqualsCondition("test=val&a=b"));
929 conditions.insert(factory->CreateQueryContainsCondition("test=val&a=b"));
931 conditions.insert(factory->CreateQueryPrefixCondition("test=v"));
932 conditions.insert(factory->CreateQueryContainsCondition("test=v"));
934 conditions.insert(factory->CreateQuerySuffixCondition("l&a=b"));
935 conditions.insert(factory->CreateQueryContainsCondition("l&a=b"));
937 // The '?' for equality is just ignored.
938 conditions.insert(factory->CreateQueryEqualsCondition("?test=val&a=b"));
939 // Due to '?' the condition created here is a prefix-testing condition.
940 conditions.insert(factory->CreateQueryContainsCondition("?test=val&a=b"));
942 const int kConditionSetId = 1;
943 URLMatcherConditionSet::Vector insert;
944 insert.push_back(make_scoped_refptr(
945 new URLMatcherConditionSet(kConditionSetId, conditions)));
946 matcher.AddConditionSets(insert);
947 EXPECT_EQ(1u, matcher.MatchURL(url).size());
950 // Check that matches in everything but the query are found.
951 TEST(URLMatcherTest, TestOriginAndPathRegExPositive) {
952 GURL url("https://www.google.com:1234/webhp?test=val&a=b");
954 URLMatcher matcher;
955 URLMatcherConditionFactory* factory = matcher.condition_factory();
957 URLMatcherConditionSet::Conditions conditions;
959 conditions.insert(factory->CreateOriginAndPathMatchesCondition("w..hp"));
960 const int kConditionSetId = 1;
961 URLMatcherConditionSet::Vector insert;
962 insert.push_back(make_scoped_refptr(
963 new URLMatcherConditionSet(kConditionSetId, conditions)));
964 matcher.AddConditionSets(insert);
965 EXPECT_EQ(1u, matcher.MatchURL(url).size());
968 // Check that matches in the query are ignored.
969 TEST(URLMatcherTest, TestOriginAndPathRegExNegative) {
970 GURL url("https://www.google.com:1234/webhp?test=val&a=b");
972 URLMatcher matcher;
973 URLMatcherConditionFactory* factory = matcher.condition_factory();
975 URLMatcherConditionSet::Conditions conditions;
977 conditions.insert(factory->CreateOriginAndPathMatchesCondition("val"));
978 const int kConditionSetId = 1;
979 URLMatcherConditionSet::Vector insert;
980 insert.push_back(make_scoped_refptr(
981 new URLMatcherConditionSet(kConditionSetId, conditions)));
982 matcher.AddConditionSets(insert);
983 EXPECT_EQ(0u, matcher.MatchURL(url).size());
986 } // namespace url_matcher