summaryrefslogtreecommitdiffstats
path: root/netwerk/test/httpserver/test/test_processasync.js
blob: 321c9b086f10785f80673d05fde0c02b92304873 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * Tests for correct behavior of asynchronous responses.
 */

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

var srv;

function run_test() {
  srv = createServer();
  for (var path in handlers) {
    srv.registerPathHandler(path, handlers[path]);
  }
  srv.start(-1);

  runHttpTests(tests, testComplete(srv));
}

/** *************
 * BEGIN TESTS *
 ***************/

ChromeUtils.defineLazyGetter(this, "tests", function () {
  return [
    new Test(PREPATH + "/handleSync", null, start_handleSync, null),
    new Test(
      PREPATH + "/handleAsync1",
      null,
      start_handleAsync1,
      stop_handleAsync1
    ),
    new Test(
      PREPATH + "/handleAsync2",
      init_handleAsync2,
      start_handleAsync2,
      stop_handleAsync2
    ),
    new Test(
      PREPATH + "/handleAsyncOrdering",
      null,
      null,
      stop_handleAsyncOrdering
    ),
  ];
});

var handlers = {};

function handleSync(request, response) {
  response.setStatusLine(request.httpVersion, 500, "handleSync fail");

  try {
    response.finish();
    do_throw("finish called on sync response");
  } catch (e) {
    isException(e, Cr.NS_ERROR_UNEXPECTED);
  }

  response.setStatusLine(request.httpVersion, 200, "handleSync pass");
}
handlers["/handleSync"] = handleSync;

function start_handleSync(ch) {
  Assert.equal(ch.responseStatus, 200);
  Assert.equal(ch.responseStatusText, "handleSync pass");
}

function handleAsync1(request, response) {
  response.setStatusLine(request.httpVersion, 500, "Old status line!");
  response.setHeader("X-Foo", "old value", false);

  response.processAsync();

  response.setStatusLine(request.httpVersion, 200, "New status line!");
  response.setHeader("X-Foo", "new value", false);

  response.finish();

  try {
    response.setStatusLine(request.httpVersion, 500, "Too late!");
    do_throw("late setStatusLine didn't throw");
  } catch (e) {
    isException(e, Cr.NS_ERROR_NOT_AVAILABLE);
  }

  try {
    response.setHeader("X-Foo", "late value", false);
    do_throw("late setHeader didn't throw");
  } catch (e) {
    isException(e, Cr.NS_ERROR_NOT_AVAILABLE);
  }

  try {
    response.bodyOutputStream;
    do_throw("late bodyOutputStream get didn't throw");
  } catch (e) {
    isException(e, Cr.NS_ERROR_NOT_AVAILABLE);
  }

  try {
    response.write("fugly");
    do_throw("late write() didn't throw");
  } catch (e) {
    isException(e, Cr.NS_ERROR_NOT_AVAILABLE);
  }
}
handlers["/handleAsync1"] = handleAsync1;

function start_handleAsync1(ch) {
  Assert.equal(ch.responseStatus, 200);
  Assert.equal(ch.responseStatusText, "New status line!");
  Assert.equal(ch.getResponseHeader("X-Foo"), "new value");
}

function stop_handleAsync1(ch, status, data) {
  Assert.equal(data.length, 0);
}

const startToHeaderDelay = 500;
const startToFinishedDelay = 750;

function handleAsync2(request, response) {
  response.processAsync();

  response.setStatusLine(request.httpVersion, 200, "Status line");
  response.setHeader("X-Custom-Header", "value", false);

  callLater(startToHeaderDelay, function () {
    var preBody = "BO";
    response.bodyOutputStream.write(preBody, preBody.length);

    try {
      response.setStatusLine(request.httpVersion, 500, "after body write");
      do_throw("setStatusLine succeeded");
    } catch (e) {
      isException(e, Cr.NS_ERROR_NOT_AVAILABLE);
    }

    try {
      response.setHeader("X-Custom-Header", "new 1", false);
    } catch (e) {
      isException(e, Cr.NS_ERROR_NOT_AVAILABLE);
    }

    callLater(startToFinishedDelay - startToHeaderDelay, function () {
      var postBody = "DY";
      response.bodyOutputStream.write(postBody, postBody.length);

      response.finish();
      response.finish(); // idempotency

      try {
        response.setStatusLine(request.httpVersion, 500, "after finish");
      } catch (e) {
        isException(e, Cr.NS_ERROR_NOT_AVAILABLE);
      }

      try {
        response.setHeader("X-Custom-Header", "new 2", false);
      } catch (e) {
        isException(e, Cr.NS_ERROR_NOT_AVAILABLE);
      }

      try {
        response.write("EVIL");
      } catch (e) {
        isException(e, Cr.NS_ERROR_NOT_AVAILABLE);
      }
    });
  });
}
handlers["/handleAsync2"] = handleAsync2;

var startTime_handleAsync2;

function init_handleAsync2(ch) {
  var now = (startTime_handleAsync2 = Date.now());
  dumpn("*** init_HandleAsync2: start time " + now);
}

function start_handleAsync2(ch) {
  var now = Date.now();
  dumpn(
    "*** start_handleAsync2: onStartRequest time " +
      now +
      ", " +
      (now - startTime_handleAsync2) +
      "ms after start time"
  );
  Assert.ok(now >= startTime_handleAsync2 + startToHeaderDelay);

  Assert.equal(ch.responseStatus, 200);
  Assert.equal(ch.responseStatusText, "Status line");
  Assert.equal(ch.getResponseHeader("X-Custom-Header"), "value");
}

function stop_handleAsync2(ch, status, data) {
  var now = Date.now();
  dumpn(
    "*** stop_handleAsync2: onStopRequest time " +
      now +
      ", " +
      (now - startTime_handleAsync2) +
      "ms after header time"
  );
  Assert.ok(now >= startTime_handleAsync2 + startToFinishedDelay);

  Assert.equal(String.fromCharCode.apply(null, data), "BODY");
}

/*
 * Tests that accessing output stream *before* calling processAsync() works
 * correctly, sending written data immediately as it is written, not buffering
 * until finish() is called -- which for this much data would mean we would all
 * but certainly deadlock, since we're trying to read/write all this data in one
 * process on a single thread.
 */
function handleAsyncOrdering(request, response) {
  var out = new BinaryOutputStream(response.bodyOutputStream);

  var data = [];
  for (var i = 0; i < 65536; i++) {
    data[i] = 0;
  }
  var count = 20;

  var writeData = {
    run() {
      if (count-- === 0) {
        response.finish();
        return;
      }

      try {
        out.writeByteArray(data);
        step();
      } catch (e) {
        try {
          do_throw("error writing data: " + e);
        } finally {
          response.finish();
        }
      }
    },
  };
  function step() {
    // Use gThreadManager here because it's expedient, *not* because it's
    // intended for public use!  If you do this in client code, expect me to
    // knowingly break your code by changing the variable name.  :-P
    Services.tm.dispatchToMainThread(writeData);
  }
  step();
  response.processAsync();
}
handlers["/handleAsyncOrdering"] = handleAsyncOrdering;

function stop_handleAsyncOrdering(ch, status, data) {
  Assert.equal(data.length, 20 * 65536);
  data.forEach(function (v, index) {
    if (v !== 0) {
      do_throw("value " + v + " at index " + index + " should be zero");
    }
  });
}