summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_bug510359.js
blob: 321da399126ef48bf66d64faa74b53a73a0ab125 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"use strict";

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

var httpserver = new HttpServer();
var index = 0;
var tests = [
  { url: "/bug510359", server: "0", expected: "0" },
  { url: "/bug510359", server: "1", expected: "1" },
];

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

function triggerNextTest() {
  var channel = setupChannel(tests[index].url, tests[index].server);
  channel.asyncOpen(new ChannelListener(checkValueAndTrigger, null));
}

function checkValueAndTrigger(request, data, ctx) {
  Assert.equal(tests[index].expected, data);

  if (index < tests.length - 1) {
    index++;
    triggerNextTest();
  } else {
    httpserver.stop(do_test_finished);
  }
}

function run_test() {
  httpserver.registerPathHandler("/bug510359", handler);
  httpserver.start(-1);

  // clear cache
  evict_cache_entries();

  triggerNextTest();

  do_test_pending();
}

function handler(metadata, response) {
  try {
    metadata.getHeader("If-Modified-Since");
    response.setStatusLine(metadata.httpVersion, 500, "Failed");
    var msg = "Client should not set If-Modified-Since header";
    response.bodyOutputStream.write(msg, msg.length);
  } catch (ex) {
    response.setStatusLine(metadata.httpVersion, 200, "Ok");
    response.setHeader("Content-Type", "text/plain", false);
    response.setHeader("Last-Modified", getDateString(-1), false);
    response.setHeader("Vary", "Cookie", false);
    var body = metadata.getHeader("x-request");
    response.bodyOutputStream.write(body, body.length);
  }
}

function getDateString(yearDelta) {
  var months = [
    "Jan",
    "Feb",
    "Mar",
    "Apr",
    "May",
    "Jun",
    "Jul",
    "Aug",
    "Sep",
    "Oct",
    "Nov",
    "Dec",
  ];
  var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];

  var d = new Date();
  return (
    days[d.getUTCDay()] +
    ", " +
    d.getUTCDate() +
    " " +
    months[d.getUTCMonth()] +
    " " +
    (d.getUTCFullYear() + yearDelta) +
    " " +
    d.getUTCHours() +
    ":" +
    d.getUTCMinutes() +
    ":" +
    d.getUTCSeconds() +
    " UTC"
  );
}