OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/service_worker/service_worker_utils.h" | 5 #include "content/browser/service_worker/service_worker_utils.h" |
6 #include "testing/gtest/include/gtest/gtest.h" | 6 #include "testing/gtest/include/gtest/gtest.h" |
7 | 7 |
8 namespace content { | 8 namespace content { |
9 | 9 |
10 TEST(ServiceWorkerUtilsTest, ScopeMatches) { | 10 TEST(ServiceWorkerUtilsTest, ScopeMatches) { |
11 ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches( | 11 ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches( |
12 GURL("http://www.example.com/*"), GURL("http://www.example.com/"))); | 12 GURL("http://www.example.com/"), GURL("http://www.example.com/"))); |
13 ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches( | 13 ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches( |
14 GURL("http://www.example.com/*"), | 14 GURL("http://www.example.com/"), |
15 GURL("http://www.example.com/page.html"))); | 15 GURL("http://www.example.com/page.html"))); |
16 | 16 |
17 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( | 17 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( |
18 GURL("http://www.example.com/*"), GURL("https://www.example.com/"))); | 18 GURL("http://www.example.com/"), GURL("https://www.example.com/"))); |
19 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( | 19 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( |
20 GURL("http://www.example.com/*"), | 20 GURL("http://www.example.com/"), |
21 GURL("https://www.example.com/page.html"))); | 21 GURL("https://www.example.com/page.html"))); |
22 | 22 |
23 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( | 23 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( |
24 GURL("http://www.example.com/*"), GURL("http://www.foo.com/"))); | 24 GURL("http://www.example.com/"), GURL("http://www.foo.com/"))); |
25 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( | 25 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( |
26 GURL("http://www.example.com/*"), GURL("https://www.foo.com/page.html"))); | 26 GURL("http://www.example.com/"), GURL("https://www.foo.com/page.html"))); |
| 27 |
| 28 // '*' is not a wildcard. |
| 29 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( |
| 30 GURL("http://www.example.com/*"), GURL("http://www.example.com/x"))); |
| 31 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( |
| 32 GURL("http://www.example.com/*"), GURL("http://www.example.com/"))); |
| 33 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( |
| 34 GURL("http://www.example.com/*"), GURL("http://www.example.com/xx"))); |
| 35 ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches( |
| 36 GURL("http://www.example.com/*"), GURL("http://www.example.com/*"))); |
27 | 37 |
28 ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches( | 38 ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches( |
29 GURL("http://www.example.com/"), GURL("http://www.example.com/"))); | 39 GURL("http://www.example.com/*/x"), GURL("http://www.example.com/*/x"))); |
30 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( | 40 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( |
31 GURL("http://www.example.com/"), GURL("http://www.example.com/x"))); | 41 GURL("http://www.example.com/*/x"), GURL("http://www.example.com/a/x"))); |
| 42 ASSERT_FALSE( |
| 43 ServiceWorkerUtils::ScopeMatches(GURL("http://www.example.com/*/x/*"), |
| 44 GURL("http://www.example.com/a/x/b"))); |
| 45 ASSERT_FALSE( |
| 46 ServiceWorkerUtils::ScopeMatches(GURL("http://www.example.com/*/x/*"), |
| 47 GURL("http://www.example.com/*/x/b"))); |
32 | 48 |
33 // '?' is not a wildcard. | 49 // '?' is not a wildcard. |
34 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( | 50 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( |
35 GURL("http://www.example.com/?"), GURL("http://www.example.com/x"))); | 51 GURL("http://www.example.com/?"), GURL("http://www.example.com/x"))); |
36 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( | 52 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( |
37 GURL("http://www.example.com/?"), GURL("http://www.example.com/"))); | 53 GURL("http://www.example.com/?"), GURL("http://www.example.com/"))); |
38 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( | 54 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( |
39 GURL("http://www.example.com/?"), GURL("http://www.example.com/xx"))); | 55 GURL("http://www.example.com/?"), GURL("http://www.example.com/xx"))); |
40 ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches( | 56 ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches( |
41 GURL("http://www.example.com/?"), GURL("http://www.example.com/?"))); | 57 GURL("http://www.example.com/?"), GURL("http://www.example.com/?"))); |
42 | 58 |
43 // Query string is part of the resource. | 59 // Query string is part of the resource. |
44 ASSERT_TRUE( | 60 ASSERT_TRUE( |
45 ServiceWorkerUtils::ScopeMatches(GURL("http://www.example.com/?a=b"), | 61 ServiceWorkerUtils::ScopeMatches(GURL("http://www.example.com/?a=b"), |
46 GURL("http://www.example.com/?a=b"))); | 62 GURL("http://www.example.com/?a=b"))); |
47 ASSERT_TRUE( | 63 ASSERT_TRUE( |
48 ServiceWorkerUtils::ScopeMatches(GURL("http://www.example.com/?a=*"), | 64 ServiceWorkerUtils::ScopeMatches(GURL("http://www.example.com/?a="), |
49 GURL("http://www.example.com/?a=b"))); | 65 GURL("http://www.example.com/?a=b"))); |
50 ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches( | 66 ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches( |
51 GURL("http://www.example.com/*"), GURL("http://www.example.com/?a=b"))); | |
52 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( | |
53 GURL("http://www.example.com/"), GURL("http://www.example.com/?a=b"))); | 67 GURL("http://www.example.com/"), GURL("http://www.example.com/?a=b"))); |
54 | 68 |
55 // '*' only has special meaning in terminal position. | 69 // URLs canonicalize \ to / so this is equivalent to "...//x" |
56 ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches( | 70 ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches( |
57 GURL("http://www.example.com/*/x"), GURL("http://www.example.com/*/x"))); | 71 GURL("http://www.example.com/\\x"), GURL("http://www.example.com//x"))); |
58 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( | |
59 GURL("http://www.example.com/*/x"), GURL("http://www.example.com/a/x"))); | |
60 ASSERT_FALSE( | |
61 ServiceWorkerUtils::ScopeMatches(GURL("http://www.example.com/*/x/*"), | |
62 GURL("http://www.example.com/a/x/b"))); | |
63 ASSERT_TRUE( | |
64 ServiceWorkerUtils::ScopeMatches(GURL("http://www.example.com/*/x/*"), | |
65 GURL("http://www.example.com/*/x/b"))); | |
66 | |
67 // URLs canonicalize \ to / so this is equivalent to "...//*" and "...//x" | |
68 ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches( | |
69 GURL("http://www.example.com/\\*"), GURL("http://www.example.com/\\x"))); | |
70 } | 72 } |
71 | 73 |
72 TEST(ServiceWorkerUtilsTest, FindLongestScopeMatch_Basic) { | 74 TEST(ServiceWorkerUtilsTest, FindLongestScopeMatch) { |
73 LongestScopeMatcher matcher(GURL("http://www.example.com/xxx")); | 75 LongestScopeMatcher matcher(GURL("http://www.example.com/xxx")); |
74 | 76 |
75 // "/xx*" should be matched longest. | 77 // "/xx" should be matched longest. |
76 ASSERT_TRUE(matcher.MatchLongest(GURL("http://www.example.com/x*"))); | 78 ASSERT_TRUE(matcher.MatchLongest(GURL("http://www.example.com/x"))); |
77 ASSERT_FALSE(matcher.MatchLongest(GURL("http://www.example.com/*"))); | 79 ASSERT_FALSE(matcher.MatchLongest(GURL("http://www.example.com/"))); |
78 ASSERT_TRUE(matcher.MatchLongest(GURL("http://www.example.com/xx*"))); | 80 ASSERT_TRUE(matcher.MatchLongest(GURL("http://www.example.com/xx"))); |
79 | 81 |
80 // "xxx*" should be matched longer than "/xx*". | 82 // "/xxx" should be matched longer than "/xx". |
81 ASSERT_TRUE(matcher.MatchLongest(GURL("http://www.example.com/xxx*"))); | 83 ASSERT_TRUE(matcher.MatchLongest(GURL("http://www.example.com/xxx"))); |
82 | 84 |
83 ASSERT_FALSE(matcher.MatchLongest(GURL("http://www.example.com/xxxx*"))); | 85 ASSERT_FALSE(matcher.MatchLongest(GURL("http://www.example.com/xxxx"))); |
84 } | |
85 | |
86 TEST(ServiceWorkerUtilsTest, FindLongestScopeMatch_SameLength) { | |
87 LongestScopeMatcher matcher1(GURL("http://www.example.com/xxx")); | |
88 | |
89 // "/xxx" has the same length with "/xx*", so they are compared as strings | |
90 // and "/xxx" should win. | |
91 // TODO(nhiroki): This isn't in the spec (see: service_worker_utils.cc) | |
92 ASSERT_TRUE(matcher1.MatchLongest(GURL("http://www.example.com/xxx"))); | |
93 ASSERT_FALSE(matcher1.MatchLongest(GURL("http://www.example.com/xx*"))); | |
94 | |
95 LongestScopeMatcher matcher2(GURL("http://www.example.com/x(1)")); | |
96 | |
97 // "/xx*" should be prioritized over "/x(1)". | |
98 // TODO(nhiroki): This isn't in the spec (see: service_worker_utils.cc) | |
99 ASSERT_TRUE(matcher2.MatchLongest(GURL("http://www.example.com/x(1)"))); | |
100 ASSERT_TRUE(matcher2.MatchLongest(GURL("http://www.example.com/x(1*"))); | |
101 } | 86 } |
102 | 87 |
103 } // namespace content | 88 } // namespace content |
OLD | NEW |