Add a test for response type for extension resources

This is a regression test for https://crrev.com/c/1980649. Resources
contained in an extension should be accessible from the extension's
background page.

Bug: 1035575, 1036693,1026546
Change-Id: Ic08cec5d526cc5594a6bf507deca43c96d6258f2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1981419
Commit-Queue: Yutaka Hirano <[email protected]>
Reviewed-by: Takashi Toyoshima <[email protected]>
Reviewed-by: Karan Bhatia <[email protected]>
Cr-Commit-Position: refs/heads/master@{#729233}
diff --git a/chrome/browser/extensions/fetch_apitest.cc b/chrome/browser/extensions/fetch_apitest.cc
index 2eae605..8d857d2 100644
--- a/chrome/browser/extensions/fetch_apitest.cc
+++ b/chrome/browser/extensions/fetch_apitest.cc
@@ -334,6 +334,39 @@
             ExecuteScriptInBackgroundPage(extension->id(), script));
 }
 
+// An extension background script should be able to fetch resources contained in
+// the extension, and those resources should not be opaque.
+IN_PROC_BROWSER_TEST_F(ExtensionFetchTest, ExtensionResourceShouldNotBeOpaque) {
+  // We use a script to test this feature. Ideally testing with fetch() and
+  // response type is better, but some logic in blink (see the manual
+  // response type handling in blink::FetchManager) would hide potential
+  // breakages, which is why we are using a script.
+  const std::string script = base::StringPrintf(R"(
+      const script = document.createElement('script');
+      window.onerror = (message) => {
+        window.domAutomationController.send('onerror: ' + message);
+      }
+      script.src = 'error.js'
+      document.body.appendChild(script);)");
+  TestExtensionDir dir;
+  dir.WriteManifest(R"JSON(
+     {
+      "background": {"scripts": ["bg.js"]},
+      "manifest_version": 2,
+      "name": "FetchResponseType",
+      "permissions": [],
+      "version": "1"
+     })JSON");
+  dir.WriteFile(FILE_PATH_LITERAL("error.js"), "throw TypeError('hi!')");
+  const Extension* extension = WriteFilesAndLoadTestExtension(&dir);
+  ASSERT_TRUE(extension);
+
+  // We expect that we can read the content of the error here. Otherwise
+  // "onerror: Script error." will be seen.
+  EXPECT_EQ("onerror: Uncaught TypeError: hi!",
+            ExecuteScriptInBackgroundPage(extension->id(), script));
+}
+
 }  // namespace
 
 }  // namespace extensions