Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(869)

Side by Side Diff: chrome/browser/chrome_service_worker_browsertest.cc

Issue 1665453003: Add browser tests for FetchEvent.request.mode/credentials in the service worker (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase on 1665533003 Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // This file tests that Service Workers (a Content feature) work in the Chromium 5 // This file tests that Service Workers (a Content feature) work in the Chromium
6 // embedder. 6 // embedder.
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/command_line.h"
9 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
10 #include "base/numerics/safe_conversions.h" 11 #include "base/numerics/safe_conversions.h"
11 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/chrome_notification_types.h" 15 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h" 17 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_window.h" 18 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h" 19 #include "chrome/browser/ui/tabs/tab_strip_model.h"
20 #include "chrome/common/chrome_switches.h"
17 #include "chrome/test/base/in_process_browser_test.h" 21 #include "chrome/test/base/in_process_browser_test.h"
18 #include "chrome/test/base/ui_test_utils.h" 22 #include "chrome/test/base/ui_test_utils.h"
19 #include "content/public/browser/browser_context.h" 23 #include "content/public/browser/browser_context.h"
24 #include "content/public/browser/render_frame_host.h"
20 #include "content/public/browser/service_worker_context.h" 25 #include "content/public/browser/service_worker_context.h"
21 #include "content/public/browser/storage_partition.h" 26 #include "content/public/browser/storage_partition.h"
22 #include "content/public/browser/web_contents.h" 27 #include "content/public/browser/web_contents.h"
28 #include "content/public/test/browser_test_utils.h"
23 #include "net/test/embedded_test_server/embedded_test_server.h" 29 #include "net/test/embedded_test_server/embedded_test_server.h"
30 #include "ppapi/shared_impl/ppapi_switches.h"
24 31
25 namespace { 32 namespace {
26 33
27 class ChromeServiceWorkerTest : public InProcessBrowserTest { 34 class ChromeServiceWorkerTest : public InProcessBrowserTest {
28 protected: 35 protected:
29 ChromeServiceWorkerTest() { 36 ChromeServiceWorkerTest() {
30 EXPECT_TRUE(service_worker_dir_.CreateUniqueTempDir()); 37 EXPECT_TRUE(service_worker_dir_.CreateUniqueTempDir());
31 } 38 }
32 39
33 void WriteFile(const base::FilePath::StringType& filename, 40 void WriteFile(const base::FilePath::StringType& filename,
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 embedded_test_server()->GetURL("/test.html")); 109 embedded_test_server()->GetURL("/test.html"));
103 110
104 content::WindowedNotificationObserver observer( 111 content::WindowedNotificationObserver observer(
105 chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(incognito)); 112 chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(incognito));
106 incognito->window()->Close(); 113 incognito->window()->Close();
107 observer.Wait(); 114 observer.Wait();
108 115
109 // Test passes if we don't crash. 116 // Test passes if we don't crash.
110 } 117 }
111 118
119 class ChromeServiceWorkerFetchTest : public ChromeServiceWorkerTest {
120 protected:
121 ChromeServiceWorkerFetchTest() {}
122
123 void SetUpOnMainThread() override {
124 WriteServiceWorkerFetchTestFiles();
125 embedded_test_server()->ServeFilesFromDirectory(service_worker_dir_.path());
126 ASSERT_TRUE(embedded_test_server()->Start());
127 InitializeServiceWorkerFetchTestPage();
128 }
129
130 std::string ExecuteScriptAndExtractString(const std::string& js) {
131 std::string result;
132 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
133 browser()->tab_strip_model()->GetActiveWebContents(), js, &result));
134 return result;
135 }
136
137 std::string RequsestString(const std::string& url,
nhiroki 2016/02/09 04:48:42 s/Requsest/Request
horo 2016/02/10 06:09:22 Done.
138 const std::string& mode,
139 const std::string& credentials) const {
140 return base::StringPrintf("url:%s, mode:%s, credentials:%s\n", url.c_str(),
141 mode.c_str(), credentials.c_str());
142 }
143
144 std::string GetURL(const std::string& relative_url) const {
145 return embedded_test_server()->GetURL(relative_url).spec();
146 }
147
148 private:
149 void WriteServiceWorkerFetchTestFiles() {
150 WriteFile(FILE_PATH_LITERAL("sw.js"),
151 "this.onactivate = function(event) {"
152 " return self.clients.claim();"
nhiroki 2016/02/09 04:48:42 "return" is not necessary. event.waitUntil(self.c
horo 2016/02/10 06:09:22 Done.
153 "};"
154 "this.onfetch = function(event) {"
155 " event.respondWith("
156 " self.clients.matchAll().then(function(clients) {"
157 " clients.forEach(function(client) {"
158 " client.postMessage("
159 " 'url:' + event.request.url + ', ' +"
160 " 'mode:' + event.request.mode + ', ' +"
161 " 'credentials:' + event.request.credentials"
162 " );"
163 " });"
164 " return fetch(event.request);"
165 " }));"
166 "};");
167 WriteFile(FILE_PATH_LITERAL("test.html"),
168 "<script>"
169 "navigator.serviceWorker.register('./sw.js', {scope: './'})"
170 " .then(function(reg) {"
171 " reg.addEventListener('updatefound', function() {"
172 " var worker = reg.installing;"
173 " worker.addEventListener('statechange', function() {"
174 " if (worker.state == 'activated')"
175 " document.title = 'READY';"
176 " });"
177 " });"
178 " });"
179 "var reportOnFetch = true;"
180 "var fetchedRequests = [];"
nhiroki 2016/02/09 04:48:42 "fetchedRequests" sounds a bit strange to me becau
horo 2016/02/10 06:09:22 Done.
181 "function reportRequests() {"
182 " var str = '';"
183 " fetchedRequests.forEach(function(data) {"
184 " str += data + '\\n';"
185 " });"
186 " window.domAutomationController.setAutomationId(0);"
187 " window.domAutomationController.send(str);"
188 "}"
189 "navigator.serviceWorker.addEventListener("
190 " 'message',"
191 " function(event) {"
192 " fetchedRequests.push(event.data);"
193 " if (reportOnFetch) {"
194 " reportRequests();"
195 " }"
196 " }, false);"
197 "</script>");
198 }
199
200 void InitializeServiceWorkerFetchTestPage() {
201 // The message "READY" will be sent when the service worker is activated.
202 const base::string16 expected_title = base::ASCIIToUTF16("READY");
203 content::TitleWatcher title_watcher(
204 browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
205 ui_test_utils::NavigateToURL(browser(),
206 embedded_test_server()->GetURL("/test.html"));
207 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
208 }
209 };
210
211 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchTest, EmbedPdfSameOrigin) {
212 // <embed src="test.pdf">
213 const std::string result(ExecuteScriptAndExtractString(
214 "var embed = document.createElement('embed');"
215 "embed.src = 'test.pdf';"
216 "document.body.appendChild(embed);"));
217 EXPECT_EQ(RequsestString(GetURL("/test.pdf"), "no-cors", "include"), result);
218 }
219
220 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchTest, EmbedPdfOtherOrigin) {
221 // <embed src="https://www.example.com/test.pdf">
222 const std::string result(ExecuteScriptAndExtractString(
223 "var embed = document.createElement('embed');"
224 "embed.src = 'https://www.example.com/test.pdf';"
225 "document.body.appendChild(embed);"));
226 EXPECT_EQ(
227 RequsestString("https://www.example.com/test.pdf", "no-cors", "include"),
228 result);
229 }
230
231 class ChromeServiceWorkerManifestFetchTest
232 : public ChromeServiceWorkerFetchTest {
233 protected:
234 ChromeServiceWorkerManifestFetchTest() {}
235
236 void SetUpCommandLine(base::CommandLine* command_line) override {
237 ChromeServiceWorkerFetchTest::SetUpCommandLine(command_line);
238 command_line->AppendSwitch(switches::kEnableAddToShelf);
239 }
240
241 std::string ExecuteManifestFetchTest(const std::string& url,
242 const std::string& cross_origin) {
243 std::string js(
244 base::StringPrintf("reportOnFetch = false;"
245 "var link = document.createElement('link');"
246 "link.rel = 'manifest';"
247 "link.href = '%s';",
248 url.c_str()));
249 if (cross_origin.length() != 0) {
nhiroki 2016/02/09 04:48:42 "!cross_origin.empty()"
horo 2016/02/10 06:09:22 Done.
250 js +=
251 base::StringPrintf("link.crossOrigin = '%s';", cross_origin.c_str());
252 }
253 js += "document.head.appendChild(link);";
254 ExecuteJavaScriptForTests(js);
255 return RequestAppBannerAndGetFetchedRequests();
256 }
257
258 private:
259 void ExecuteJavaScriptForTests(const std::string& js) {
260 browser()
261 ->tab_strip_model()
262 ->GetActiveWebContents()
263 ->GetMainFrame()
264 ->ExecuteJavaScriptForTests(base::ASCIIToUTF16(js));
265 }
266
267 std::string RequestAppBannerAndGetFetchedRequests() {
268 EXPECT_TRUE(browser()->RequestAppBanner(
269 browser()->tab_strip_model()->GetActiveWebContents()));
270 return ExecuteScriptAndExtractString(
271 "if (fetchedRequests.length != 0) reportRequests();"
272 "else reportOnFetch = true;");
273 }
274 };
275
276 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerManifestFetchTest, SameOrigin) {
277 // <link rel="manifest" href="manifest.json">
278 EXPECT_EQ(RequsestString(GetURL("/manifest.json"), "cors", "same-origin"),
279 ExecuteManifestFetchTest("manifest.json", ""));
280 }
281
nhiroki 2016/02/09 04:48:42 Should we test 'crossOrigin="anonymous"'?
horo 2016/02/10 06:09:22 ManifestFetcher supports only "use-credentials".
nhiroki 2016/02/10 06:46:10 Acknowledged.
282 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerManifestFetchTest,
283 SameOriginUseCredentials) {
284 // <link rel="manifest" href="manifest.json" crossorigin="use-credentials">
285 EXPECT_EQ(RequsestString(GetURL("/manifest.json"), "cors", "include"),
286 ExecuteManifestFetchTest("manifest.json", "use-credentials"));
287 }
288
289 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerManifestFetchTest, OtherOrigin) {
290 // <link rel="manifest" href="https://www.example.com/manifest.json">
291 EXPECT_EQ(
292 RequsestString("https://www.example.com/manifest.json", "cors",
293 "same-origin"),
294 ExecuteManifestFetchTest("https://www.example.com/manifest.json", ""));
295 }
296
nhiroki 2016/02/09 04:48:42 ditto: 'crossOrigin="anonymous"'
297 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerManifestFetchTest,
298 OtherOriginUseCredentials) {
299 // <link rel="manifest" href="https://www.example.com/manifest.json"
300 // crossorigin="use-credentials">
301 EXPECT_EQ(RequsestString("https://www.example.com/manifest.json", "cors",
302 "include"),
303 ExecuteManifestFetchTest("https://www.example.com/manifest.json",
304 "use-credentials"));
305 }
306
307 class ChromeServiceWorkerFetchPPAPITest : public ChromeServiceWorkerFetchTest {
308 protected:
309 ChromeServiceWorkerFetchPPAPITest() {}
310
311 void SetUpOnMainThread() override {
312 base::FilePath document_root;
313 ASSERT_TRUE(ui_test_utils::GetRelativeBuildDirectory(&document_root));
314 embedded_test_server()->AddDefaultHandlers(
315 document_root.Append(FILE_PATH_LITERAL("nacl_test_data"))
316 .Append(FILE_PATH_LITERAL("pnacl")));
317 ChromeServiceWorkerFetchTest::SetUpOnMainThread();
318 test_page_url_ = GetURL("/pnacl_url_loader.html");
319 }
320
321 std::string GetRequsestStringForPNACL() const {
nhiroki 2016/02/09 04:48:42 s/Requsest/Request/
horo 2016/02/10 06:09:22 Done.
322 return RequsestString(test_page_url_, "navigate", "include") +
323 RequsestString(GetURL("/pnacl_url_loader.nmf"), "same-origin",
324 "include") +
325 RequsestString(GetURL("/pnacl_url_loader_newlib_pnacl.pexe"),
326 "same-origin", "include");
327 }
328
329 std::string ExecutePNACLUrlLoaderTest(const std::string& mode) {
330 std::string result(ExecuteScriptAndExtractString(
331 base::StringPrintf("reportOnFetch = false;"
332 "var iframe = document.createElement('iframe');"
333 "iframe.src='%s#%s';"
334 "document.body.appendChild(iframe);",
335 test_page_url_.c_str(), mode.c_str())));
336 EXPECT_EQ(base::StringPrintf("OnOpen%s", mode.c_str()), result);
337 return ExecuteScriptAndExtractString("reportRequests();");
338 }
339
340 private:
341 std::string test_page_url_;
342 };
343
344 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest, SameOrigin) {
345 // In pnacl_url_loader.cc:
346 // request.SetMethod("GET");
347 // request.SetURL("/echo");
348 EXPECT_EQ(GetRequsestStringForPNACL() +
349 RequsestString(GetURL("/echo"), "same-origin", "include"),
350 ExecutePNACLUrlLoaderTest("Same"));
351 }
352
353 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest, SameOriginCORS) {
354 // In pnacl_url_loader.cc:
355 // request.SetMethod("GET");
356 // request.SetURL("/echo");
357 // request.SetAllowCrossOriginRequests(true);
358 EXPECT_EQ(GetRequsestStringForPNACL() +
359 RequsestString(GetURL("/echo"), "cors", "same-origin"),
360 ExecutePNACLUrlLoaderTest("SameCORS"));
361 }
362
363 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest,
364 SameOriginCredentials) {
365 // In pnacl_url_loader.cc:
366 // request.SetMethod("GET");
367 // request.SetURL("/echo");
368 // request.SetAllowCredentials(true);
369 EXPECT_EQ(GetRequsestStringForPNACL() +
370 RequsestString(GetURL("/echo"), "same-origin", "include"),
371 ExecutePNACLUrlLoaderTest("SameCredentials"));
372 }
373
374 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest,
375 SameOriginCORSCredentials) {
376 // In pnacl_url_loader.cc:
377 // request.SetMethod("GET");
378 // request.SetURL("/echo");
379 // request.SetAllowCrossOriginRequests(true);
380 // request.SetAllowCredentials(true);
381 EXPECT_EQ(GetRequsestStringForPNACL() +
382 RequsestString(GetURL("/echo"), "cors", "include"),
383 ExecutePNACLUrlLoaderTest("SameCORSCredentials"));
384 }
385
386 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest, OtherOrigin) {
387 // In pnacl_url_loader.cc:
388 // request.SetMethod("GET");
389 // request.SetURL("https://www.example.com/echo");
390 // This request fails because AllowCrossOriginRequests is not set.
391 EXPECT_EQ(GetRequsestStringForPNACL(), ExecutePNACLUrlLoaderTest("Other"));
392 }
393
394 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest, OtherOriginCORS) {
395 // In pnacl_url_loader.cc:
396 // request.SetMethod("GET");
397 // request.SetURL("https://www.example.com/echo");
398 // request.SetAllowCrossOriginRequests(true);
399 EXPECT_EQ(
400 GetRequsestStringForPNACL() +
401 RequsestString("https://www.example.com/echo", "cors", "same-origin"),
402 ExecutePNACLUrlLoaderTest("OtherCORS"));
403 }
404
405 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest,
406 OtherOriginCredentials) {
407 // In pnacl_url_loader.cc:
408 // request.SetMethod("GET");
409 // request.SetURL("https://www.example.com/echo");
410 // request.SetAllowCredentials(true);
411 // This request fails because AllowCrossOriginRequests is not set.
412 EXPECT_EQ(GetRequsestStringForPNACL(),
413 ExecutePNACLUrlLoaderTest("OtherCredentials"));
414 }
415
416 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest,
417 OtherOriginCORSCredentials) {
418 // In pnacl_url_loader.cc:
419 // request.SetMethod("GET");
420 // request.SetURL("https://www.example.com/echo");
421 // request.SetAllowCrossOriginRequests(true);
422 // request.SetAllowCredentials(true);
423 EXPECT_EQ(
424 GetRequsestStringForPNACL() +
425 RequsestString("https://www.example.com/echo", "cors", "include"),
426 ExecutePNACLUrlLoaderTest("OtherCORSCredentials"));
427 }
428
429 class ChromeServiceWorkerFetchPPAPIPrivateTest
430 : public ChromeServiceWorkerFetchPPAPITest {
431 protected:
432 ChromeServiceWorkerFetchPPAPIPrivateTest() {}
433
434 void SetUpCommandLine(base::CommandLine* command_line) override {
435 ChromeServiceWorkerFetchPPAPITest::SetUpCommandLine(command_line);
436 // Sets this flag to test that the fetch request from the plugins with
437 // private permission (PERMISSION_PRIVATE) should not go to the service
438 // worker.
439 command_line->AppendSwitch(switches::kEnablePepperTesting);
440 }
441 };
442
443 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest, SameOrigin) {
444 EXPECT_EQ(GetRequsestStringForPNACL(), ExecutePNACLUrlLoaderTest("Same"));
445 }
446
447 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest,
448 SameOriginCORS) {
449 EXPECT_EQ(GetRequsestStringForPNACL(), ExecutePNACLUrlLoaderTest("SameCORS"));
450 }
451
452 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest,
453 SameOriginCredentials) {
454 EXPECT_EQ(GetRequsestStringForPNACL(),
455 ExecutePNACLUrlLoaderTest("SameCredentials"));
456 }
457
458 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest,
459 SameOriginCORSCredentials) {
460 EXPECT_EQ(GetRequsestStringForPNACL(),
461 ExecutePNACLUrlLoaderTest("SameCORSCredentials"));
462 }
463
464 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest, OtherOrigin) {
465 EXPECT_EQ(GetRequsestStringForPNACL(), ExecutePNACLUrlLoaderTest("Other"));
466 }
467
468 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest,
469 OtherOriginCORS) {
470 EXPECT_EQ(GetRequsestStringForPNACL(),
471 ExecutePNACLUrlLoaderTest("OtherCORS"));
472 }
473
474 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest,
475 OtherOriginCredentials) {
476 EXPECT_EQ(GetRequsestStringForPNACL(),
477 ExecutePNACLUrlLoaderTest("OtherCredentials"));
478 }
479
480 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest,
481 OtherOriginCORSCredentials) {
482 EXPECT_EQ(GetRequsestStringForPNACL(),
483 ExecutePNACLUrlLoaderTest("OtherCORSCredentials"));
484 }
485
112 } // namespace 486 } // namespace
OLDNEW
« no previous file with comments | « no previous file | chrome/chrome_tests.gypi » ('j') | chrome/test/data/nacl/pnacl_url_loader/pnacl_url_loader.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698