Bug 1146304 - Touch slider bar or tap forward button, the video got stuck 1s then...
[gecko.git] / netwerk / test / unit / test_gzipped_206.js
blob0b1b4d56af9698ce2f370e2ae40e848644798fd5
1 Cu.import("resource://testing-common/httpd.js");
2 Cu.import("resource://gre/modules/Services.jsm");
4 var httpserver = null;
6 // testString = "This is a slightly longer test\n";
7 const responseBody = [0x1f, 0x8b, 0x08, 0x08, 0xef, 0x70, 0xe6, 0x4c, 0x00, 0x03, 0x74, 0x65, 0x78, 0x74, 0x66, 0x69,
8                      0x6c, 0x65, 0x2e, 0x74, 0x78, 0x74, 0x00, 0x0b, 0xc9, 0xc8, 0x2c, 0x56, 0x00, 0xa2, 0x44, 0x85,
9                      0xe2, 0x9c, 0xcc, 0xf4, 0x8c, 0x92, 0x9c, 0x4a, 0x85, 0x9c, 0xfc, 0xbc, 0xf4, 0xd4, 0x22, 0x85,
10                      0x92, 0xd4, 0xe2, 0x12, 0x2e, 0x2e, 0x00, 0x00, 0xe5, 0xe6, 0xf0, 0x20, 0x00, 0x00, 0x00];
12 function make_channel(url, callback, ctx) {
13   var ios = Cc["@mozilla.org/network/io-service;1"].
14             getService(Ci.nsIIOService);
15   return ios.newChannel(url, "", null);
18 var doRangeResponse = false;
20 function cachedHandler(metadata, response) {
21   response.setHeader("Content-Type", "application/x-gzip", false);
22   response.setHeader("Content-Encoding", "gzip", false);
23   response.setHeader("ETag", "Just testing");
24   response.setHeader("Cache-Control", "max-age=3600000"); // avoid validation
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     response.setHeader("Content-Length", "" + (to + 1 - from));
40     // always respond to successful range requests with 206
41     response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
42     response.setHeader("Content-Range", from + "-" + to + "/" + responseBody.length, false);
43   } else {
44     // This response will get cut off prematurely
45     response.setHeader("Content-Length", "" + responseBody.length);
46     response.setHeader("Accept-Ranges", "bytes");
47     body = body.slice(0, 17); // slice off a piece to send first
48     doRangeResponse = true;
49   }
51   var bos = Cc["@mozilla.org/binaryoutputstream;1"]
52       .createInstance(Ci.nsIBinaryOutputStream);
53   bos.setOutputStream(response.bodyOutputStream);
55   response.processAsync();
56   bos.writeByteArray(body, body.length);
57   response.finish();
60 function continue_test(request, data) {
61   do_check_eq(17, data.length);
62   var chan = make_channel("http://localhost:" +
63                           httpserver.identity.primaryPort + "/cached/test.gz");
64   chan.asyncOpen(new ChannelListener(finish_test, null, CL_EXPECT_GZIP), null);
67 var enforcePref;
69 function finish_test(request, data, ctx) {
70   do_check_eq(request.status, 0);
71   do_check_eq(data.length, responseBody.length);
72   for (var i = 0; i < data.length; ++i) {
73     do_check_eq(data.charCodeAt(i), responseBody[i]);
74   }
75   Services.prefs.setBoolPref("network.http.enforce-framing.http1", enforcePref);
76   httpserver.stop(do_test_finished);
79 function run_test() {
80   enforcePref = Services.prefs.getBoolPref("network.http.enforce-framing.http1");
81   Services.prefs.setBoolPref("network.http.enforce-framing.http1", false);
83   httpserver = new HttpServer();
84   httpserver.registerPathHandler("/cached/test.gz", cachedHandler);
85   httpserver.start(-1);
87   // wipe out cached content
88   evict_cache_entries();
90   var chan = make_channel("http://localhost:" +
91                           httpserver.identity.primaryPort + "/cached/test.gz");
92   chan.asyncOpen(new ChannelListener(continue_test, null, CL_EXPECT_GZIP | CL_IGNORE_CL), null);
93   do_test_pending();