summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_headers.js
blob: e1439c6c4335d911485f69eb5b8ad7bb6ae6e6cf (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//
//  cleaner HTTP header test infrastructure
//
//  tests bugs:  589292, [add more here: see hg log for definitive list]
//
//  TO ADD NEW TESTS:
//  1) Increment up 'lastTest' to new number (say, "99")
//  2) Add new test 'handler99' and 'completeTest99' functions.
//  3) If your test should fail the necko channel, set
//     test_flags[99] = CL_EXPECT_FAILURE.
//
// TO DEBUG JUST ONE TEST: temporarily change firstTest and lastTest to equal
//                         the test # you're interested in.
//
//  For tests that need duplicate copies of headers to be sent, see
//  test_duplicate_headers.js

"use strict";

var firstTest = 1; // set to test of interest when debugging
var lastTest = 4; // set to test of interest when debugging
////////////////////////////////////////////////////////////////////////////////

// Note: sets Cc and Ci variables

const { HttpServer } = ChromeUtils.importESModule(
  "resource://testing-common/httpd.sys.mjs"
);

ChromeUtils.defineLazyGetter(this, "URL", function () {
  return "http://localhost:" + httpserver.identity.primaryPort;
});

var httpserver = new HttpServer();
var nextTest = firstTest;
var test_flags = [];
var testPathBase = "/test_headers";

function run_test() {
  httpserver.start(-1);

  do_test_pending();
  run_test_number(nextTest);
}

function runNextTest() {
  if (nextTest == lastTest) {
    endTests();
    return;
  }
  nextTest++;
  // Make sure test functions exist
  if (globalThis["handler" + nextTest] == undefined) {
    do_throw("handler" + nextTest + " undefined!");
  }
  if (globalThis["completeTest" + nextTest] == undefined) {
    do_throw("completeTest" + nextTest + " undefined!");
  }

  run_test_number(nextTest);
}

function run_test_number(num) {
  let testPath = testPathBase + num;
  httpserver.registerPathHandler(testPath, globalThis["handler" + num]);

  var channel = setupChannel(testPath);
  let flags = test_flags[num]; // OK if flags undefined for test
  channel.asyncOpen(
    new ChannelListener(globalThis["completeTest" + num], channel, flags)
  );
}

function setupChannel(url) {
  var chan = NetUtil.newChannel({
    uri: URL + url,
    loadUsingSystemPrincipal: true,
  });
  var httpChan = chan.QueryInterface(Ci.nsIHttpChannel);
  return httpChan;
}

function endTests() {
  httpserver.stop(do_test_finished);
}

////////////////////////////////////////////////////////////////////////////////
// Test 1: test Content-Disposition channel attributes
// eslint-disable-next-line no-unused-vars
function handler1(metadata, response) {
  response.setStatusLine(metadata.httpVersion, 200, "OK");
  response.setHeader("Content-Disposition", "attachment; filename=foo");
  response.setHeader("Content-Type", "text/plain", false);
}

// eslint-disable-next-line no-unused-vars
function completeTest1(request, data, ctx) {
  try {
    var chan = request.QueryInterface(Ci.nsIChannel);
    Assert.equal(chan.contentDisposition, chan.DISPOSITION_ATTACHMENT);
    Assert.equal(chan.contentDispositionFilename, "foo");
    Assert.equal(chan.contentDispositionHeader, "attachment; filename=foo");
  } catch (ex) {
    do_throw("error parsing Content-Disposition: " + ex);
  }
  runNextTest();
}

////////////////////////////////////////////////////////////////////////////////
// Test 2: no filename
// eslint-disable-next-line no-unused-vars
function handler2(metadata, response) {
  response.setStatusLine(metadata.httpVersion, 200, "OK");
  response.setHeader("Content-Type", "text/plain", false);
  response.setHeader("Content-Disposition", "attachment");
  var body = "foo";
  response.bodyOutputStream.write(body, body.length);
}

// eslint-disable-next-line no-unused-vars
function completeTest2(request, data, ctx) {
  try {
    var chan = request.QueryInterface(Ci.nsIChannel);
    Assert.equal(chan.contentDisposition, chan.DISPOSITION_ATTACHMENT);
    Assert.equal(chan.contentDispositionHeader, "attachment");
    chan.contentDispositionFilename; // should barf
    do_throw("Should have failed getting Content-Disposition filename");
  } catch (ex) {
    info("correctly ate exception");
  }
  runNextTest();
}

////////////////////////////////////////////////////////////////////////////////
// Test 3: filename missing
// eslint-disable-next-line no-unused-vars
function handler3(metadata, response) {
  response.setStatusLine(metadata.httpVersion, 200, "OK");
  response.setHeader("Content-Type", "text/plain", false);
  response.setHeader("Content-Disposition", "attachment; filename=");
  var body = "foo";
  response.bodyOutputStream.write(body, body.length);
}

// eslint-disable-next-line no-unused-vars
function completeTest3(request, data, ctx) {
  try {
    var chan = request.QueryInterface(Ci.nsIChannel);
    Assert.equal(chan.contentDisposition, chan.DISPOSITION_ATTACHMENT);
    Assert.equal(chan.contentDispositionHeader, "attachment; filename=");
    chan.contentDispositionFilename; // should barf

    do_throw("Should have failed getting Content-Disposition filename");
  } catch (ex) {
    info("correctly ate exception");
  }
  runNextTest();
}

////////////////////////////////////////////////////////////////////////////////
// Test 4: inline
// eslint-disable-next-line no-unused-vars
function handler4(metadata, response) {
  response.setStatusLine(metadata.httpVersion, 200, "OK");
  response.setHeader("Content-Type", "text/plain", false);
  response.setHeader("Content-Disposition", "inline");
  var body = "foo";
  response.bodyOutputStream.write(body, body.length);
}

// eslint-disable-next-line no-unused-vars
function completeTest4(request, data, ctx) {
  try {
    var chan = request.QueryInterface(Ci.nsIChannel);
    Assert.equal(chan.contentDisposition, chan.DISPOSITION_INLINE);
    Assert.equal(chan.contentDispositionHeader, "inline");

    chan.contentDispositionFilename; // should barf
    do_throw("Should have failed getting Content-Disposition filename");
  } catch (ex) {
    info("correctly ate exception");
  }
  runNextTest();
}