netwerk/test/unit/test_gzipped_206.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_gzipped_206.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,85 @@
     1.4 +Cu.import("resource://testing-common/httpd.js");
     1.5 +
     1.6 +var httpserver = null;
     1.7 +
     1.8 +// testString = "This is a slightly longer test\n";
     1.9 +const responseBody = [0x1f, 0x8b, 0x08, 0x08, 0xef, 0x70, 0xe6, 0x4c, 0x00, 0x03, 0x74, 0x65, 0x78, 0x74, 0x66, 0x69,
    1.10 +                     0x6c, 0x65, 0x2e, 0x74, 0x78, 0x74, 0x00, 0x0b, 0xc9, 0xc8, 0x2c, 0x56, 0x00, 0xa2, 0x44, 0x85,
    1.11 +                     0xe2, 0x9c, 0xcc, 0xf4, 0x8c, 0x92, 0x9c, 0x4a, 0x85, 0x9c, 0xfc, 0xbc, 0xf4, 0xd4, 0x22, 0x85,
    1.12 +                     0x92, 0xd4, 0xe2, 0x12, 0x2e, 0x2e, 0x00, 0x00, 0xe5, 0xe6, 0xf0, 0x20, 0x00, 0x00, 0x00];
    1.13 +
    1.14 +function make_channel(url, callback, ctx) {
    1.15 +  var ios = Cc["@mozilla.org/network/io-service;1"].
    1.16 +            getService(Ci.nsIIOService);
    1.17 +  return ios.newChannel(url, "", null);
    1.18 +}
    1.19 +
    1.20 +var doRangeResponse = false;
    1.21 +
    1.22 +function cachedHandler(metadata, response) {
    1.23 +  response.setHeader("Content-Type", "application/x-gzip", false);
    1.24 +  response.setHeader("Content-Encoding", "gzip", false);
    1.25 +  response.setHeader("ETag", "Just testing");
    1.26 +  response.setHeader("Cache-Control", "max-age=3600000"); // avoid validation
    1.27 +  response.setHeader("Content-Length", "" + responseBody.length);
    1.28 +
    1.29 +  var body = responseBody;
    1.30 +
    1.31 +  if (doRangeResponse) {
    1.32 +    do_check_true(metadata.hasHeader("Range"));
    1.33 +    var matches = metadata.getHeader("Range").match(/^\s*bytes=(\d+)?-(\d+)?\s*$/);
    1.34 +    var from = (matches[1] === undefined) ? 0 : matches[1];
    1.35 +    var to = (matches[2] === undefined) ? responseBody.length - 1 : matches[2];
    1.36 +    if (from >= responseBody.length) {
    1.37 +      response.setStatusLine(metadata.httpVersion, 416, "Start pos too high");
    1.38 +      response.setHeader("Content-Range", "*/" + responseBody.length, false);
    1.39 +      return;
    1.40 +    }
    1.41 +    body = body.slice(from, to + 1);
    1.42 +    // always respond to successful range requests with 206
    1.43 +    response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
    1.44 +    response.setHeader("Content-Range", from + "-" + to + "/" + responseBody.length, false);
    1.45 +  } else {
    1.46 +    response.setHeader("Accept-Ranges", "bytes");
    1.47 +    body = body.slice(0, 17); // slice off a piece to send first
    1.48 +    doRangeResponse = true;
    1.49 +  }
    1.50 +
    1.51 +  var bos = Cc["@mozilla.org/binaryoutputstream;1"]
    1.52 +      .createInstance(Ci.nsIBinaryOutputStream);
    1.53 +  bos.setOutputStream(response.bodyOutputStream);
    1.54 +
    1.55 +  response.processAsync();
    1.56 +  bos.writeByteArray(body, body.length);
    1.57 +  response.finish();
    1.58 +}
    1.59 +
    1.60 +function continue_test(request, data) {
    1.61 +  do_check_true(17 == data.length);
    1.62 +  var chan = make_channel("http://localhost:" +
    1.63 +                          httpserver.identity.primaryPort + "/cached/test.gz");
    1.64 +  chan.asyncOpen(new ChannelListener(finish_test, null, CL_EXPECT_GZIP), null);
    1.65 +}
    1.66 +
    1.67 +function finish_test(request, data, ctx) {
    1.68 +  do_check_eq(request.status, 0);
    1.69 +  do_check_eq(data.length, responseBody.length);
    1.70 +  for (var i = 0; i < data.length; ++i) {
    1.71 +    do_check_eq(data.charCodeAt(i), responseBody[i]);
    1.72 +  }
    1.73 +  httpserver.stop(do_test_finished);
    1.74 +}
    1.75 +
    1.76 +function run_test() {
    1.77 +  httpserver = new HttpServer();
    1.78 +  httpserver.registerPathHandler("/cached/test.gz", cachedHandler);
    1.79 +  httpserver.start(-1);
    1.80 +
    1.81 +  // wipe out cached content
    1.82 +  evict_cache_entries();
    1.83 +
    1.84 +  var chan = make_channel("http://localhost:" +
    1.85 +                          httpserver.identity.primaryPort + "/cached/test.gz");
    1.86 +  chan.asyncOpen(new ChannelListener(continue_test, null, CL_EXPECT_GZIP), null);
    1.87 +  do_test_pending();
    1.88 +}

mercurial