image/test/mochitest/bug496292-1.sjs
author Andrew Osmond <aosmond@mozilla.com>
Tue, 09 Oct 2018 10:01:26 -0400
changeset 446579 f8fefc9626c12c7e3f48bdd82baab9fc4edec345
parent 443174 649c36f9f0a69ddc8116cbd4b10a565d4a667332
permissions -rw-r--r--
Bug 1503653 - Part 2. Enable WebP by default. r=tnikkel Bug 1249474 suggested that we add image/webp to the front of the Accept header for images, to indicate to servers that we actually support WebP. Differential Revision: https://phabricator.services.mozilla.com/D8120

function handleRequest(request, response)
{
  var file = Components.classes["@mozilla.org/file/directory_service;1"]
             .getService(Components.interfaces.nsIProperties)
             .get("CurWorkD", Components.interfaces.nsIFile);

  file.append("tests");
  file.append("image");
  file.append("test");
  file.append("mochitest");

  if (request.getHeader("Accept") == "image/webp,*/*") {
    file.append('blue.png');
  } else {
    file.append('red.png');
  }
  response.setStatusLine(request.httpVersion, 200, "OK");
  response.setHeader("Content-Type", "image/png", false);
  response.setHeader("Cache-Control", "no-cache", false);

  var fileStream = Components.classes['@mozilla.org/network/file-input-stream;1']
                   .createInstance(Components.interfaces.nsIFileInputStream);
  fileStream.init(file, 1, 0, false);
  var binaryStream = Components.classes['@mozilla.org/binaryinputstream;1']
                     .createInstance(Components.interfaces.nsIBinaryInputStream);
  binaryStream.setInputStream(fileStream);

  response.bodyOutputStream.writeFrom(binaryStream, binaryStream.available());

  binaryStream.close();
  fileStream.close();
}