netwerk/test/unit/test_gzipped_206.js

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

     1 Cu.import("resource://testing-common/httpd.js");
     3 var httpserver = null;
     5 // testString = "This is a slightly longer test\n";
     6 const responseBody = [0x1f, 0x8b, 0x08, 0x08, 0xef, 0x70, 0xe6, 0x4c, 0x00, 0x03, 0x74, 0x65, 0x78, 0x74, 0x66, 0x69,
     7                      0x6c, 0x65, 0x2e, 0x74, 0x78, 0x74, 0x00, 0x0b, 0xc9, 0xc8, 0x2c, 0x56, 0x00, 0xa2, 0x44, 0x85,
     8                      0xe2, 0x9c, 0xcc, 0xf4, 0x8c, 0x92, 0x9c, 0x4a, 0x85, 0x9c, 0xfc, 0xbc, 0xf4, 0xd4, 0x22, 0x85,
     9                      0x92, 0xd4, 0xe2, 0x12, 0x2e, 0x2e, 0x00, 0x00, 0xe5, 0xe6, 0xf0, 0x20, 0x00, 0x00, 0x00];
    11 function make_channel(url, callback, ctx) {
    12   var ios = Cc["@mozilla.org/network/io-service;1"].
    13             getService(Ci.nsIIOService);
    14   return ios.newChannel(url, "", null);
    15 }
    17 var doRangeResponse = false;
    19 function cachedHandler(metadata, response) {
    20   response.setHeader("Content-Type", "application/x-gzip", false);
    21   response.setHeader("Content-Encoding", "gzip", false);
    22   response.setHeader("ETag", "Just testing");
    23   response.setHeader("Cache-Control", "max-age=3600000"); // avoid validation
    24   response.setHeader("Content-Length", "" + responseBody.length);
    26   var body = responseBody;
    28   if (doRangeResponse) {
    29     do_check_true(metadata.hasHeader("Range"));
    30     var matches = metadata.getHeader("Range").match(/^\s*bytes=(\d+)?-(\d+)?\s*$/);
    31     var from = (matches[1] === undefined) ? 0 : matches[1];
    32     var to = (matches[2] === undefined) ? responseBody.length - 1 : matches[2];
    33     if (from >= responseBody.length) {
    34       response.setStatusLine(metadata.httpVersion, 416, "Start pos too high");
    35       response.setHeader("Content-Range", "*/" + responseBody.length, false);
    36       return;
    37     }
    38     body = body.slice(from, to + 1);
    39     // always respond to successful range requests with 206
    40     response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
    41     response.setHeader("Content-Range", from + "-" + to + "/" + responseBody.length, false);
    42   } else {
    43     response.setHeader("Accept-Ranges", "bytes");
    44     body = body.slice(0, 17); // slice off a piece to send first
    45     doRangeResponse = true;
    46   }
    48   var bos = Cc["@mozilla.org/binaryoutputstream;1"]
    49       .createInstance(Ci.nsIBinaryOutputStream);
    50   bos.setOutputStream(response.bodyOutputStream);
    52   response.processAsync();
    53   bos.writeByteArray(body, body.length);
    54   response.finish();
    55 }
    57 function continue_test(request, data) {
    58   do_check_true(17 == data.length);
    59   var chan = make_channel("http://localhost:" +
    60                           httpserver.identity.primaryPort + "/cached/test.gz");
    61   chan.asyncOpen(new ChannelListener(finish_test, null, CL_EXPECT_GZIP), null);
    62 }
    64 function finish_test(request, data, ctx) {
    65   do_check_eq(request.status, 0);
    66   do_check_eq(data.length, responseBody.length);
    67   for (var i = 0; i < data.length; ++i) {
    68     do_check_eq(data.charCodeAt(i), responseBody[i]);
    69   }
    70   httpserver.stop(do_test_finished);
    71 }
    73 function run_test() {
    74   httpserver = new HttpServer();
    75   httpserver.registerPathHandler("/cached/test.gz", cachedHandler);
    76   httpserver.start(-1);
    78   // wipe out cached content
    79   evict_cache_entries();
    81   var chan = make_channel("http://localhost:" +
    82                           httpserver.identity.primaryPort + "/cached/test.gz");
    83   chan.asyncOpen(new ChannelListener(continue_test, null, CL_EXPECT_GZIP), null);
    84   do_test_pending();
    85 }

mercurial