Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_utils_unittest.cc
blob30e26b49d43265af36f9e8e548699eb3f0712017
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
3 // found in the LICENSE file.
5 #include "content/browser/service_worker/service_worker_utils.h"
6 #include "testing/gtest/include/gtest/gtest.h"
8 namespace content {
10 TEST(ServiceWorkerUtilsTest, ScopeMatches) {
11 ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches(
12 GURL("http://www.example.com/"), GURL("http://www.example.com/")));
13 ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches(
14 GURL("http://www.example.com/"),
15 GURL("http://www.example.com/page.html")));
17 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches(
18 GURL("http://www.example.com/"), GURL("https://www.example.com/")));
19 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches(
20 GURL("http://www.example.com/"),
21 GURL("https://www.example.com/page.html")));
23 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches(
24 GURL("http://www.example.com/"), GURL("http://www.foo.com/")));
25 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches(
26 GURL("http://www.example.com/"), GURL("https://www.foo.com/page.html")));
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/*")));
38 ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches(
39 GURL("http://www.example.com/*/x"), GURL("http://www.example.com/*/x")));
40 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches(
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")));
49 // '?' is not a wildcard.
50 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches(
51 GURL("http://www.example.com/?"), GURL("http://www.example.com/x")));
52 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches(
53 GURL("http://www.example.com/?"), GURL("http://www.example.com/")));
54 ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches(
55 GURL("http://www.example.com/?"), GURL("http://www.example.com/xx")));
56 ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches(
57 GURL("http://www.example.com/?"), GURL("http://www.example.com/?")));
59 // Query string is part of the resource.
60 ASSERT_TRUE(
61 ServiceWorkerUtils::ScopeMatches(GURL("http://www.example.com/?a=b"),
62 GURL("http://www.example.com/?a=b")));
63 ASSERT_TRUE(
64 ServiceWorkerUtils::ScopeMatches(GURL("http://www.example.com/?a="),
65 GURL("http://www.example.com/?a=b")));
66 ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches(
67 GURL("http://www.example.com/"), GURL("http://www.example.com/?a=b")));
69 // URLs canonicalize \ to / so this is equivalent to "...//x"
70 ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches(
71 GURL("http://www.example.com/\\x"), GURL("http://www.example.com//x")));
74 TEST(ServiceWorkerUtilsTest, FindLongestScopeMatch) {
75 LongestScopeMatcher matcher(GURL("http://www.example.com/xxx"));
77 // "/xx" should be matched longest.
78 ASSERT_TRUE(matcher.MatchLongest(GURL("http://www.example.com/x")));
79 ASSERT_FALSE(matcher.MatchLongest(GURL("http://www.example.com/")));
80 ASSERT_TRUE(matcher.MatchLongest(GURL("http://www.example.com/xx")));
82 // "/xxx" should be matched longer than "/xx".
83 ASSERT_TRUE(matcher.MatchLongest(GURL("http://www.example.com/xxx")));
85 ASSERT_FALSE(matcher.MatchLongest(GURL("http://www.example.com/xxxx")));
88 } // namespace content