OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/link_header_support.h" | 5 #include "content/browser/service_worker/link_header_support.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 10 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
11 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 11 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
12 #include "content/browser/service_worker/service_worker_registration.h" | 12 #include "content/browser/service_worker/service_worker_registration.h" |
13 #include "content/public/browser/resource_request_info.h" | 13 #include "content/public/browser/resource_request_info.h" |
14 #include "content/public/common/content_switches.h" | 14 #include "content/public/common/content_switches.h" |
15 #include "content/public/test/mock_resource_context.h" | 15 #include "content/public/test/mock_resource_context.h" |
16 #include "content/public/test/test_browser_thread_bundle.h" | 16 #include "content/public/test/test_browser_thread_bundle.h" |
17 #include "net/http/http_response_headers.h" | 17 #include "net/http/http_response_headers.h" |
18 #include "net/url_request/url_request_test_job.h" | 18 #include "net/url_request/url_request_test_job.h" |
19 #include "net/url_request/url_request_test_util.h" | 19 #include "net/url_request/url_request_test_util.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
21 | 21 |
22 namespace content { | 22 namespace content { |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 TEST(LinkHeaderTest, SplitEmpty) { | |
27 std::vector<std::string> values; | |
28 SplitLinkHeaderForTesting("", &values); | |
29 ASSERT_EQ(0u, values.size()); | |
30 } | |
31 | |
32 TEST(LinkHeaderTest, SplitSimple) { | |
33 std::vector<std::string> values; | |
34 SplitLinkHeaderForTesting("hello", &values); | |
35 ASSERT_EQ(1u, values.size()); | |
36 EXPECT_EQ("hello", values[0]); | |
37 | |
38 SplitLinkHeaderForTesting("foo, bar", &values); | |
39 ASSERT_EQ(2u, values.size()); | |
40 EXPECT_EQ("foo", values[0]); | |
41 EXPECT_EQ("bar", values[1]); | |
42 | |
43 SplitLinkHeaderForTesting(" 1\t,\t2,3", &values); | |
44 ASSERT_EQ(3u, values.size()); | |
45 EXPECT_EQ("1", values[0]); | |
46 EXPECT_EQ("2", values[1]); | |
47 EXPECT_EQ("3", values[2]); | |
48 } | |
49 | |
50 TEST(LinkHeaderTest, SplitSkipsEmpty) { | |
51 std::vector<std::string> values; | |
52 SplitLinkHeaderForTesting(", foo, , \t, bar", &values); | |
53 ASSERT_EQ(2u, values.size()); | |
54 EXPECT_EQ("foo", values[0]); | |
55 EXPECT_EQ("bar", values[1]); | |
56 } | |
57 | |
58 TEST(LinkHeaderTest, SplitQuotes) { | |
59 std::vector<std::string> values; | |
60 SplitLinkHeaderForTesting("\"foo,bar\", 'bar,foo', <hel,lo>", &values); | |
61 ASSERT_EQ(3u, values.size()); | |
62 EXPECT_EQ("\"foo,bar\"", values[0]); | |
63 EXPECT_EQ("'bar,foo'", values[1]); | |
64 EXPECT_EQ("<hel,lo>", values[2]); | |
65 } | |
66 | |
67 TEST(LinkHeaderTest, SplitEscapedQuotes) { | |
68 std::vector<std::string> values; | |
69 SplitLinkHeaderForTesting("\"f\\\"oo,bar\", 'b\\'ar,foo', <hel\\>,lo>", | |
70 &values); | |
71 ASSERT_EQ(4u, values.size()); | |
72 EXPECT_EQ("\"f\\\"oo,bar\"", values[0]); | |
73 EXPECT_EQ("'b\\'ar,foo'", values[1]); | |
74 EXPECT_EQ("<hel\\>", values[2]); | |
75 EXPECT_EQ("lo>", values[3]); | |
76 } | |
77 | |
78 struct SimpleParseTestData { | |
79 const char* link; | |
80 bool valid; | |
81 const char* url; | |
82 const char* rel; | |
83 const char* as; | |
84 }; | |
85 | |
86 void PrintTo(const SimpleParseTestData& test, std::ostream* os) { | |
87 *os << ::testing::PrintToString(test.link); | |
88 } | |
89 | |
90 class SimpleParseTest : public ::testing::TestWithParam<SimpleParseTestData> {}; | |
91 | |
92 TEST_P(SimpleParseTest, Simple) { | |
93 const SimpleParseTestData test = GetParam(); | |
94 | |
95 std::string url; | |
96 std::unordered_map<std::string, std::string> params; | |
97 EXPECT_EQ(test.valid, | |
98 ParseLinkHeaderValueForTesting(test.link, &url, ¶ms)); | |
99 if (test.valid) { | |
100 EXPECT_EQ(test.url, url); | |
101 EXPECT_EQ(test.rel, params["rel"]); | |
102 EXPECT_EQ(test.as, params["as"]); | |
103 } | |
104 } | |
105 | |
106 // Test data mostly copied from blink::LinkHeaderTest. Expectations for some | |
107 // test cases are different though. Mostly because blink::LinkHeader is stricter | |
108 // about validity while parsing (primarily things like mismatched quotes), and | |
109 // factors in knowledge about semantics of Link headers (parameters that are | |
110 // required to have a value if they occur, some parameters are auto-lower-cased, | |
111 // headers with an "anchor" parameter are rejected by base::LinkHeader). | |
112 // The code this tests purely parses without actually interpreting the data, as | |
113 // it is expected that another layer on top will do more specific validations. | |
114 const SimpleParseTestData simple_parse_tests[] = { | |
115 {"</images/cat.jpg>; rel=prefetch", true, "/images/cat.jpg", "prefetch", | |
116 ""}, | |
117 {"</images/cat.jpg>;rel=prefetch", true, "/images/cat.jpg", "prefetch", ""}, | |
118 {"</images/cat.jpg> ;rel=prefetch", true, "/images/cat.jpg", "prefetch", | |
119 ""}, | |
120 {"</images/cat.jpg> ; rel=prefetch", true, "/images/cat.jpg", | |
121 "prefetch", ""}, | |
122 {"< /images/cat.jpg> ; rel=prefetch", true, "/images/cat.jpg", | |
123 "prefetch", ""}, | |
124 {"</images/cat.jpg > ; rel=prefetch", true, "/images/cat.jpg", | |
125 "prefetch", ""}, | |
126 {"</images/cat.jpg wutwut> ; rel=prefetch", true, | |
127 "/images/cat.jpg wutwut", "prefetch", ""}, | |
128 {"</images/cat.jpg wutwut \t > ; rel=prefetch", true, | |
129 "/images/cat.jpg wutwut", "prefetch", ""}, | |
130 {"</images/cat.jpg>; rel=prefetch ", true, "/images/cat.jpg", "prefetch", | |
131 ""}, | |
132 {"</images/cat.jpg>; Rel=prefetch ", true, "/images/cat.jpg", "prefetch", | |
133 ""}, | |
134 {"</images/cat.jpg>; Rel=PReFetCh ", true, "/images/cat.jpg", "PReFetCh", | |
135 ""}, | |
136 {"</images/cat.jpg>; rel=prefetch; rel=somethingelse", true, | |
137 "/images/cat.jpg", "prefetch", ""}, | |
138 {"</images/cat.jpg>\t\t ; \trel=prefetch \t ", true, "/images/cat.jpg", | |
139 "prefetch", ""}, | |
140 {"</images/cat.jpg>; rel= prefetch", true, "/images/cat.jpg", "prefetch", | |
141 ""}, | |
142 {"<../images/cat.jpg?dog>; rel= prefetch", true, "../images/cat.jpg?dog", | |
143 "prefetch", ""}, | |
144 {"</images/cat.jpg>; rel =prefetch", true, "/images/cat.jpg", "prefetch", | |
145 ""}, | |
146 {"</images/cat.jpg>; rel pel=prefetch", false}, | |
147 {"< /images/cat.jpg>", true, "/images/cat.jpg", "", ""}, | |
148 {"</images/cat.jpg>; wut=sup; rel =prefetch", true, "/images/cat.jpg", | |
149 "prefetch", ""}, | |
150 {"</images/cat.jpg>; wut=sup ; rel =prefetch", true, "/images/cat.jpg", | |
151 "prefetch", ""}, | |
152 {"</images/cat.jpg>; wut=sup ; rel =prefetch \t ;", true, | |
153 "/images/cat.jpg", "prefetch", ""}, | |
154 {"</images/cat.jpg> wut=sup ; rel =prefetch \t ;", false}, | |
155 {"< /images/cat.jpg", false}, | |
156 {"< http://wut.com/ sdfsdf ?sd>; rel=dns-prefetch", true, | |
157 "http://wut.com/ sdfsdf ?sd", "dns-prefetch", ""}, | |
158 {"< http://wut.com/%20%20%3dsdfsdf?sd>; rel=dns-prefetch", true, | |
159 "http://wut.com/%20%20%3dsdfsdf?sd", "dns-prefetch", ""}, | |
160 {"< http://wut.com/dfsdf?sdf=ghj&wer=rty>; rel=prefetch", true, | |
161 "http://wut.com/dfsdf?sdf=ghj&wer=rty", "prefetch", ""}, | |
162 {"< http://wut.com/dfsdf?sdf=ghj&wer=rty>;;;;; rel=prefetch", true, | |
163 "http://wut.com/dfsdf?sdf=ghj&wer=rty", "prefetch", ""}, | |
164 {"< http://wut.com/%20%20%3dsdfsdf?sd>; rel=preload;as=image", true, | |
165 "http://wut.com/%20%20%3dsdfsdf?sd", "preload", "image"}, | |
166 {"< http://wut.com/%20%20%3dsdfsdf?sd>; rel=preload;as=whatever", true, | |
167 "http://wut.com/%20%20%3dsdfsdf?sd", "preload", "whatever"}, | |
168 {"</images/cat.jpg>; rel=prefetch;", true, "/images/cat.jpg", "prefetch", | |
169 ""}, | |
170 {"</images/cat.jpg>; rel=prefetch ;", true, "/images/cat.jpg", | |
171 "prefetch", ""}, | |
172 {"</images/ca,t.jpg>; rel=prefetch ;", true, "/images/ca,t.jpg", | |
173 "prefetch", ""}, | |
174 {"<simple.css>; rel=stylesheet; title=\"title with a DQUOTE and " | |
175 "backslash\"", | |
176 true, "simple.css", "stylesheet", ""}, | |
177 {"<simple.css>; rel=stylesheet; title=\"title with a DQUOTE \\\" and " | |
178 "backslash: \\\"", | |
179 true, "simple.css", "stylesheet", ""}, | |
180 {"<simple.css>; title=\"title with a DQUOTE \\\" and backslash: \"; " | |
181 "rel=stylesheet; ", | |
182 true, "simple.css", "stylesheet", ""}, | |
183 {"<simple.css>; title=\'title with a DQUOTE \\\' and backslash: \'; " | |
184 "rel=stylesheet; ", | |
185 true, "simple.css", "stylesheet", ""}, | |
186 {"<simple.css>; title=\"title with a DQUOTE \\\" and ;backslash,: \"; " | |
187 "rel=stylesheet; ", | |
188 true, "simple.css", "stylesheet", ""}, | |
189 {"<simple.css>; title=\"title with a DQUOTE \' and ;backslash,: \"; " | |
190 "rel=stylesheet; ", | |
191 true, "simple.css", "stylesheet", ""}, | |
192 {"<simple.css>; title=\"\"; rel=stylesheet; ", true, "simple.css", | |
193 "stylesheet", ""}, | |
194 {"<simple.css>; title=\"\"; rel=\"stylesheet\"; ", true, "simple.css", | |
195 "stylesheet", ""}, | |
196 {"<simple.css>; rel=stylesheet; title=\"", true, "simple.css", "stylesheet", | |
197 ""}, | |
198 {"<simple.css>; rel=stylesheet; title=\"\"", true, "simple.css", | |
199 "stylesheet", ""}, | |
200 {"<simple.css>; rel=\"stylesheet\"; title=\"", true, "simple.css", | |
201 "stylesheet", ""}, | |
202 {"<simple.css>; rel=\";style,sheet\"; title=\"", true, "simple.css", | |
203 ";style,sheet", ""}, | |
204 {"<simple.css>; rel=\"bla'sdf\"; title=\"", true, "simple.css", "bla'sdf", | |
205 ""}, | |
206 {"<simple.css>; rel=\"\"; title=\"\"", true, "simple.css", "", ""}, | |
207 {"<simple.css>; rel=''; title=\"\"", true, "simple.css", "", ""}, | |
208 {"<simple.css>; rel=''; bla", true, "simple.css", "", ""}, | |
209 {"<simple.css>; rel='prefetch", true, "simple.css", "prefetch", ""}, | |
210 {"<simple.css>; rel=\"prefetch", true, "simple.css", "prefetch", ""}, | |
211 {"<simple.css>; rel=\"", true, "simple.css", "", ""}, | |
212 {"simple.css; rel=prefetch", false}, | |
213 {"<simple.css>; rel=prefetch; rel=foobar", true, "simple.css", "prefetch", | |
214 ""}, | |
215 }; | |
216 | |
217 INSTANTIATE_TEST_CASE_P(LinkHeaderTest, | |
218 SimpleParseTest, | |
219 testing::ValuesIn(simple_parse_tests)); | |
220 | |
221 void SaveFoundRegistrationsCallback( | 26 void SaveFoundRegistrationsCallback( |
222 ServiceWorkerStatusCode expected_status, | 27 ServiceWorkerStatusCode expected_status, |
223 bool* called, | 28 bool* called, |
224 std::vector<ServiceWorkerRegistrationInfo>* registrations, | 29 std::vector<ServiceWorkerRegistrationInfo>* registrations, |
225 ServiceWorkerStatusCode status, | 30 ServiceWorkerStatusCode status, |
226 const std::vector<ServiceWorkerRegistrationInfo>& result) { | 31 const std::vector<ServiceWorkerRegistrationInfo>& result) { |
227 EXPECT_EQ(expected_status, status); | 32 EXPECT_EQ(expected_status, status); |
228 *called = true; | 33 *called = true; |
229 *registrations = result; | 34 *registrations = result; |
230 } | 35 } |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); | 206 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
402 ASSERT_EQ(1u, registrations.size()); | 207 ASSERT_EQ(1u, registrations.size()); |
403 EXPECT_EQ(GURL("https://example.com/foobar/scope"), registrations[0].pattern); | 208 EXPECT_EQ(GURL("https://example.com/foobar/scope"), registrations[0].pattern); |
404 EXPECT_EQ(GURL("https://example.com/foobar/baz.js"), | 209 EXPECT_EQ(GURL("https://example.com/foobar/baz.js"), |
405 registrations[0].active_version.script_url); | 210 registrations[0].active_version.script_url); |
406 } | 211 } |
407 | 212 |
408 } // namespace | 213 } // namespace |
409 | 214 |
410 } // namespace content | 215 } // namespace content |
OLD | NEW |