Bug 497003 - Adjust partial content test and revert httpd.js r=jduell
authorSteve Workman <sworkman@mozilla.com>
Fri, 14 Jun 2013 19:34:17 -0700
changeset 146665 26c1d80edf1f27a23a60da8bb6f3709e19b48a44
parent 146664 863ba9c9b7224a94f78fbe6c0e38423d16130083
child 146666 43223a927976550fe5ea0395fe5b57b25f9249a2
push id2697
push userbbajaj@mozilla.com
push dateMon, 05 Aug 2013 18:49:53 +0000
treeherdermozilla-beta@dfec938c7b63 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjduell
bugs497003
milestone24.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 497003 - Adjust partial content test and revert httpd.js r=jduell
netwerk/test/httpserver/httpd.js
netwerk/test/mochitests/partial_content.sjs
netwerk/test/mochitests/test_partially_cached_content.html
--- a/netwerk/test/httpserver/httpd.js
+++ b/netwerk/test/httpserver/httpd.js
@@ -3969,20 +3969,17 @@ Response.prototype =
     if (!this._processAsync)
     {
       dumpn("*** non-async response, set Content-Length");
 
       var bodyStream = this._bodyInputStream;
       var avail = bodyStream ? bodyStream.available() : 0;
 
       // XXX assumes stream will always report the full amount of data available
-      // Set "Content-Length" if not already set by request handler.
-      if (!headers.hasHeader("Content-Length")) {
-        headers.setHeader("Content-Length", "" + avail, false);
-      }
+      headers.setHeader("Content-Length", "" + avail, false);
     }
 
 
     // construct and send response
     dumpn("*** header post-processing completed, sending response head...");
 
     // request-line
     var preambleData = [statusLine];
--- a/netwerk/test/mochitests/partial_content.sjs
+++ b/netwerk/test/mochitests/partial_content.sjs
@@ -1,63 +1,72 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
-// HTTP Version from request, used for response.
-var gHttpVersion;
-
 /* Debug and Error wrapper functions for dump().
  */
 function ERR(response, responseCode, responseCodeStr, msg)
 {
+  // Reset state var.
+  setState("expectedRequestType", "");
   // Dump to console log and send to client in response.
   dump("SERVER ERROR: " + msg + "\n");
-  response.setStatusLine(gHttpVersion, responseCode, responseCodeStr);
+  response.write("HTTP/1.1" + responseCode + responseCodeStr + "\r\n");
+  response.write("Content-Type: text/html; charset=UTF-8\r\n");
+  response.write("Content-Length: " + msg.length + "\r\n");
+  response.write("\r\n");
   response.write(msg);
 }
 
 function DBG(msg)
 {
-  // Dump to console only.
-  dump("SERVER DEBUG: " + msg + "\n");
+  // enable when you want to debug
+  if (0) {
+    // Dump to console only.
+    dump("SERVER DEBUG: " + msg + "\n");
+  }
 }
 
 /* Delivers content in parts to test partially cached content: requires two
  * requests for the same resource.
  *
  * First call will respond with partial content, but a 200 header and
  * Content-Length equal to the full content length. No Range or If-Range
  * headers are allowed in the request.
  *
  * Second call will require Range and If-Range in the request headers, and
  * will respond with the range requested.
  */
 function handleRequest(request, response)
 {
-  // Set http version for error responses.
-  gHttpVersion = request.httpVersion;
+  DBG("Trying to seize power");
+  response.seizePower();
 
-  // All responses, inc. errors, are text/html.
-  response.setHeader("Content-Type", "text/html; charset=UTF-8", false);
-
+  DBG("About to check state vars");
   // Get state var to determine if this is the first or second request.
   var expectedRequestType;
+  var lastModified;
   if (getState("expectedRequestType") === "") {
     DBG("First call: Should be requesting full content.");
     expectedRequestType = "fullRequest";
     // Set state var for second request.
     setState("expectedRequestType", "partialRequest");
+    // Create lastModified variable for responses.
+    lastModified = (new Date()).toUTCString();
+    setState("lastModified", lastModified);
   } else if (getState("expectedRequestType") === "partialRequest") {
     DBG("Second call: Should be requesting undelivered content.");
     expectedRequestType = "partialRequest";
     // Reset state var for first request.
     setState("expectedRequestType", "");
+    // Get last modified date and reset state var.
+    lastModified = getState("lastModified");
   } else {
     ERR(response, 500, "Internal Server Error",
         "Invalid expectedRequestType \"" + expectedRequestType + "\"in " +
         "server state db.");
     return;
   }
 
   // Look for Range and If-Range
@@ -100,34 +109,46 @@ function handleRequest(request, response
                        "First response</p>";
   var remainderContent = "<p id=\"secondResponse\">Second response</p>" +
                          "</body></html>";
   var totalLength = partialContent.length + remainderContent.length;
 
   DBG("totalLength: " + totalLength);
 
   // Prepare common headers for the two responses.
-  response.setHeader("ETag", "abcd0123", false);
-  response.setHeader("Accept-Ranges", "bytes", false);
+  date = new Date();
+  DBG("Date: " + date.toUTCString() + ", Last-Modified: " + lastModified);
+  var commonHeaders = "Date: " + date.toUTCString() + "\r\n" +
+                      "Last-Modified: " + lastModified + "\r\n" +
+                      "Content-Type: text/html; charset=UTF-8\r\n" +
+                      "ETag: abcd0123\r\n" +
+                      "Accept-Ranges: bytes\r\n";
+
 
   // Prepare specific headers and content for first and second responses.
   if (expectedRequestType === "fullRequest") {
     DBG("First response: Sending partial content with a full header");
-    response.setStatusLine(request.httpVersion, 200, "OK");
-    response.write(partialContent, partialContent.length);
+    response.write("HTTP/1.1 200 OK\r\n");
+    response.write(commonHeaders);
     // Set Content-Length to full length of resource.
-    response.setHeader("Content-Length", "" + totalLength, false);
+    response.write("Content-Length: " + totalLength + "\r\n");
+    response.write("\r\n");
+    response.write(partialContent);
   } else if (expectedRequestType === "partialRequest") {
     DBG("Second response: Sending remaining content with a range header");
-    response.setStatusLine(request.httpVersion, 206, "Partial Content");
-    response.setHeader("Content-Range", "bytes " + partialContent.length + "-" +
-                       (totalLength - 1) + "/" + totalLength);
+    response.write("HTTP/1.1 206 Partial Content\r\n");
+    response.write(commonHeaders);
+    // Set Content-Length to length of bytes transmitted.
+    response.write("Content-Length: " + remainderContent.length + "\r\n");
+    response.write("Content-Range: bytes " + partialContent.length + "-" +
+                   (totalLength - 1) + "/" + totalLength + "\r\n");
+    response.write("\r\n");
     response.write(remainderContent);
-    // Set Content-Length to length of bytes transmitted.
-    response.setHeader("Content-Length", "" + remainderContent.length, false);
   } else {
     // Somewhat redundant, but a check for errors in this test code.
     ERR(response, 500, "Internal Server Error",
        "Something very bad happened here: expectedRequestType is invalid " +
        "towards the end of handleRequest! - \"" + expectedRequestType + "\"");
     return;
   }
+
+  response.finish();
 }
--- a/netwerk/test/mochitests/test_partially_cached_content.html
+++ b/netwerk/test/mochitests/test_partially_cached_content.html
@@ -28,17 +28,21 @@
  */
 function expectInitialContent(e) {
   info("expectInitialContent",
        "First response received: should have partial content");
   var frameWindow = document.getElementById('contentFrame').contentWindow;
 
   // Expect "First response" in received HTML.
   var firstResponse = frameWindow.document.getElementById('firstResponse');
-  is(firstResponse.innerHTML, "First response", "firstResponse");
+  ok(firstResponse, "First response should exist");
+  if (firstResponse) {
+    is(firstResponse.innerHTML, "First response",
+       "First response should be correct");
+  }
 
   // Expect NOT to get any second response element.
   var secondResponse = frameWindow.document.getElementById('secondResponse');
   ok(!secondResponse, "Should not get text for second response in first.");
 
   // Set up listener for second load.
   e.target.removeEventListener("load", expectInitialContent, false);
   e.target.addEventListener("load", expectFullContent, false);
@@ -52,21 +56,29 @@ function expectInitialContent(e) {
 function expectFullContent(e)
 {
   info("expectFullContent",
        "Second response received: should complete content from first load");
   var frameWindow = document.getElementById('contentFrame').contentWindow;
 
   // Expect "First response" to still be there
   var firstResponse = frameWindow.document.getElementById('firstResponse');
-  is(firstResponse.innerHTML, "First response", "firstResponse");
+  ok(firstResponse, "First response should exist");
+  if (firstResponse) {
+    is(firstResponse.innerHTML, "First response",
+       "First response should be correct");
+  }
 
   // Expect "Second response" to be there also.
   var secondResponse = frameWindow.document.getElementById('secondResponse');
-  is(secondResponse.innerHTML, "Second response", "secondResponse");
+  ok(secondResponse, "Second response should exist");
+  if (secondResponse) {
+    is(secondResponse.innerHTML, "Second response",
+       "Second response should be correct");
+  }
 
   SimpleTest.finish();
 }
 
 // Set listener for first load to expect partial content.
 document.getElementById('contentFrame')
   .addEventListener("load", expectInitialContent, false);