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

Unified 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 side-by-side diff with in-line comments
Download patch
« 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')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/chrome_service_worker_browsertest.cc
diff --git a/chrome/browser/chrome_service_worker_browsertest.cc b/chrome/browser/chrome_service_worker_browsertest.cc
index e6c32fd7a9a2760c577ad2e2341dfcdf2a86cb13..61fa653bfdefcc0930563e0edaaca2cb3e6e7b6b 100644
--- a/chrome/browser/chrome_service_worker_browsertest.cc
+++ b/chrome/browser/chrome_service_worker_browsertest.cc
@@ -6,21 +6,28 @@
// embedder.
#include "base/bind.h"
+#include "base/command_line.h"
#include "base/files/scoped_temp_dir.h"
#include "base/numerics/safe_conversions.h"
#include "base/run_loop.h"
+#include "base/strings/stringprintf.h"
+#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
+#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/browser_context.h"
+#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/service_worker_context.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
+#include "content/public/test/browser_test_utils.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
+#include "ppapi/shared_impl/ppapi_switches.h"
namespace {
@@ -109,4 +116,371 @@ IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest,
// Test passes if we don't crash.
}
+class ChromeServiceWorkerFetchTest : public ChromeServiceWorkerTest {
+ protected:
+ ChromeServiceWorkerFetchTest() {}
+
+ void SetUpOnMainThread() override {
+ WriteServiceWorkerFetchTestFiles();
+ embedded_test_server()->ServeFilesFromDirectory(service_worker_dir_.path());
+ ASSERT_TRUE(embedded_test_server()->Start());
+ InitializeServiceWorkerFetchTestPage();
+ }
+
+ std::string ExecuteScriptAndExtractString(const std::string& js) {
+ std::string result;
+ EXPECT_TRUE(content::ExecuteScriptAndExtractString(
+ browser()->tab_strip_model()->GetActiveWebContents(), js, &result));
+ return result;
+ }
+
+ 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.
+ const std::string& mode,
+ const std::string& credentials) const {
+ return base::StringPrintf("url:%s, mode:%s, credentials:%s\n", url.c_str(),
+ mode.c_str(), credentials.c_str());
+ }
+
+ std::string GetURL(const std::string& relative_url) const {
+ return embedded_test_server()->GetURL(relative_url).spec();
+ }
+
+ private:
+ void WriteServiceWorkerFetchTestFiles() {
+ WriteFile(FILE_PATH_LITERAL("sw.js"),
+ "this.onactivate = function(event) {"
+ " 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.
+ "};"
+ "this.onfetch = function(event) {"
+ " event.respondWith("
+ " self.clients.matchAll().then(function(clients) {"
+ " clients.forEach(function(client) {"
+ " client.postMessage("
+ " 'url:' + event.request.url + ', ' +"
+ " 'mode:' + event.request.mode + ', ' +"
+ " 'credentials:' + event.request.credentials"
+ " );"
+ " });"
+ " return fetch(event.request);"
+ " }));"
+ "};");
+ WriteFile(FILE_PATH_LITERAL("test.html"),
+ "<script>"
+ "navigator.serviceWorker.register('./sw.js', {scope: './'})"
+ " .then(function(reg) {"
+ " reg.addEventListener('updatefound', function() {"
+ " var worker = reg.installing;"
+ " worker.addEventListener('statechange', function() {"
+ " if (worker.state == 'activated')"
+ " document.title = 'READY';"
+ " });"
+ " });"
+ " });"
+ "var reportOnFetch = true;"
+ "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.
+ "function reportRequests() {"
+ " var str = '';"
+ " fetchedRequests.forEach(function(data) {"
+ " str += data + '\\n';"
+ " });"
+ " window.domAutomationController.setAutomationId(0);"
+ " window.domAutomationController.send(str);"
+ "}"
+ "navigator.serviceWorker.addEventListener("
+ " 'message',"
+ " function(event) {"
+ " fetchedRequests.push(event.data);"
+ " if (reportOnFetch) {"
+ " reportRequests();"
+ " }"
+ " }, false);"
+ "</script>");
+ }
+
+ void InitializeServiceWorkerFetchTestPage() {
+ // The message "READY" will be sent when the service worker is activated.
+ const base::string16 expected_title = base::ASCIIToUTF16("READY");
+ content::TitleWatcher title_watcher(
+ browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
+ ui_test_utils::NavigateToURL(browser(),
+ embedded_test_server()->GetURL("/test.html"));
+ EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
+ }
+};
+
+IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchTest, EmbedPdfSameOrigin) {
+ // <embed src="test.pdf">
+ const std::string result(ExecuteScriptAndExtractString(
+ "var embed = document.createElement('embed');"
+ "embed.src = 'test.pdf';"
+ "document.body.appendChild(embed);"));
+ EXPECT_EQ(RequsestString(GetURL("/test.pdf"), "no-cors", "include"), result);
+}
+
+IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchTest, EmbedPdfOtherOrigin) {
+ // <embed src="https://www.example.com/test.pdf">
+ const std::string result(ExecuteScriptAndExtractString(
+ "var embed = document.createElement('embed');"
+ "embed.src = 'https://www.example.com/test.pdf';"
+ "document.body.appendChild(embed);"));
+ EXPECT_EQ(
+ RequsestString("https://www.example.com/test.pdf", "no-cors", "include"),
+ result);
+}
+
+class ChromeServiceWorkerManifestFetchTest
+ : public ChromeServiceWorkerFetchTest {
+ protected:
+ ChromeServiceWorkerManifestFetchTest() {}
+
+ void SetUpCommandLine(base::CommandLine* command_line) override {
+ ChromeServiceWorkerFetchTest::SetUpCommandLine(command_line);
+ command_line->AppendSwitch(switches::kEnableAddToShelf);
+ }
+
+ std::string ExecuteManifestFetchTest(const std::string& url,
+ const std::string& cross_origin) {
+ std::string js(
+ base::StringPrintf("reportOnFetch = false;"
+ "var link = document.createElement('link');"
+ "link.rel = 'manifest';"
+ "link.href = '%s';",
+ url.c_str()));
+ if (cross_origin.length() != 0) {
nhiroki 2016/02/09 04:48:42 "!cross_origin.empty()"
horo 2016/02/10 06:09:22 Done.
+ js +=
+ base::StringPrintf("link.crossOrigin = '%s';", cross_origin.c_str());
+ }
+ js += "document.head.appendChild(link);";
+ ExecuteJavaScriptForTests(js);
+ return RequestAppBannerAndGetFetchedRequests();
+ }
+
+ private:
+ void ExecuteJavaScriptForTests(const std::string& js) {
+ browser()
+ ->tab_strip_model()
+ ->GetActiveWebContents()
+ ->GetMainFrame()
+ ->ExecuteJavaScriptForTests(base::ASCIIToUTF16(js));
+ }
+
+ std::string RequestAppBannerAndGetFetchedRequests() {
+ EXPECT_TRUE(browser()->RequestAppBanner(
+ browser()->tab_strip_model()->GetActiveWebContents()));
+ return ExecuteScriptAndExtractString(
+ "if (fetchedRequests.length != 0) reportRequests();"
+ "else reportOnFetch = true;");
+ }
+};
+
+IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerManifestFetchTest, SameOrigin) {
+ // <link rel="manifest" href="manifest.json">
+ EXPECT_EQ(RequsestString(GetURL("/manifest.json"), "cors", "same-origin"),
+ ExecuteManifestFetchTest("manifest.json", ""));
+}
+
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.
+IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerManifestFetchTest,
+ SameOriginUseCredentials) {
+ // <link rel="manifest" href="manifest.json" crossorigin="use-credentials">
+ EXPECT_EQ(RequsestString(GetURL("/manifest.json"), "cors", "include"),
+ ExecuteManifestFetchTest("manifest.json", "use-credentials"));
+}
+
+IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerManifestFetchTest, OtherOrigin) {
+ // <link rel="manifest" href="https://www.example.com/manifest.json">
+ EXPECT_EQ(
+ RequsestString("https://www.example.com/manifest.json", "cors",
+ "same-origin"),
+ ExecuteManifestFetchTest("https://www.example.com/manifest.json", ""));
+}
+
nhiroki 2016/02/09 04:48:42 ditto: 'crossOrigin="anonymous"'
+IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerManifestFetchTest,
+ OtherOriginUseCredentials) {
+ // <link rel="manifest" href="https://www.example.com/manifest.json"
+ // crossorigin="use-credentials">
+ EXPECT_EQ(RequsestString("https://www.example.com/manifest.json", "cors",
+ "include"),
+ ExecuteManifestFetchTest("https://www.example.com/manifest.json",
+ "use-credentials"));
+}
+
+class ChromeServiceWorkerFetchPPAPITest : public ChromeServiceWorkerFetchTest {
+ protected:
+ ChromeServiceWorkerFetchPPAPITest() {}
+
+ void SetUpOnMainThread() override {
+ base::FilePath document_root;
+ ASSERT_TRUE(ui_test_utils::GetRelativeBuildDirectory(&document_root));
+ embedded_test_server()->AddDefaultHandlers(
+ document_root.Append(FILE_PATH_LITERAL("nacl_test_data"))
+ .Append(FILE_PATH_LITERAL("pnacl")));
+ ChromeServiceWorkerFetchTest::SetUpOnMainThread();
+ test_page_url_ = GetURL("/pnacl_url_loader.html");
+ }
+
+ std::string GetRequsestStringForPNACL() const {
nhiroki 2016/02/09 04:48:42 s/Requsest/Request/
horo 2016/02/10 06:09:22 Done.
+ return RequsestString(test_page_url_, "navigate", "include") +
+ RequsestString(GetURL("/pnacl_url_loader.nmf"), "same-origin",
+ "include") +
+ RequsestString(GetURL("/pnacl_url_loader_newlib_pnacl.pexe"),
+ "same-origin", "include");
+ }
+
+ std::string ExecutePNACLUrlLoaderTest(const std::string& mode) {
+ std::string result(ExecuteScriptAndExtractString(
+ base::StringPrintf("reportOnFetch = false;"
+ "var iframe = document.createElement('iframe');"
+ "iframe.src='%s#%s';"
+ "document.body.appendChild(iframe);",
+ test_page_url_.c_str(), mode.c_str())));
+ EXPECT_EQ(base::StringPrintf("OnOpen%s", mode.c_str()), result);
+ return ExecuteScriptAndExtractString("reportRequests();");
+ }
+
+ private:
+ std::string test_page_url_;
+};
+
+IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest, SameOrigin) {
+ // In pnacl_url_loader.cc:
+ // request.SetMethod("GET");
+ // request.SetURL("/echo");
+ EXPECT_EQ(GetRequsestStringForPNACL() +
+ RequsestString(GetURL("/echo"), "same-origin", "include"),
+ ExecutePNACLUrlLoaderTest("Same"));
+}
+
+IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest, SameOriginCORS) {
+ // In pnacl_url_loader.cc:
+ // request.SetMethod("GET");
+ // request.SetURL("/echo");
+ // request.SetAllowCrossOriginRequests(true);
+ EXPECT_EQ(GetRequsestStringForPNACL() +
+ RequsestString(GetURL("/echo"), "cors", "same-origin"),
+ ExecutePNACLUrlLoaderTest("SameCORS"));
+}
+
+IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest,
+ SameOriginCredentials) {
+ // In pnacl_url_loader.cc:
+ // request.SetMethod("GET");
+ // request.SetURL("/echo");
+ // request.SetAllowCredentials(true);
+ EXPECT_EQ(GetRequsestStringForPNACL() +
+ RequsestString(GetURL("/echo"), "same-origin", "include"),
+ ExecutePNACLUrlLoaderTest("SameCredentials"));
+}
+
+IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest,
+ SameOriginCORSCredentials) {
+ // In pnacl_url_loader.cc:
+ // request.SetMethod("GET");
+ // request.SetURL("/echo");
+ // request.SetAllowCrossOriginRequests(true);
+ // request.SetAllowCredentials(true);
+ EXPECT_EQ(GetRequsestStringForPNACL() +
+ RequsestString(GetURL("/echo"), "cors", "include"),
+ ExecutePNACLUrlLoaderTest("SameCORSCredentials"));
+}
+
+IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest, OtherOrigin) {
+ // In pnacl_url_loader.cc:
+ // request.SetMethod("GET");
+ // request.SetURL("https://www.example.com/echo");
+ // This request fails because AllowCrossOriginRequests is not set.
+ EXPECT_EQ(GetRequsestStringForPNACL(), ExecutePNACLUrlLoaderTest("Other"));
+}
+
+IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest, OtherOriginCORS) {
+ // In pnacl_url_loader.cc:
+ // request.SetMethod("GET");
+ // request.SetURL("https://www.example.com/echo");
+ // request.SetAllowCrossOriginRequests(true);
+ EXPECT_EQ(
+ GetRequsestStringForPNACL() +
+ RequsestString("https://www.example.com/echo", "cors", "same-origin"),
+ ExecutePNACLUrlLoaderTest("OtherCORS"));
+}
+
+IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest,
+ OtherOriginCredentials) {
+ // In pnacl_url_loader.cc:
+ // request.SetMethod("GET");
+ // request.SetURL("https://www.example.com/echo");
+ // request.SetAllowCredentials(true);
+ // This request fails because AllowCrossOriginRequests is not set.
+ EXPECT_EQ(GetRequsestStringForPNACL(),
+ ExecutePNACLUrlLoaderTest("OtherCredentials"));
+}
+
+IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest,
+ OtherOriginCORSCredentials) {
+ // In pnacl_url_loader.cc:
+ // request.SetMethod("GET");
+ // request.SetURL("https://www.example.com/echo");
+ // request.SetAllowCrossOriginRequests(true);
+ // request.SetAllowCredentials(true);
+ EXPECT_EQ(
+ GetRequsestStringForPNACL() +
+ RequsestString("https://www.example.com/echo", "cors", "include"),
+ ExecutePNACLUrlLoaderTest("OtherCORSCredentials"));
+}
+
+class ChromeServiceWorkerFetchPPAPIPrivateTest
+ : public ChromeServiceWorkerFetchPPAPITest {
+ protected:
+ ChromeServiceWorkerFetchPPAPIPrivateTest() {}
+
+ void SetUpCommandLine(base::CommandLine* command_line) override {
+ ChromeServiceWorkerFetchPPAPITest::SetUpCommandLine(command_line);
+ // Sets this flag to test that the fetch request from the plugins with
+ // private permission (PERMISSION_PRIVATE) should not go to the service
+ // worker.
+ command_line->AppendSwitch(switches::kEnablePepperTesting);
+ }
+};
+
+IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest, SameOrigin) {
+ EXPECT_EQ(GetRequsestStringForPNACL(), ExecutePNACLUrlLoaderTest("Same"));
+}
+
+IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest,
+ SameOriginCORS) {
+ EXPECT_EQ(GetRequsestStringForPNACL(), ExecutePNACLUrlLoaderTest("SameCORS"));
+}
+
+IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest,
+ SameOriginCredentials) {
+ EXPECT_EQ(GetRequsestStringForPNACL(),
+ ExecutePNACLUrlLoaderTest("SameCredentials"));
+}
+
+IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest,
+ SameOriginCORSCredentials) {
+ EXPECT_EQ(GetRequsestStringForPNACL(),
+ ExecutePNACLUrlLoaderTest("SameCORSCredentials"));
+}
+
+IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest, OtherOrigin) {
+ EXPECT_EQ(GetRequsestStringForPNACL(), ExecutePNACLUrlLoaderTest("Other"));
+}
+
+IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest,
+ OtherOriginCORS) {
+ EXPECT_EQ(GetRequsestStringForPNACL(),
+ ExecutePNACLUrlLoaderTest("OtherCORS"));
+}
+
+IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest,
+ OtherOriginCredentials) {
+ EXPECT_EQ(GetRequsestStringForPNACL(),
+ ExecutePNACLUrlLoaderTest("OtherCredentials"));
+}
+
+IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest,
+ OtherOriginCORSCredentials) {
+ EXPECT_EQ(GetRequsestStringForPNACL(),
+ ExecutePNACLUrlLoaderTest("OtherCORSCredentials"));
+}
+
} // namespace
« 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