summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_bug561276.js
blob: 03ff7b3e355ba8aba27a5134adb67d1c6b7d9a78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// Verify that we hit the net if we discover a cycle of redirects
// coming from cache.
//

"use strict";

const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");

var httpserver = new HttpServer();
var iteration = 0;

function setupChannel(suffix) {
  var chan = NetUtil.newChannel({
    uri: "http://localhost:" + httpserver.identity.primaryPort + suffix,
    loadUsingSystemPrincipal: true,
  });
  var httpChan = chan.QueryInterface(Ci.nsIHttpChannel);
  httpChan.requestMethod = "GET";
  return httpChan;
}

function checkValueAndTrigger(request, data, ctx) {
  Assert.equal("Ok", data);
  httpserver.stop(do_test_finished);
}

function run_test() {
  httpserver.registerPathHandler("/redirect1", redirectHandler1);
  httpserver.registerPathHandler("/redirect2", redirectHandler2);
  httpserver.start(-1);

  // clear cache
  evict_cache_entries();

  // load first time
  var channel = setupChannel("/redirect1");
  channel.asyncOpen(new ChannelListener(checkValueAndTrigger, null));

  do_test_pending();
}

function redirectHandler1(metadata, response) {
  // first time we return a cacheable 302 pointing to next redirect
  if (iteration < 1) {
    response.setStatusLine(metadata.httpVersion, 302, "Found");
    response.setHeader("Cache-Control", "max-age=600", false);
    response.setHeader("Location", "/redirect2", false);

    // next time called we return 200
  } else {
    response.setStatusLine(metadata.httpVersion, 200, "Ok");
    response.setHeader("Cache-Control", "max-age=600", false);
    response.setHeader("Content-Type", "text/plain");
    response.bodyOutputStream.write("Ok", "Ok".length);
  }
  iteration += 1;
}

function redirectHandler2(metadata, response) {
  response.setStatusLine(metadata.httpVersion, 302, "Found");
  response.setHeader("Cache-Control", "max-age=600", false);
  response.setHeader("Location", "/redirect1", false);
}