Bug 1417463 - Default accept header should follow the fetch spec - tests, r=mayhemer
authorAndrea Marchesini <amarchesini@mozilla.com>
Tue, 08 Jan 2019 23:36:17 +0100
changeset 510092 2a7dce7a42ea0527694c978e98d9af1687aecb36
parent 510091 fdf7342d19f7af1d2620bdf5db38e745ebbc08b7
child 510093 f6a85c2b0dffda801e097f6efea14b46a35626e2
push id10547
push userffxbld-merge
push dateMon, 21 Jan 2019 13:03:58 +0000
treeherdermozilla-beta@24ec1916bffe [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmayhemer
bugs1417463
milestone66.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1417463 - Default accept header should follow the fetch spec - tests, r=mayhemer
devtools/client/netmonitor/test/browser_net_copy_headers.js
netwerk/test/mochitests/mochitest.ini
netwerk/test/mochitests/test_accept_header.html
netwerk/test/mochitests/test_accept_header.sjs
--- a/devtools/client/netmonitor/test/browser_net_copy_headers.js
+++ b/devtools/client/netmonitor/test/browser_net_copy_headers.js
@@ -32,17 +32,17 @@ add_task(async function() {
 
   const selectedRequest = getSelectedRequest(store.getState());
   is(selectedRequest, requestItem, "Proper request is selected");
 
   const EXPECTED_REQUEST_HEADERS = [
     `${method} ${SIMPLE_URL} ${httpVersion}`,
     "Host: example.com",
     "User-Agent: " + navigator.userAgent + "",
-    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
+    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
     "Accept-Language: " + navigator.languages.join(",") + ";q=0.5",
     "Accept-Encoding: gzip, deflate",
     "Connection: keep-alive",
     "Upgrade-Insecure-Requests: 1",
     "Pragma: no-cache",
     "Cache-Control: no-cache",
   ].join("\n");
 
--- a/netwerk/test/mochitests/mochitest.ini
+++ b/netwerk/test/mochitests/mochitest.ini
@@ -40,8 +40,10 @@ skip-if = (verify && debug && os == 'mac
 skip-if = verify
 [test_1396395.html]
 [test_1421324.html]
 [test_1425031.html]
 [test_1503201.html]
 [test_origin_header.html]
 [test_1502055.html]
 support-files = sw_1502055.js file_1502055.sjs iframe_1502055.html
+[test_accept_header.html]
+support-files = test_accept_header.sjs
new file mode 100644
--- /dev/null
+++ b/netwerk/test/mochitests/test_accept_header.html
@@ -0,0 +1,106 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Accept header</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+<script>
+
+// All the requests are sent to test_accept_header.sjs which will return
+// different content based on the queryString. When the queryString is 'get',
+// test_accept_header.sjs returns a JSON object with the latest request and its
+// accept header value.
+
+function test_last_request_and_continue(query, expected) {
+  fetch("test_accept_header.sjs?get").then(r => r.json()).then(json => {
+    is(json.type, query, "Expected: " + query);
+    is(json.accept, expected, "Accept header: " + expected);
+    next();
+  });
+}
+
+function test_iframe() {
+  let observer = new PerformanceObserver(function(list, obj) {
+    obj.disconnect();
+
+    list.getEntries().forEach(entry => {
+      if (entry.name.endsWith("test_accept_header.sjs?iframe")) {
+        obj.disconnect();
+        test_last_request_and_continue("iframe", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
+      }
+    });
+  });
+
+  observer.observe({entryTypes: ["resource"]});
+
+  let ifr = document.createElement("iframe");
+  ifr.src = "test_accept_header.sjs?iframe";
+  document.body.appendChild(ifr);
+}
+
+function test_image() {
+  let i = new Image();
+  i.src = "test_accept_header.sjs?image";
+  i.onload = function() {
+    // Fetch spec says we should have: "image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5"
+    test_last_request_and_continue("image", "image/webp,*/*");
+  }
+}
+
+function test_style() {
+  let observer = new PerformanceObserver(function(list, obj) {
+    obj.disconnect();
+
+    list.getEntries().forEach(entry => {
+      if (entry.name.endsWith("test_accept_header.sjs?style")) {
+        obj.disconnect();
+        test_last_request_and_continue("style", "text/css,*/*;q=0.1");
+      }
+    });
+  });
+
+  observer.observe({entryTypes: ["resource"]});
+
+  let head = document.getElementsByTagName("head")[0];
+  let link = document.createElement("link");
+  link.rel = "stylesheet";
+  link.type = "text/css";
+  link.href = "test_accept_header.sjs?style";
+  head.appendChild(link);
+}
+
+function test_worker() {
+  let w = new Worker("test_accept_header.sjs?worker");
+  w.onmessage = function() {
+    test_last_request_and_continue("worker", "*/*");
+  }
+}
+
+let tests = [
+  test_iframe,
+  test_image,
+  test_style,
+  test_worker,
+];
+
+function next() {
+  if (tests.length == 0) {
+    SimpleTest.finish();
+    return;
+  }
+
+  let test = tests.shift();
+  test();
+}
+
+SimpleTest.waitForExplicitFinish();
+
+SpecialPowers.pushPrefEnv({ "set": [
+  [ "dom.enable_performance_observer", true ]
+]}, next);
+
+</script>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/netwerk/test/mochitests/test_accept_header.sjs
@@ -0,0 +1,48 @@
+function handleRequest(request, response) {
+  response.setStatusLine(request.httpVersion, "200", "OK");
+
+  if (request.queryString == "worker") {
+    response.setHeader("Content-Type", "application/json", false);
+    response.write("postMessage(42)");
+
+    setState("data", JSON.stringify({type: "worker", accept: request.getHeader("Accept") }));
+    return;
+  }
+
+  if (request.queryString == "image") {
+    // A 1x1 PNG image.
+    // Source: https://commons.wikimedia.org/wiki/File:1x1.png (Public Domain)
+    const IMAGE = atob("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAA" +
+                       "ACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=");
+
+    response.setHeader("Content-Type", "image/png", false);
+    response.write(IMAGE);
+
+    setState("data", JSON.stringify({type: "image", accept: request.getHeader("Accept") }));
+    return;
+  }
+
+  if (request.queryString == "style") {
+    response.setHeader("Content-Type", "text/css", false);
+    response.write("");
+
+    setState("data", JSON.stringify({type: "style", accept: request.getHeader("Accept") }));
+    return;
+  }
+
+  if (request.queryString == "iframe") {
+    response.setHeader("Content-Type", "text/html", false);
+    response.write("<h1>Hello world!</h1>");
+
+    setState("data", JSON.stringify({type: "iframe", accept: request.getHeader("Accept") }));
+    return;
+  }
+
+  if (request.queryString == "get") {
+    response.setHeader("Content-Type", "text/javascript", false);
+    response.write(getState("data"));
+
+    setState("data", "");
+    return;
+  }
+}