Bug 1504323 - P1 Enable ESLint for netwerk/test/httpserver/ (automatic changes) r=Standard8,dragana

Differential Revision: https://phabricator.services.mozilla.com/D10985

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Junior Hsu 2018-11-11 21:25:52 +00:00
parent edcd1c640e
commit 964925b6fe
30 changed files with 1140 additions and 2369 deletions

File diff suppressed because it is too large Load diff

View file

@ -20,8 +20,7 @@ ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
* is possible to run these tests (with at most slight modifications) against
* the server when used as an XPCOM component (not as an inline script).
*/
function createServer()
{
function createServer() {
return new nsHttpServer();
}
@ -31,8 +30,7 @@ function createServer()
* @param url
* the URL of the channel to create
*/
function makeChannel(url)
{
function makeChannel(url) {
return NetUtil.newChannel({uri: url, loadUsingSystemPrincipal: true})
.QueryInterface(Ci.nsIHttpChannel);
}
@ -43,8 +41,7 @@ function makeChannel(url)
* @param stream
* the nsIInputStream to wrap
*/
function makeBIS(stream)
{
function makeBIS(stream) {
return new BinaryInputStream(stream);
}
@ -57,8 +54,7 @@ function makeBIS(stream)
* @returns string
* the contents of the file
*/
function fileContents(file)
{
function fileContents(file) {
const PR_RDONLY = 0x01;
var fis = new FileInputStream(file, PR_RDONLY, 0o444,
Ci.nsIFileInputStream.CLOSE_ON_EOF);
@ -78,11 +74,9 @@ function fileContents(file)
* an Iterator which returns each line from data in turn; note that this
* includes a final empty line if data ended with a CRLF
*/
function* LineIterator(data)
{
function* LineIterator(data) {
var start = 0, index = 0;
do
{
do {
index = data.indexOf("\r\n");
if (index >= 0)
yield data.substring(0, index);
@ -105,11 +99,9 @@ function* LineIterator(data)
* @throws string
* an error message if iter doesn't agree with expectedLines
*/
function expectLines(iter, expectedLines)
{
function expectLines(iter, expectedLines) {
var index = 0;
for (var line of iter)
{
for (var line of iter) {
if (expectedLines.length == index)
throw "Error: got more than " + expectedLines.length + " expected lines!";
@ -120,8 +112,7 @@ function expectLines(iter, expectedLines)
" expect: '" + expected + "'";
}
if (expectedLines.length !== index)
{
if (expectedLines.length !== index) {
throw "Expected more lines! Got " + index +
", expected " + expectedLines.length;
}
@ -135,8 +126,7 @@ function expectLines(iter, expectedLines)
* @param response : nsIHttpResponse
* the response to which the metadata is written
*/
function writeDetails(request, response)
{
function writeDetails(request, response) {
response.write("Method: " + request.method + "\r\n");
response.write("Path: " + request.path + "\r\n");
response.write("Query: " + request.queryString + "\r\n");
@ -154,8 +144,7 @@ function writeDetails(request, response)
* an iterator over the CRLF-delimited lines in an HTTP response, currently
* just after the Request-Line
*/
function skipHeaders(iter)
{
function skipHeaders(iter) {
var line = iter.next().value;
while (line !== "")
line = iter.next().value;
@ -170,8 +159,7 @@ function skipHeaders(iter)
* @param code : nsresult
* the expected exception
*/
function isException(e, code)
{
function isException(e, code) {
if (e !== code && e.result !== code)
do_throw("unexpected error: " + e);
}
@ -186,13 +174,12 @@ function isException(e, code)
* @param callback : function() : void
* the function to call
*/
function callLater(msecs, callback)
{
function callLater(msecs, callback) {
do_timeout(msecs, callback);
}
/*******************************************************
/** *****************************************************
* SIMPLE SUPPORT FOR LOADING/TESTING A SERIES OF URLS *
*******************************************************/
@ -200,10 +187,8 @@ function callLater(msecs, callback)
* Create a completion callback which will stop the given server and end the
* test, assuming nothing else remains to be done at that point.
*/
function testComplete(srv)
{
return function complete()
{
function testComplete(srv) {
return function complete() {
do_test_pending();
srv.stop(function quit() { do_test_finished(); });
};
@ -230,8 +215,7 @@ function testComplete(srv)
* nsIHttpChannel and nsIHttpChannelInternal for convenience; may be null if
* nothing needs to be done
*/
function Test(path, initChannel, onStartRequest, onStopRequest)
{
function Test(path, initChannel, onStartRequest, onStopRequest) {
function nil() { }
this.path = path;
@ -248,19 +232,13 @@ function Test(path, initChannel, onStartRequest, onStopRequest)
* @param done
* function to call when all tests have run (e.g. to shut down the server)
*/
function runHttpTests(testArray, done)
{
function runHttpTests(testArray, done) {
/** Kicks off running the next test in the array. */
function performNextTest()
{
if (++testIndex == testArray.length)
{
try
{
function performNextTest() {
if (++testIndex == testArray.length) {
try {
done();
}
catch (e)
{
} catch (e) {
do_report_unexpected_exception(e, "running test-completion callback");
}
return;
@ -270,18 +248,12 @@ function runHttpTests(testArray, done)
var test = testArray[testIndex];
var ch = makeChannel(test.path);
try
{
try {
test.initChannel(ch);
}
catch (e)
{
try
{
} catch (e) {
try {
do_report_unexpected_exception(e, "testArray[" + testIndex + "].initChannel(ch)");
}
catch (e)
{
} catch (e) {
/* swallow and let tests continue */
}
}
@ -301,42 +273,32 @@ function runHttpTests(testArray, done)
/** Array of bytes of data in body of response. */
_data: [],
onStartRequest: function(request, cx)
{
onStartRequest(request, cx) {
Assert.ok(request === this._channel);
var ch = request.QueryInterface(Ci.nsIHttpChannel)
.QueryInterface(Ci.nsIHttpChannelInternal);
this._data.length = 0;
try
{
try
{
try {
try {
testArray[testIndex].onStartRequest(ch, cx);
}
catch (e)
{
} catch (e) {
do_report_unexpected_exception(e, "testArray[" + testIndex + "].onStartRequest");
}
}
catch (e)
{
} catch (e) {
do_note_exception(e, "!!! swallowing onStartRequest exception so onStopRequest is " +
"called...");
}
},
onDataAvailable: function(request, cx, inputStream, offset, count)
{
onDataAvailable(request, cx, inputStream, offset, count) {
var quantum = 262144; // just above half the argument-count limit
var bis = makeBIS(inputStream);
for (var start = 0; start < count; start += quantum)
{
for (var start = 0; start < count; start += quantum) {
var newData = bis.readByteArray(Math.min(quantum, count - start));
Array.prototype.push.apply(this._data, newData);
}
},
onStopRequest: function(request, cx, status)
{
onStopRequest(request, cx, status) {
this._channel = null;
var ch = request.QueryInterface(Ci.nsIHttpChannel)
@ -346,37 +308,24 @@ function runHttpTests(testArray, done)
// because the latter runs the next test's initChannel callback, and
// we want one test to be sequentially processed before the next
// one.
try
{
try {
testArray[testIndex].onStopRequest(ch, cx, status, this._data);
}
finally
{
try
{
} finally {
try {
performNextTest();
}
finally
{
} finally {
do_test_finished();
}
}
},
QueryInterface: function(aIID)
{
if (aIID.equals(Ci.nsIStreamListener) ||
aIID.equals(Ci.nsIRequestObserver) ||
aIID.equals(Ci.nsISupports))
return this;
throw Cr.NS_ERROR_NO_INTERFACE;
}
QueryInterface: ChromeUtils.generateQI(["nsIStreamListener", "nsIRequestObserver"]),
};
performNextTest();
}
/****************************************
/** **************************************
* RAW REQUEST FORMAT TESTING FUNCTIONS *
****************************************/
@ -398,8 +347,7 @@ function runHttpTests(testArray, done)
* conducts whatever tests it wants on that data; useful for tweaking the test
* environment between tests
*/
function RawTest(host, port, data, responseCheck)
{
function RawTest(host, port, data, responseCheck) {
if (0 > port || 65535 < port || port % 1 !== 0)
throw "bad port";
if (!(data instanceof Array))
@ -423,8 +371,7 @@ function RawTest(host, port, data, responseCheck)
* @param done
* function to call when all tests have run (e.g. to shut down the server)
*/
function runRawTests(testArray, done)
{
function runRawTests(testArray, done) {
do_test_pending();
var sts = Cc["@mozilla.org/network/socket-transport-service;1"]
@ -433,19 +380,14 @@ function runRawTests(testArray, done)
var currentThread = Cc["@mozilla.org/thread-manager;1"]
.getService()
.currentThread;
/** Kicks off running the next test in the array. */
function performNextTest()
{
if (++testIndex == testArray.length)
{
function performNextTest() {
if (++testIndex == testArray.length) {
do_test_finished();
try
{
try {
done();
}
catch (e)
{
} catch (e) {
do_report_unexpected_exception(e, "running test-completion callback");
}
return;
@ -468,15 +410,13 @@ function runRawTests(testArray, done)
waitToWriteOutput(outStream);
}
function waitForMoreInput(stream)
{
function waitForMoreInput(stream) {
reader.stream = stream;
stream = stream.QueryInterface(Ci.nsIAsyncInputStream);
stream.asyncWait(reader, 0, 0, currentThread);
}
function waitToWriteOutput(stream)
{
function waitToWriteOutput(stream) {
// Do the QueryInterface here, not earlier, because there is no
// guarantee that 'stream' passed in here been QIed to nsIAsyncOutputStream
// since the last GC.
@ -500,101 +440,77 @@ function runRawTests(testArray, done)
/** Reads data from the socket. */
var reader =
{
onInputStreamReady: function(stream)
{
onInputStreamReady(stream) {
Assert.ok(stream === this.stream);
try
{
try {
var bis = new BinaryInputStream(stream);
var av = 0;
try
{
try {
av = bis.available();
}
catch (e)
{
} catch (e) {
/* default to 0 */
do_note_exception(e);
}
if (av > 0)
{
if (av > 0) {
var quantum = 262144;
for (var start = 0; start < av; start += quantum)
{
for (var start = 0; start < av; start += quantum) {
var bytes = bis.readByteArray(Math.min(quantum, av - start));
received += String.fromCharCode.apply(null, bytes);
}
waitForMoreInput(stream);
return;
}
}
catch(e)
{
} catch (e) {
do_report_unexpected_exception(e);
}
var rawTest = testArray[testIndex];
try
{
try {
rawTest.responseCheck(received);
}
catch (e)
{
} catch (e) {
do_report_unexpected_exception(e);
}
finally
{
try
{
} finally {
try {
stream.close();
performNextTest();
}
catch (e)
{
} catch (e) {
do_report_unexpected_exception(e);
}
}
}
},
};
/** Writes data to the socket. */
var writer =
var writer =
{
onOutputStreamReady: function(stream)
{
onOutputStreamReady(stream) {
var str = testArray[testIndex].data[dataIndex];
var written = 0;
try
{
try {
written = stream.write(str, str.length);
if (written == str.length)
dataIndex++;
else
testArray[testIndex].data[dataIndex] = str.substring(written);
}
catch (e)
{
} catch (e) {
do_note_exception(e);
/* stream could have been closed, just ignore */
}
try
{
// Keep writing data while we can write and
try {
// Keep writing data while we can write and
// until there's no more data to read
if (written > 0 && dataIndex < testArray[testIndex].data.length)
waitToWriteOutput(stream);
else
stream.close();
}
catch (e)
{
} catch (e) {
do_report_unexpected_exception(e);
}
}
},
};
performNextTest();

File diff suppressed because it is too large Load diff

View file

@ -27,8 +27,7 @@ XPCOMUtils.defineLazyGetter(this, "tests", function() {
var srv;
function run_test()
{
function run_test() {
srv = createServer();
// base path
@ -54,16 +53,14 @@ const HEADER_COUNT = 1000;
// common properties *always* appended by server
// or invariants for every URL in paths
function commonCheck(ch)
{
function commonCheck(ch) {
Assert.ok(ch.contentLength > -1);
Assert.equal(ch.getResponseHeader("connection"), "close");
Assert.ok(!ch.isNoStoreResponse());
Assert.ok(!ch.isPrivateResponse());
}
function start_objHandler(ch, cx)
{
function start_objHandler(ch, cx) {
commonCheck(ch);
Assert.equal(ch.responseStatus, 200);
@ -78,8 +75,7 @@ function start_objHandler(ch, cx)
reqMin.value == respMin.value);
}
function start_functionHandler(ch, cx)
{
function start_functionHandler(ch, cx) {
commonCheck(ch);
Assert.equal(ch.responseStatus, 404);
@ -94,16 +90,14 @@ function start_functionHandler(ch, cx)
Assert.ok(respMaj.value == 1 && respMin.value == 1);
}
function start_non_existent_path(ch, cx)
{
function start_non_existent_path(ch, cx) {
commonCheck(ch);
Assert.equal(ch.responseStatus, 404);
Assert.ok(!ch.requestSucceeded);
}
function start_lots_of_headers(ch, cx)
{
function start_lots_of_headers(ch, cx) {
commonCheck(ch);
Assert.equal(ch.responseStatus, 200);
@ -118,8 +112,7 @@ function start_lots_of_headers(ch, cx)
// /objHandler
var objHandler =
{
handle: function(metadata, response)
{
handle(metadata, response) {
response.setStatusLine(metadata.httpVersion, 200, "OK");
response.setHeader("Content-Type", "text/plain", false);
@ -134,8 +127,7 @@ var objHandler =
body += " HTTP/" + metadata.httpVersion + "\n";
var headEnum = metadata.headers;
while (headEnum.hasMoreElements())
{
while (headEnum.hasMoreElements()) {
var fieldName = headEnum.getNext()
.QueryInterface(Ci.nsISupportsString)
.data;
@ -144,17 +136,11 @@ var objHandler =
response.bodyOutputStream.write(body, body.length);
},
QueryInterface: function(id)
{
if (id.equals(Ci.nsISupports) || id.equals(Ci.nsIHttpRequestHandler))
return this;
throw Cr.NS_ERROR_NOINTERFACE;
}
QueryInterface: ChromeUtils.generateQI(["nsIHttpRequestHandler"]),
};
// /functionHandler
function functionHandler(metadata, response)
{
function functionHandler(metadata, response) {
response.setStatusLine("1.1", 404, "Page Not Found");
response.setHeader("foopy", "quux-baz", false);
@ -167,8 +153,7 @@ function functionHandler(metadata, response)
}
// /lotsOfHeaders
function lotsOfHeadersHandler(request, response)
{
function lotsOfHeadersHandler(request, response) {
response.setHeader("Content-Type", "text/plain", false);
for (var i = 0; i < HEADER_COUNT; i++)

View file

@ -12,8 +12,7 @@
var srv;
function run_test()
{
function run_test() {
srv = createServer();
srv.registerPathHandler("/content-length", contentLength);
srv.start(-1);
@ -23,8 +22,7 @@ function run_test()
const REQUEST_DATA = "12345678901234567";
function contentLength(request, response)
{
function contentLength(request, response) {
Assert.equal(request.method, "POST");
Assert.equal(request.getHeader("Content-Length"), "017");
@ -38,19 +36,18 @@ function contentLength(request, response)
Assert.equal(data, REQUEST_DATA);
}
/***************
/** *************
* BEGIN TESTS *
***************/
XPCOMUtils.defineLazyGetter(this, 'tests', function() {
XPCOMUtils.defineLazyGetter(this, "tests", function() {
return [
new Test("http://localhost:" + srv.identity.primaryPort + "/content-length",
init_content_length),
];
});
function init_content_length(ch)
{
function init_content_length(ch) {
var content = Cc["@mozilla.org/io/string-input-stream;1"]
.createInstance(Ci.nsIStringInputStream);
content.data = REQUEST_DATA;

View file

@ -41,12 +41,11 @@ XPCOMUtils.defineLazyGetter(this, "tests", function() {
new Test(PREFIX + "/headers.txt",
init_byterange13, start_byterange13, null),
new Test(PREFIX + "/range.txt",
null, start_normal, stop_normal)
null, start_normal, stop_normal),
];
});
function run_test()
{
function run_test() {
srv = createServer();
var dir = do_get_file("data/ranges/");
srv.registerDirectory("/", dir);
@ -56,65 +55,55 @@ function run_test()
runHttpTests(tests, testComplete(srv));
}
function start_normal(ch, cx)
{
function start_normal(ch, cx) {
Assert.equal(ch.responseStatus, 200);
Assert.equal(ch.getResponseHeader("Content-Length"), "21");
Assert.equal(ch.getResponseHeader("Content-Type"), "text/plain");
}
function stop_normal(ch, cx, status, data)
{
function stop_normal(ch, cx, status, data) {
Assert.equal(data.length, 21);
Assert.equal(data[0], 0x54);
Assert.equal(data[0], 0x54);
Assert.equal(data[20], 0x0a);
}
function init_byterange(ch)
{
function init_byterange(ch) {
ch.setRequestHeader("Range", "bytes=10-", false);
}
function start_byterange(ch, cx)
{
function start_byterange(ch, cx) {
Assert.equal(ch.responseStatus, 206);
Assert.equal(ch.getResponseHeader("Content-Length"), "11");
Assert.equal(ch.getResponseHeader("Content-Type"), "text/plain");
Assert.equal(ch.getResponseHeader("Content-Range"), "bytes 10-20/21");
}
function stop_byterange(ch, cx, status, data)
{
function stop_byterange(ch, cx, status, data) {
Assert.equal(data.length, 11);
Assert.equal(data[0], 0x64);
Assert.equal(data[10], 0x0a);
}
function init_byterange2(ch)
{
function init_byterange2(ch) {
ch.setRequestHeader("Range", "bytes=21-", false);
}
function start_byterange2(ch, cx)
{
function start_byterange2(ch, cx) {
Assert.equal(ch.responseStatus, 416);
}
function init_byterange3(ch)
{
function init_byterange3(ch) {
ch.setRequestHeader("Range", "bytes=10-15", false);
}
function start_byterange3(ch, cx)
{
function start_byterange3(ch, cx) {
Assert.equal(ch.responseStatus, 206);
Assert.equal(ch.getResponseHeader("Content-Length"), "6");
Assert.equal(ch.getResponseHeader("Content-Type"), "text/plain");
Assert.equal(ch.getResponseHeader("Content-Range"), "bytes 10-15/21");
}
function stop_byterange3(ch, cx, status, data)
{
function stop_byterange3(ch, cx, status, data) {
Assert.equal(data.length, 6);
Assert.equal(data[0], 0x64);
Assert.equal(data[1], 0x20);
@ -124,28 +113,23 @@ function stop_byterange3(ch, cx, status, data)
Assert.equal(data[5], 0x73);
}
function init_byterange4(ch)
{
function init_byterange4(ch) {
ch.setRequestHeader("Range", "xbytes=21-", false);
}
function start_byterange4(ch, cx)
{
function start_byterange4(ch, cx) {
Assert.equal(ch.responseStatus, 400);
}
function init_byterange5(ch)
{
function init_byterange5(ch) {
ch.setRequestHeader("Range", "bytes=-5", false);
}
function start_byterange5(ch, cx)
{
function start_byterange5(ch, cx) {
Assert.equal(ch.responseStatus, 206);
}
function stop_byterange5(ch, cx, status, data)
{
function stop_byterange5(ch, cx, status, data) {
Assert.equal(data.length, 5);
Assert.equal(data[0], 0x65);
Assert.equal(data[1], 0x65);
@ -154,38 +138,32 @@ function stop_byterange5(ch, cx, status, data)
Assert.equal(data[4], 0x0a);
}
function init_byterange6(ch)
{
function init_byterange6(ch) {
ch.setRequestHeader("Range", "bytes=15-12", false);
}
function start_byterange6(ch, cx)
{
function start_byterange6(ch, cx) {
Assert.equal(ch.responseStatus, 200);
}
function stop_byterange6(ch, cx, status, data)
{
function stop_byterange6(ch, cx, status, data) {
Assert.equal(data.length, 21);
Assert.equal(data[0], 0x54);
Assert.equal(data[0], 0x54);
Assert.equal(data[20], 0x0a);
}
function init_byterange7(ch)
{
function init_byterange7(ch) {
ch.setRequestHeader("Range", "bytes=0-5", false);
}
function start_byterange7(ch, cx)
{
function start_byterange7(ch, cx) {
Assert.equal(ch.responseStatus, 206);
Assert.equal(ch.getResponseHeader("Content-Length"), "6");
Assert.equal(ch.getResponseHeader("Content-Type"), "text/plain");
Assert.equal(ch.getResponseHeader("Content-Range"), "bytes 0-5/21");
}
function stop_byterange7(ch, cx, status, data)
{
function stop_byterange7(ch, cx, status, data) {
Assert.equal(data.length, 6);
Assert.equal(data[0], 0x54);
Assert.equal(data[1], 0x68);
@ -195,84 +173,69 @@ function stop_byterange7(ch, cx, status, data)
Assert.equal(data[5], 0x73);
}
function init_byterange8(ch)
{
function init_byterange8(ch) {
ch.setRequestHeader("Range", "bytes=20-21", false);
}
function start_byterange8(ch, cx)
{
function start_byterange8(ch, cx) {
Assert.equal(ch.responseStatus, 206);
Assert.equal(ch.getResponseHeader("Content-Range"), "bytes 20-20/21");
}
function stop_byterange8(ch, cx, status, data)
{
function stop_byterange8(ch, cx, status, data) {
Assert.equal(data.length, 1);
Assert.equal(data[0], 0x0a);
}
function init_byterange9(ch)
{
function init_byterange9(ch) {
ch.setRequestHeader("Range", "bytes=020-021", false);
}
function start_byterange9(ch, cx)
{
function start_byterange9(ch, cx) {
Assert.equal(ch.responseStatus, 206);
}
function stop_byterange9(ch, cx, status, data)
{
function stop_byterange9(ch, cx, status, data) {
Assert.equal(data.length, 1);
Assert.equal(data[0], 0x0a);
}
function init_byterange10(ch)
{
function init_byterange10(ch) {
ch.setRequestHeader("Range", "bytes=-", false);
}
function start_byterange10(ch, cx)
{
function start_byterange10(ch, cx) {
Assert.equal(ch.responseStatus, 400);
}
function init_byterange11(ch)
{
function init_byterange11(ch) {
ch.setRequestHeader("Range", "bytes=-500", false);
}
function start_byterange11(ch, cx)
{
function start_byterange11(ch, cx) {
Assert.equal(ch.responseStatus, 206);
}
function stop_byterange11(ch, cx, status, data)
{
function stop_byterange11(ch, cx, status, data) {
Assert.equal(data.length, 21);
Assert.equal(data[0], 0x54);
Assert.equal(data[0], 0x54);
Assert.equal(data[20], 0x0a);
}
function start_byterange12(ch, cx)
{
function start_byterange12(ch, cx) {
Assert.equal(ch.responseStatus, 200);
Assert.equal(ch.getResponseHeader("Content-Length"), "0");
}
function stop_byterange12(ch, cx, status, data)
{
function stop_byterange12(ch, cx, status, data) {
Assert.equal(data.length, 0);
}
function init_byterange13(ch)
{
function init_byterange13(ch) {
ch.setRequestHeader("Range", "bytes=9999999-", false);
}
function start_byterange13(ch, cx)
{
function start_byterange13(ch, cx) {
Assert.equal(ch.responseStatus, 416);
Assert.equal(ch.getResponseHeader("X-SJS-Header"), "customized");
}

View file

@ -7,11 +7,11 @@
// exercises support for mod_cern_meta-style header/status line modification
var srv;
XPCOMUtils.defineLazyGetter(this, 'PREFIX', function() {
XPCOMUtils.defineLazyGetter(this, "PREFIX", function() {
return "http://localhost:" + srv.identity.primaryPort;
});
XPCOMUtils.defineLazyGetter(this, 'tests', function() {
XPCOMUtils.defineLazyGetter(this, "tests", function() {
return [
new Test(PREFIX + "/test_both.html",
null, start_testBoth, null),
@ -22,12 +22,11 @@ XPCOMUtils.defineLazyGetter(this, 'tests', function() {
new Test(PREFIX + "/test_status_override_nodesc.txt",
null, start_test_status_override_nodesc_txt, null),
new Test(PREFIX + "/caret_test.txt^",
null, start_caret_test_txt_, null)
null, start_caret_test_txt_, null),
];
});
function run_test()
{
function run_test() {
srv = createServer();
var cernDir = do_get_file("data/cern_meta/");
@ -41,33 +40,28 @@ function run_test()
// TEST DATA
function start_testBoth(ch, cx)
{
function start_testBoth(ch, cx) {
Assert.equal(ch.responseStatus, 501);
Assert.equal(ch.responseStatusText, "Unimplemented");
Assert.equal(ch.getResponseHeader("Content-Type"), "text/plain");
}
function start_test_ctype_override_txt(ch, cx)
{
function start_test_ctype_override_txt(ch, cx) {
Assert.equal(ch.getResponseHeader("Content-Type"), "text/html");
}
function start_test_status_override_html(ch, cx)
{
function start_test_status_override_html(ch, cx) {
Assert.equal(ch.responseStatus, 404);
Assert.equal(ch.responseStatusText, "Can't Find This");
}
function start_test_status_override_nodesc_txt(ch, cx)
{
function start_test_status_override_nodesc_txt(ch, cx) {
Assert.equal(ch.responseStatus, 732);
Assert.equal(ch.responseStatusText, "");
}
function start_caret_test_txt_(ch, cx)
{
function start_caret_test_txt_(ch, cx) {
Assert.equal(ch.responseStatus, 500);
Assert.equal(ch.responseStatusText, "This Isn't A Server Error");

View file

@ -10,14 +10,13 @@
var srv, dir, dirEntries;
XPCOMUtils.defineLazyGetter(this, 'BASE_URL', function() {
XPCOMUtils.defineLazyGetter(this, "BASE_URL", function() {
return "http://localhost:" + srv.identity.primaryPort + "/";
});
Cu.importGlobalProperties(["DOMParser"]);
function run_test()
{
function run_test() {
createTestDirectory();
srv = createServer();
@ -28,8 +27,7 @@ function run_test()
srv.start(-1);
function done()
{
function done() {
do_test_pending();
destroyTestDirectory();
srv.stop(function() { do_test_finished(); });
@ -38,8 +36,7 @@ function run_test()
runHttpTests(tests, done);
}
function createTestDirectory()
{
function createTestDirectory() {
dir = Cc["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIProperties)
.get("TmpD", Ci.nsIFile);
@ -80,19 +77,17 @@ function createTestDirectory()
dirEntries.push(files);
}
function destroyTestDirectory()
{
function destroyTestDirectory() {
dir.remove(true);
}
/*************
/** ***********
* UTILITIES *
*************/
/** Verifies data in bytes for the trailing-caret path above. */
function hiddenDataCheck(bytes, uri, path)
{
function hiddenDataCheck(bytes, uri, path) {
var data = String.fromCharCode.apply(null, bytes);
var parser = new DOMParser();
@ -100,12 +95,9 @@ function hiddenDataCheck(bytes, uri, path)
// Note: the index format isn't XML -- it's actually HTML -- but we require
// the index format also be valid XML, albeit XML without namespaces,
// XML declarations, etc. Doing this simplifies output checking.
try
{
try {
var doc = parser.parseFromString(data, "application/xml");
}
catch (e)
{
} catch (e) {
do_throw("document failed to parse as XML");
}
@ -133,8 +125,7 @@ function hiddenDataCheck(bytes, uri, path)
var dirEntries = [{name: "file.txt", isDirectory: false},
{name: "SHOULD_SEE_THIS.txt^", isDirectory: false}];
for (var i = 0; i < items.length; i++)
{
for (var i = 0; i < items.length; i++) {
var link = items[i].childNodes[0];
var f = dirEntries[i];
@ -164,8 +155,7 @@ function hiddenDataCheck(bytes, uri, path)
* without / if it's a directory) and an isDirectory property (with expected
* value)
*/
function dataCheck(bytes, uri, path, dirEntries)
{
function dataCheck(bytes, uri, path, dirEntries) {
var data = String.fromCharCode.apply(null, bytes);
var parser = new DOMParser();
@ -173,12 +163,9 @@ function dataCheck(bytes, uri, path, dirEntries)
// Note: the index format isn't XML -- it's actually HTML -- but we require
// the index format also be valid XML, albeit XML without namespaces,
// XML declarations, etc. Doing this simplifies output checking.
try
{
try {
var doc = parser.parseFromString(data, "application/xml");
}
catch (e)
{
} catch (e) {
do_throw("document failed to parse as XML");
}
@ -202,8 +189,7 @@ function dataCheck(bytes, uri, path, dirEntries)
var dirURI = ios.newURI(uri);
for (var i = 0; i < items.length; i++)
{
for (var i = 0; i < items.length; i++) {
var link = items[i].childNodes[0];
var f = dirEntries[i];
@ -221,21 +207,18 @@ function dataCheck(bytes, uri, path, dirEntries)
* append an object with name/isDirectory properties to lst corresponding
* to it if the file/directory could be created.
*/
function makeFile(name, isDirectory, parentDir, lst)
{
function makeFile(name, isDirectory, parentDir, lst) {
var type = Ci.nsIFile[isDirectory ? "DIRECTORY_TYPE" : "NORMAL_FILE_TYPE"];
var file = parentDir.clone();
try
{
try {
file.append(name);
file.create(type, 0o755);
lst.push({name: name, isDirectory: isDirectory});
}
catch (e) { /* OS probably doesn't like file name, skip */ }
lst.push({name, isDirectory});
} catch (e) { /* OS probably doesn't like file name, skip */ }
}
/*********
/** *******
* TESTS *
*********/
@ -248,23 +231,19 @@ XPCOMUtils.defineLazyGetter(this, "tests", function() {
});
// check top-level directory listing
function start(ch)
{
function start(ch) {
Assert.equal(ch.getResponseHeader("Content-Type"), "text/html;charset=utf-8");
}
function stopRootDirectory(ch, cx, status, data)
{
function stopRootDirectory(ch, cx, status, data) {
dataCheck(data, BASE_URL, "/", dirEntries[0]);
}
// check non-top-level, too
function stopFooDirectory(ch, cx, status, data)
{
function stopFooDirectory(ch, cx, status, data) {
dataCheck(data, BASE_URL + "foo/", "/foo/", dirEntries[1]);
}
// trailing-caret leaf with hidden files
function stopTrailingCaretDirectory(ch, cx, status, data)
{
function stopTrailingCaretDirectory(ch, cx, status, data) {
hiddenDataCheck(data, BASE_URL + "bar/folder^/", "/bar/folder^/");
}

View file

@ -18,8 +18,7 @@ XPCOMUtils.defineLazyGetter(this, "tests", function() {
];
});
function run_test()
{
function run_test() {
srv = createServer();
// register a few test paths
@ -33,22 +32,19 @@ function run_test()
// TEST DATA
function ensureEmpty(ch, cx)
{
function ensureEmpty(ch, cx) {
Assert.ok(ch.contentLength == 0);
}
// PATH HANDLERS
// /empty-body-unwritten
function emptyBodyUnwritten(metadata, response)
{
function emptyBodyUnwritten(metadata, response) {
response.setStatusLine("1.1", 200, "OK");
}
// /empty-body-written
function emptyBodyWritten(metadata, response)
{
function emptyBodyWritten(metadata, response) {
response.setStatusLine("1.1", 200, "OK");
var body = "";
response.bodyOutputStream.write(body, body.length);

View file

@ -22,8 +22,7 @@ XPCOMUtils.defineLazyGetter(this, "tests", function() {
var srv;
function run_test()
{
function run_test() {
srv = createServer();
srv.registerErrorHandler(404, throwsException);
@ -37,8 +36,7 @@ function run_test()
// TEST DATA
function checkStatusLine(channel, httpMaxVer, httpMinVer, httpCode, statusText)
{
function checkStatusLine(channel, httpMaxVer, httpMinVer, httpCode, statusText) {
Assert.equal(channel.responseStatus, httpCode);
Assert.equal(channel.responseStatusText, statusText);
@ -48,28 +46,23 @@ function checkStatusLine(channel, httpMaxVer, httpMinVer, httpCode, statusText)
Assert.equal(respMin.value, httpMinVer);
}
function start_throws_exception(ch, cx)
{
function start_throws_exception(ch, cx) {
checkStatusLine(ch, 1, 1, 500, "Internal Server Error");
}
function start_nonexistent_404_fails_so_400(ch, cx)
{
function start_nonexistent_404_fails_so_400(ch, cx) {
checkStatusLine(ch, 1, 1, 400, "Bad Request");
}
function start_multiple_exceptions_500(ch, cx)
{
function start_multiple_exceptions_500(ch, cx) {
checkStatusLine(ch, 1, 1, 500, "Internal Server Error");
}
function succeeded(ch, cx, status, data)
{
function succeeded(ch, cx, status, data) {
Assert.ok(Components.isSuccessCode(status));
}
function register400Handler(ch)
{
function register400Handler(ch) {
srv.registerErrorHandler(400, throwsException);
}
@ -77,8 +70,7 @@ function register400Handler(ch)
// PATH HANDLERS
// /throws/exception (and also a 404 and 400 error handler)
function throwsException(metadata, response)
{
function throwsException(metadata, response) {
throw "this shouldn't cause an exit...";
do_throw("Not reached!");
}

View file

@ -8,8 +8,7 @@
var srv;
function run_test()
{
function run_test() {
srv;
srv = createServer();
@ -20,12 +19,11 @@ function run_test()
}
/************
/** **********
* HANDLERS *
************/
function pathHandler(request, response)
{
function pathHandler(request, response) {
response.setHeader("Cache-Control", "no-cache", false);
response.setHeader("Proxy-Authenticate", "First line 1", true);
@ -43,19 +41,18 @@ function pathHandler(request, response)
response.setHeader("X-Single-Header-Merge", "Single 2", true);
}
/***************
/** *************
* BEGIN TESTS *
***************/
XPCOMUtils.defineLazyGetter(this, "tests", function() {
return [
new Test("http://localhost:" + srv.identity.primaryPort + "/path-handler",
null, check)
null, check),
];
});
function check(ch, cx)
{
function check(ch, cx) {
var headerValue;
headerValue = ch.getResponseHeader("Proxy-Authenticate");

View file

@ -19,14 +19,10 @@
* @param headers
* an nsHttpHeaders object to use to check validity
*/
function assertValidHeader(fieldName, fieldValue, headers)
{
try
{
function assertValidHeader(fieldName, fieldValue, headers) {
try {
headers.setHeader(fieldName, fieldValue, false);
}
catch (e)
{
} catch (e) {
do_throw("Unexpected exception thrown: " + e);
}
}
@ -41,32 +37,26 @@ function assertValidHeader(fieldName, fieldValue, headers)
* @param headers
* an nsHttpHeaders object to use to check validity
*/
function assertInvalidHeader(fieldName, fieldValue, headers)
{
try
{
function assertInvalidHeader(fieldName, fieldValue, headers) {
try {
headers.setHeader(fieldName, fieldValue, false);
throw "Setting (" + fieldName + ", " +
fieldValue + ") as header succeeded!";
}
catch (e)
{
} catch (e) {
if (e.result !== Cr.NS_ERROR_INVALID_ARG)
do_throw("Unexpected exception thrown: " + e);
}
}
function run_test()
{
function run_test() {
testHeaderValidity();
testGetHeader();
testHeaderEnumerator();
testHasHeader();
}
function testHeaderValidity()
{
function testHeaderValidity() {
var headers = new nsHttpHeaders();
assertInvalidHeader("f o", "bar", headers);
@ -102,8 +92,7 @@ function testHeaderValidity()
assertValidHeader("~~~", "b\r\n\tbar", headers);
}
function testGetHeader()
{
function testGetHeader() {
var headers = new nsHttpHeaders();
headers.setHeader("Content-Type", "text/html", false);
@ -114,46 +103,38 @@ function testGetHeader()
var c = headers.getHeader("test");
Assert.equal(c, "FOO");
try
{
try {
headers.getHeader(":");
throw "Failed to throw for invalid header";
}
catch (e)
{
} catch (e) {
if (e.result !== Cr.NS_ERROR_INVALID_ARG)
do_throw("headers.getHeader(':') must throw invalid arg");
}
try
{
try {
headers.getHeader("valid");
throw 'header doesn\'t exist';
}
catch (e)
{
throw "header doesn't exist";
} catch (e) {
if (e.result !== Cr.NS_ERROR_NOT_AVAILABLE)
do_throw("shouldn't be a header named 'valid' in headers!");
}
}
function testHeaderEnumerator()
{
function testHeaderEnumerator() {
var headers = new nsHttpHeaders();
var heads =
{
"foo": "17",
"baz": "two six niner",
"decaf": "class Program { int .7; int main(){ .7 = 5; return 7 - .7; } }"
"decaf": "class Program { int .7; int main(){ .7 = 5; return 7 - .7; } }",
};
for (var i in heads)
headers.setHeader(i, heads[i], false);
var en = headers.enumerator;
while (en.hasMoreElements())
{
while (en.hasMoreElements()) {
var it = en.getNext().QueryInterface(Ci.nsISupportsString).data;
Assert.ok(it.toLowerCase() in heads);
delete heads[it.toLowerCase()];
@ -164,8 +145,7 @@ function testHeaderEnumerator()
}
function testHasHeader()
{
function testHasHeader() {
var headers = new nsHttpHeaders();
headers.setHeader("foo", "bar", false);
@ -176,13 +156,10 @@ function testHasHeader()
headers.setHeader("f`'~", "bar", false);
Assert.ok(headers.hasHeader("F`'~"));
try
{
try {
headers.hasHeader(":");
throw "failed to throw";
}
catch (e)
{
} catch (e) {
if (e.result !== Cr.NS_ERROR_INVALID_ARG)
do_throw(".hasHeader for an invalid name should throw");
}

View file

@ -15,8 +15,7 @@ const FAKE_PORT_TWO = 8889;
var srv, id;
function run_test()
{
function run_test() {
dumpn("*** run_test");
srv = createServer();
@ -78,22 +77,17 @@ function run_test()
// restart it on the correct port, to exercise port-changing behaviors at
// server start and stop.
do_test_pending();
srv.stop(function()
{
try
{
srv.stop(function() {
try {
do_test_pending();
run_test_2();
}
finally
{
} finally {
do_test_finished();
}
});
}
function run_test_2()
{
function run_test_2() {
dumpn("*** run_test_2");
do_test_finished();
@ -141,22 +135,17 @@ function run_test_2()
Assert.ok(!id.has("http", "127.0.0.1", FAKE_PORT_ONE));
do_test_pending();
srv.stop(function()
{
try
{
srv.stop(function() {
try {
do_test_pending();
run_test_3();
}
finally
{
} finally {
do_test_finished();
}
});
}
function run_test_3()
{
function run_test_3() {
dumpn("*** run_test_3");
do_test_finished();
@ -212,7 +201,7 @@ function run_test_3()
}
/*********************
/** *******************
* UTILITY FUNCTIONS *
*********************/
@ -223,37 +212,27 @@ function run_test_3()
* @param id : nsIHttpServerIdentity
* the server identity to test
*/
function checkPrimariesThrow(id)
{
function checkPrimariesThrow(id) {
var threw = false;
try
{
try {
id.primaryScheme;
}
catch (e)
{
} catch (e) {
threw = e.result === Cr.NS_ERROR_NOT_INITIALIZED;
}
Assert.ok(threw);
threw = false;
try
{
try {
id.primaryHost;
}
catch (e)
{
} catch (e) {
threw = e.result === Cr.NS_ERROR_NOT_INITIALIZED;
}
Assert.ok(threw);
threw = false;
try
{
try {
id.primaryPort;
}
catch (e)
{
} catch (e) {
threw = e.result === Cr.NS_ERROR_NOT_INITIALIZED;
}
Assert.ok(threw);
@ -262,8 +241,7 @@ function checkPrimariesThrow(id)
/**
* Utility function to check for a 400 response.
*/
function check400(data)
{
function check400(data) {
var iter = LineIterator(data);
// Status-Line
@ -272,7 +250,7 @@ function check400(data)
}
/***************
/** *************
* BEGIN TESTS *
***************/
@ -284,15 +262,13 @@ var tests = [];
// HTTP/1.0 request, to ensure we see our default scheme/host/port
function http10Request(request, response)
{
function http10Request(request, response) {
writeDetails(request, response);
response.setStatusLine("1.0", 200, "TEST PASSED");
}
data = "GET /http/1.0-request HTTP/1.0\r\n" +
"\r\n";
function check10(data)
{
function check10(data) {
var iter = LineIterator(data);
// Status-Line
@ -382,16 +358,14 @@ tests.push(test);
// HTTP/1.1 request, correct Host header, expect handler's response
function http11goodHost(request, response)
{
function http11goodHost(request, response) {
writeDetails(request, response);
response.setStatusLine("1.1", 200, "TEST PASSED");
}
data = "GET /http/1.1-good-host HTTP/1.1\r\n" +
"Host: localhost:4444\r\n" +
"\r\n";
function check11goodHost(data)
{
function check11goodHost(data) {
var iter = LineIterator(data);
// Status-Line
@ -419,16 +393,14 @@ tests.push(test);
// HTTP/1.1 request, Host header is secondary identity
function http11ipHost(request, response)
{
function http11ipHost(request, response) {
writeDetails(request, response);
response.setStatusLine("1.1", 200, "TEST PASSED");
}
data = "GET /http/1.1-ip-host HTTP/1.1\r\n" +
"Host: 127.0.0.1:4444\r\n" +
"\r\n";
function check11ipHost(data)
{
function check11ipHost(data) {
var iter = LineIterator(data);
// Status-Line
@ -494,8 +466,7 @@ tests.push(test);
data = "GET http://localhost:4444/http/1.1-good-host HTTP/1.1\r\n" +
"Host: yippity-skippity\r\n" +
"\r\n";
function checkInaccurate(data)
{
function checkInaccurate(data) {
check11goodHost(data);
// dynamism setup
@ -512,8 +483,7 @@ tests.push(test);
data = "GET /http/1.0-request HTTP/1.0\r\n" +
"Host: not-localhost:4444\r\n" +
"\r\n";
function check10ip(data)
{
function check10ip(data) {
var iter = LineIterator(data);
// Status-Line
@ -541,16 +511,14 @@ tests.push(test);
// HTTP/1.1 request, Host header with implied port
function http11goodHostWackyPort(request, response)
{
function http11goodHostWackyPort(request, response) {
writeDetails(request, response);
response.setStatusLine("1.1", 200, "TEST PASSED");
}
data = "GET /http/1.1-good-host-wacky-port HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"\r\n";
function check11goodHostWackyPort(data)
{
function check11goodHostWackyPort(data) {
var iter = LineIterator(data);
// Status-Line

View file

@ -6,8 +6,7 @@
// test that the LineData internal data structure works correctly
function run_test()
{
function run_test() {
var data = new LineData();
data.appendBytes(["a".charCodeAt(0), CR]);

View file

@ -41,13 +41,12 @@ XPCOMUtils.defineLazyGetter(this, "tests", function() {
new Test(PREFIX + "/end-caret^/normal-file.txt",
null, start_normal_file_txt, null),
new Test(PREFIX + "/end-caret^/folder^/file.txt",
null, start_folder__file_txt, null)
null, start_folder__file_txt, null),
];
});
function run_test()
{
function run_test() {
srv = createServer();
// make sure underscores work in directories "mounted" in directories with
@ -65,26 +64,22 @@ function run_test()
// TEST DATA
function start_bar_html_(ch, cx)
{
function start_bar_html_(ch, cx) {
Assert.equal(ch.responseStatus, 200);
Assert.equal(ch.getResponseHeader("Content-Type"), "text/html");
}
function start_foo_html_(ch, cx)
{
function start_foo_html_(ch, cx) {
Assert.equal(ch.responseStatus, 404);
}
function start_normal_file_txt(ch, cx)
{
function start_normal_file_txt(ch, cx) {
Assert.equal(ch.responseStatus, 200);
Assert.equal(ch.getResponseHeader("Content-Type"), "text/plain");
}
function start_folder__file_txt(ch, cx)
{
function start_folder__file_txt(ch, cx) {
Assert.equal(ch.responseStatus, 200);
Assert.equal(ch.getResponseHeader("Content-Type"), "text/plain");
}

View file

@ -14,8 +14,7 @@ XPCOMUtils.defineLazyGetter(this, "PREPATH", function() {
var srv;
function run_test()
{
function run_test() {
srv = createServer();
for (var path in handlers)
srv.registerPathHandler(path, handlers[path]);
@ -25,7 +24,7 @@ function run_test()
}
/***************
/** *************
* BEGIN TESTS *
***************/
@ -37,23 +36,19 @@ XPCOMUtils.defineLazyGetter(this, "tests", function() {
new Test(PREPATH + "/handleAsync2", init_handleAsync2, start_handleAsync2,
stop_handleAsync2),
new Test(PREPATH + "/handleAsyncOrdering", null, null,
stop_handleAsyncOrdering)
stop_handleAsyncOrdering),
];
});
var handlers = {};
function handleSync(request, response)
{
function handleSync(request, response) {
response.setStatusLine(request.httpVersion, 500, "handleSync fail");
try
{
try {
response.finish();
do_throw("finish called on sync response");
}
catch (e)
{
} catch (e) {
isException(e, Cr.NS_ERROR_UNEXPECTED);
}
@ -61,14 +56,12 @@ function handleSync(request, response)
}
handlers["/handleSync"] = handleSync;
function start_handleSync(ch, cx)
{
function start_handleSync(ch, cx) {
Assert.equal(ch.responseStatus, 200);
Assert.equal(ch.responseStatusText, "handleSync pass");
}
function handleAsync1(request, response)
{
function handleAsync1(request, response) {
response.setStatusLine(request.httpVersion, 500, "Old status line!");
response.setHeader("X-Foo", "old value", false);
@ -79,126 +72,94 @@ function handleAsync1(request, response)
response.finish();
try
{
try {
response.setStatusLine(request.httpVersion, 500, "Too late!");
do_throw("late setStatusLine didn't throw");
}
catch (e)
{
} catch (e) {
isException(e, Cr.NS_ERROR_NOT_AVAILABLE);
}
try
{
try {
response.setHeader("X-Foo", "late value", false);
do_throw("late setHeader didn't throw");
}
catch (e)
{
} catch (e) {
isException(e, Cr.NS_ERROR_NOT_AVAILABLE);
}
try
{
try {
response.bodyOutputStream;
do_throw("late bodyOutputStream get didn't throw");
}
catch (e)
{
} catch (e) {
isException(e, Cr.NS_ERROR_NOT_AVAILABLE);
}
try
{
try {
response.write("fugly");
do_throw("late write() didn't throw");
}
catch (e)
{
} catch (e) {
isException(e, Cr.NS_ERROR_NOT_AVAILABLE);
}
}
handlers["/handleAsync1"] = handleAsync1;
function start_handleAsync1(ch, cx)
{
function start_handleAsync1(ch, cx) {
Assert.equal(ch.responseStatus, 200);
Assert.equal(ch.responseStatusText, "New status line!");
Assert.equal(ch.getResponseHeader("X-Foo"), "new value");
}
function stop_handleAsync1(ch, cx, status, data)
{
function stop_handleAsync1(ch, cx, status, data) {
Assert.equal(data.length, 0);
}
const startToHeaderDelay = 500;
const startToFinishedDelay = 750;
function handleAsync2(request, response)
{
function handleAsync2(request, response) {
response.processAsync();
response.setStatusLine(request.httpVersion, 200, "Status line");
response.setHeader("X-Custom-Header", "value", false);
callLater(startToHeaderDelay, function()
{
callLater(startToHeaderDelay, function() {
var body = "BO";
response.bodyOutputStream.write(body, body.length);
try
{
try {
response.setStatusLine(request.httpVersion, 500, "after body write");
do_throw("setStatusLine succeeded");
}
catch (e)
{
} catch (e) {
isException(e, Cr.NS_ERROR_NOT_AVAILABLE);
}
try
{
try {
response.setHeader("X-Custom-Header", "new 1", false);
}
catch (e)
{
} catch (e) {
isException(e, Cr.NS_ERROR_NOT_AVAILABLE);
}
callLater(startToFinishedDelay - startToHeaderDelay, function()
{
callLater(startToFinishedDelay - startToHeaderDelay, function() {
var body = "DY";
response.bodyOutputStream.write(body, body.length);
response.finish();
response.finish(); // idempotency
try
{
try {
response.setStatusLine(request.httpVersion, 500, "after finish");
}
catch (e)
{
} catch (e) {
isException(e, Cr.NS_ERROR_NOT_AVAILABLE);
}
try
{
try {
response.setHeader("X-Custom-Header", "new 2", false);
}
catch (e)
{
} catch (e) {
isException(e, Cr.NS_ERROR_NOT_AVAILABLE);
}
try
{
try {
response.write("EVIL");
}
catch (e)
{
} catch (e) {
isException(e, Cr.NS_ERROR_NOT_AVAILABLE);
}
});
@ -208,14 +169,12 @@ handlers["/handleAsync2"] = handleAsync2;
var startTime_handleAsync2;
function init_handleAsync2(ch)
{
function init_handleAsync2(ch) {
var now = startTime_handleAsync2 = Date.now();
dumpn("*** init_HandleAsync2: start time " + now);
}
function start_handleAsync2(ch, cx)
{
function start_handleAsync2(ch, cx) {
var now = Date.now();
dumpn("*** start_handleAsync2: onStartRequest time " + now + ", " +
(now - startTime_handleAsync2) + "ms after start time");
@ -226,8 +185,7 @@ function start_handleAsync2(ch, cx)
Assert.equal(ch.getResponseHeader("X-Custom-Header"), "value");
}
function stop_handleAsync2(ch, cx, status, data)
{
function stop_handleAsync2(ch, cx, status, data) {
var now = Date.now();
dumpn("*** stop_handleAsync2: onStopRequest time " + now + ", " +
(now - startTime_handleAsync2) + "ms after header time");
@ -243,8 +201,7 @@ function stop_handleAsync2(ch, cx, status, data)
* but certainly deadlock, since we're trying to read/write all this data in one
* process on a single thread.
*/
function handleAsyncOrdering(request, response)
{
function handleAsyncOrdering(request, response) {
var out = new BinaryOutputStream(response.bodyOutputStream);
var data = [];
@ -254,34 +211,25 @@ function handleAsyncOrdering(request, response)
var writeData =
{
run: function()
{
if (count-- === 0)
{
run() {
if (count-- === 0) {
response.finish();
return;
}
try
{
try {
out.writeByteArray(data, data.length);
step();
}
catch (e)
{
try
{
} catch (e) {
try {
do_throw("error writing data: " + e);
}
finally
{
} finally {
response.finish();
}
}
}
},
};
function step()
{
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
@ -292,11 +240,9 @@ function handleAsyncOrdering(request, response)
}
handlers["/handleAsyncOrdering"] = handleAsyncOrdering;
function stop_handleAsyncOrdering(ch, cx, status, data)
{
function stop_handleAsyncOrdering(ch, cx, status, data) {
Assert.equal(data.length, 20 * 65536);
data.forEach(function(v, index)
{
data.forEach(function(v, index) {
if (v !== 0)
do_throw("value " + v + " at index " + index + " should be zero");
});

View file

@ -21,17 +21,13 @@ XPCOMUtils.defineLazyGetter(this, "tests", function() {
var srv;
function run_test()
{
function run_test() {
srv = createServer();
var qi;
try
{
try {
qi = srv.identity.QueryInterface(Ci.nsIHttpServerIdentity);
}
catch (e)
{
} catch (e) {
var exstr = ("" + e).split(/[\x09\x20-\x7f\x81-\xff]+/)[0];
do_throw("server identity didn't QI: " + exstr);
return;
@ -48,60 +44,49 @@ function run_test()
// TEST DATA
function start_test(ch, cx)
{
function start_test(ch, cx) {
Assert.equal(ch.responseStatusText, "QI Tests Passed");
Assert.equal(ch.responseStatus, 200);
}
function start_sjs_qi(ch, cx)
{
function start_sjs_qi(ch, cx) {
Assert.equal(ch.responseStatusText, "SJS QI Tests Passed");
Assert.equal(ch.responseStatus, 200);
}
function testHandler(request, response)
{
function testHandler(request, response) {
var exstr;
var qid;
response.setStatusLine(request.httpVersion, 500, "FAIL");
var passed = false;
try
{
try {
qid = request.QueryInterface(Ci.nsIHttpRequest);
passed = qid === request;
}
catch (e)
{
} catch (e) {
exstr = ("" + e).split(/[\x09\x20-\x7f\x81-\xff]+/)[0];
response.setStatusLine(request.httpVersion, 500,
"request doesn't QI: " + exstr);
return;
}
if (!passed)
{
if (!passed) {
response.setStatusLine(request.httpVersion, 500, "request QI'd wrongly?");
return;
}
passed = false;
try
{
try {
qid = response.QueryInterface(Ci.nsIHttpResponse);
passed = qid === response;
}
catch (e)
{
} catch (e) {
exstr = ("" + e).split(/[\x09\x20-\x7f\x81-\xff]+/)[0];
response.setStatusLine(request.httpVersion, 500,
"response doesn't QI: " + exstr);
return;
}
if (!passed)
{
if (!passed) {
response.setStatusLine(request.httpVersion, 500, "response QI'd wrongly?");
return;
}

View file

@ -11,33 +11,28 @@ XPCOMUtils.defineLazyGetter(this, "BASE", function() {
});
function nocache(ch)
{
function nocache(ch) {
ch.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE; // important!
}
function notFound(ch)
{
function notFound(ch) {
Assert.equal(ch.responseStatus, 404);
Assert.ok(!ch.requestSucceeded);
}
function checkOverride(ch)
{
function checkOverride(ch) {
Assert.equal(ch.responseStatus, 200);
Assert.equal(ch.responseStatusText, "OK");
Assert.ok(ch.requestSucceeded);
Assert.equal(ch.getResponseHeader("Override-Succeeded"), "yes");
}
function check200(ch)
{
function check200(ch) {
Assert.equal(ch.responseStatus, 200);
Assert.equal(ch.responseStatusText, "OK");
}
function checkFile(ch, cx, status, data)
{
function checkFile(ch, cx, status, data) {
Assert.equal(ch.responseStatus, 200);
Assert.ok(ch.requestSucceeded);
@ -52,18 +47,17 @@ function checkFile(ch, cx, status, data)
XPCOMUtils.defineLazyGetter(this, "tests", function() {
return [
/***********************
/** *********************
* without a base path *
***********************/
new Test(BASE + "/test_registerdirectory.js",
nocache, notFound, null),
/********************
/** ******************
* with a base path *
********************/
new Test(BASE + "/test_registerdirectory.js",
function(ch)
{
function(ch) {
nocache(ch);
serverBasePath = testsDirectory.clone();
srv.registerDirectory("/", serverBasePath);
@ -71,12 +65,11 @@ XPCOMUtils.defineLazyGetter(this, "tests", function() {
null,
checkFile),
/*****************************
/** ***************************
* without a base path again *
*****************************/
new Test(BASE + "/test_registerdirectory.js",
function(ch)
{
function(ch) {
nocache(ch);
serverBasePath = null;
srv.registerDirectory("/", serverBasePath);
@ -84,12 +77,11 @@ XPCOMUtils.defineLazyGetter(this, "tests", function() {
notFound,
null),
/***************************
/** *************************
* registered path handler *
***************************/
new Test(BASE + "/test_registerdirectory.js",
function(ch)
{
function(ch) {
nocache(ch);
srv.registerPathHandler("/test_registerdirectory.js",
override_test_registerdirectory);
@ -97,24 +89,22 @@ XPCOMUtils.defineLazyGetter(this, "tests", function() {
checkOverride,
null),
/************************
/** **********************
* removed path handler *
************************/
new Test(BASE + "/test_registerdirectory.js",
function init_registerDirectory6(ch)
{
function init_registerDirectory6(ch) {
nocache(ch);
srv.registerPathHandler("/test_registerdirectory.js", null);
},
notFound,
null),
/********************
/** ******************
* with a base path *
********************/
new Test(BASE + "/test_registerdirectory.js",
function(ch)
{
function(ch) {
nocache(ch);
// set the base path again
@ -124,12 +114,11 @@ XPCOMUtils.defineLazyGetter(this, "tests", function() {
null,
checkFile),
/*************************
/** ***********************
* ...and a path handler *
*************************/
new Test(BASE + "/test_registerdirectory.js",
function(ch)
{
function(ch) {
nocache(ch);
srv.registerPathHandler("/test_registerdirectory.js",
override_test_registerdirectory);
@ -137,12 +126,11 @@ XPCOMUtils.defineLazyGetter(this, "tests", function() {
checkOverride,
null),
/************************
/** **********************
* removed base handler *
************************/
new Test(BASE + "/test_registerdirectory.js",
function(ch)
{
function(ch) {
nocache(ch);
serverBasePath = null;
srv.registerDirectory("/", serverBasePath);
@ -150,24 +138,22 @@ XPCOMUtils.defineLazyGetter(this, "tests", function() {
checkOverride,
null),
/************************
/** **********************
* removed path handler *
************************/
new Test(BASE + "/test_registerdirectory.js",
function(ch)
{
function(ch) {
nocache(ch);
srv.registerPathHandler("/test_registerdirectory.js", null);
},
notFound,
null),
/*************************
/** ***********************
* mapping set up, works *
*************************/
new Test(BASE + "/foo/test_registerdirectory.js",
function(ch)
{
function(ch) {
nocache(ch);
serverBasePath = testsDirectory.clone();
srv.registerDirectory("/foo/", serverBasePath);
@ -175,7 +161,7 @@ XPCOMUtils.defineLazyGetter(this, "tests", function() {
check200,
null),
/*********************
/** *******************
* no mapping, fails *
*********************/
new Test(BASE + "/foo/test_registerdirectory.js/test_registerdirectory.js",
@ -183,12 +169,11 @@ XPCOMUtils.defineLazyGetter(this, "tests", function() {
notFound,
null),
/******************
/** ****************
* mapping, works *
******************/
new Test(BASE + "/foo/test_registerdirectory.js/test_registerdirectory.js",
function(ch)
{
function(ch) {
nocache(ch);
srv.registerDirectory("/foo/test_registerdirectory.js/",
serverBasePath);
@ -196,41 +181,39 @@ XPCOMUtils.defineLazyGetter(this, "tests", function() {
null,
checkFile),
/************************************
/** **********************************
* two mappings set up, still works *
************************************/
new Test(BASE + "/foo/test_registerdirectory.js",
nocache, null, checkFile),
/**************************
/** ************************
* remove topmost mapping *
**************************/
new Test(BASE + "/foo/test_registerdirectory.js",
function(ch)
{
function(ch) {
nocache(ch);
srv.registerDirectory("/foo/", null);
},
notFound,
null),
/**************************************
/** ************************************
* lower mapping still present, works *
**************************************/
new Test(BASE + "/foo/test_registerdirectory.js/test_registerdirectory.js",
nocache, null, checkFile),
/*******************
/** *****************
* mapping removed *
*******************/
new Test(BASE + "/foo/test_registerdirectory.js/test_registerdirectory.js",
function(ch)
{
function(ch) {
nocache(ch);
srv.registerDirectory("/foo/test_registerdirectory.js/", null);
},
notFound,
null)
null),
];
});
@ -239,8 +222,7 @@ var srv;
var serverBasePath;
var testsDirectory;
function run_test()
{
function run_test() {
testsDirectory = do_get_cwd();
srv = createServer();
@ -253,8 +235,7 @@ function run_test()
// PATH HANDLERS
// override of /test_registerdirectory.js
function override_test_registerdirectory(metadata, response)
{
function override_test_registerdirectory(metadata, response) {
response.setStatusLine("1.1", 200, "OK");
response.setHeader("Override-Succeeded", "yes", false);

View file

@ -12,13 +12,11 @@ XPCOMUtils.defineLazyGetter(this, "BASE", function() {
var file = do_get_file("test_registerfile.js");
function onStart(ch, cx)
{
function onStart(ch, cx) {
Assert.equal(ch.responseStatus, 200);
}
function onStop(ch, cx, status, data)
{
function onStop(ch, cx, status, data) {
// not sufficient for equality, but not likely to be wrong!
Assert.equal(data.length, file.fileSize);
}
@ -29,17 +27,13 @@ XPCOMUtils.defineLazyGetter(this, "test", function() {
var srv;
function run_test()
{
function run_test() {
srv = createServer();
try
{
try {
srv.registerFile("/foo", do_get_profile());
throw "registerFile succeeded!";
}
catch (e)
{
} catch (e) {
isException(e, Cr.NS_ERROR_INVALID_ARG);
}

View file

@ -10,21 +10,17 @@ XPCOMUtils.defineLazyGetter(this, "BASE", function() {
return "http://localhost:" + srv.identity.primaryPort;
});
function nocache(ch)
{
function nocache(ch) {
ch.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE; // important!
}
function notFound(ch)
{
function notFound(ch) {
Assert.equal(ch.responseStatus, 404);
Assert.ok(!ch.requestSucceeded);
}
function makeCheckOverride(magic)
{
return (function checkOverride(ch)
{
function makeCheckOverride(magic) {
return (function checkOverride(ch) {
Assert.equal(ch.responseStatus, 200);
Assert.equal(ch.responseStatusText, "OK");
Assert.ok(ch.requestSucceeded);
@ -42,57 +38,52 @@ XPCOMUtils.defineLazyGetter(this, "tests", function() {
makeCheckOverride("subpath")),
new Test(BASE + "/prefix/dummy", removeHandlers, null, notFound),
new Test(BASE + "/prefix/subpath/dummy", newPrefixHandler, null,
makeCheckOverride("subpath"))
makeCheckOverride("subpath")),
];
});
/***************************
/** *************************
* registered prefix handler *
***************************/
function prefixHandler(channel)
{
function prefixHandler(channel) {
nocache(channel);
srv.registerPrefixHandler("/prefix/", makeOverride("prefix"));
}
/********************************
/** ******************************
* registered path handler on top *
********************************/
function pathHandler(channel)
{
function pathHandler(channel) {
nocache(channel);
srv.registerPathHandler("/prefix/dummy", makeOverride("path"));
}
/**********************************
/** ********************************
* registered longer prefix handler *
**********************************/
function longerPrefixHandler(channel)
{
function longerPrefixHandler(channel) {
nocache(channel);
srv.registerPrefixHandler("/prefix/subpath/", makeOverride("subpath"));
}
/************************
/** **********************
* removed prefix handler *
************************/
function removeHandlers(channel)
{
function removeHandlers(channel) {
nocache(channel);
srv.registerPrefixHandler("/prefix/", null);
srv.registerPathHandler("/prefix/dummy", null);
}
/*****************************
/** ***************************
* re-register shorter handler *
*****************************/
function newPrefixHandler(channel)
{
function newPrefixHandler(channel) {
nocache(channel);
srv.registerPrefixHandler("/prefix/", makeOverride("prefix"));
}
@ -101,8 +92,7 @@ var srv;
var serverBasePath;
var testsDirectory;
function run_test()
{
function run_test() {
testsDirectory = do_get_profile();
srv = createServer();
@ -114,10 +104,8 @@ function run_test()
// PATH HANDLERS
// generate an override
function makeOverride(magic)
{
return (function override(metadata, response)
{
function makeOverride(magic) {
return (function override(metadata, response) {
response.setStatusLine("1.1", 200, "OK");
response.setHeader("Override-Succeeded", magic, false);

View file

@ -14,8 +14,7 @@ var srv = createServer();
srv.start(-1);
const PORT = srv.identity.primaryPort;
function run_test()
{
function run_test() {
srv.registerPathHandler("/lots-of-leading-blank-lines",
lotsOfLeadingBlankLines);
srv.registerPathHandler("/very-long-request-line",
@ -25,7 +24,7 @@ function run_test()
}
/***************
/** *************
* BEGIN TESTS *
***************/
@ -33,8 +32,7 @@ var test, data, str;
var tests = [];
function veryLongRequestLine(request, response)
{
function veryLongRequestLine(request, response) {
writeDetails(request, response);
response.setStatusLine(request.httpVersion, 200, "TEST PASSED");
}
@ -57,8 +55,7 @@ data = [];
for (var i = 0; i < str.length; i += 16384)
data.push(str.substr(i, 16384));
function checkVeryLongRequestLine(data)
{
function checkVeryLongRequestLine(data) {
var iter = LineIterator(data);
print("data length: " + data.length);
@ -87,8 +84,7 @@ test = new RawTest("localhost", PORT, data, checkVeryLongRequestLine),
tests.push(test);
function lotsOfLeadingBlankLines(request, response)
{
function lotsOfLeadingBlankLines(request, response) {
writeDetails(request, response);
response.setStatusLine(request.httpVersion, 200, "TEST PASSED");
}
@ -104,8 +100,7 @@ data = [];
for (var i = 0; i < str.length; i += 100)
data.push(str.substr(i, 100));
function checkLotsOfLeadingBlankLines(data)
{
function checkLotsOfLeadingBlankLines(data) {
var iter = LineIterator(data);
// Status-Line

View file

@ -17,8 +17,7 @@ XPCOMUtils.defineLazyGetter(this, "tests", function() {
var srv;
function run_test()
{
function run_test() {
srv = createServer();
srv.registerPathHandler("/writeString", writeString);
@ -31,25 +30,21 @@ function run_test()
// TEST DATA
function succeeded(ch, cx, status, data)
{
function succeeded(ch, cx, status, data) {
Assert.ok(Components.isSuccessCode(status));
Assert.equal(data.map(v => String.fromCharCode(v)).join(""), "1234");
}
function check_1234(ch, cx)
{
function check_1234(ch, cx) {
Assert.equal(ch.getResponseHeader("Content-Length"), "4");
}
// PATH HANDLERS
function writeString(metadata, response)
{
function writeString(metadata, response) {
response.write("1234");
}
function writeInt(metadata, response)
{
function writeInt(metadata, response) {
response.write(1234);
}

View file

@ -14,8 +14,7 @@ XPCOMUtils.defineLazyGetter(this, "PORT", function() {
var srv;
function run_test()
{
function run_test() {
srv = createServer();
srv.registerPathHandler("/raw-data", handleRawData);
@ -30,14 +29,10 @@ function run_test()
}
function checkException(fun, err, msg)
{
try
{
function checkException(fun, err, msg) {
try {
fun();
}
catch (e)
{
} catch (e) {
if (e !== err && e.result !== err)
do_throw(msg);
return;
@ -45,30 +40,26 @@ function checkException(fun, err, msg)
do_throw(msg);
}
function callASAPLater(fun)
{
function callASAPLater(fun) {
gThreadManager.dispatchToMainThread({
run: function()
{
run() {
fun();
}
},
});
}
/*****************
/** ***************
* PATH HANDLERS *
*****************/
function handleRawData(request, response)
{
function handleRawData(request, response) {
response.seizePower();
response.write("Raw data!");
response.finish();
}
function handleTooLate(request, response)
{
function handleTooLate(request, response) {
response.write("DO NOT WANT");
var output = response.bodyOutputStream;
@ -81,8 +72,7 @@ function handleTooLate(request, response)
response.finish();
}
function handleExceptions(request, response)
{
function handleExceptions(request, response) {
response.seizePower();
checkException(function() { response.setStatusLine("1.0", 500, "ISE"); },
Cr.NS_ERROR_NOT_AVAILABLE,
@ -104,35 +94,30 @@ function handleExceptions(request, response)
"seizePower should throw unexpected after finish");
}
function handleAsyncSeizure(request, response)
{
function handleAsyncSeizure(request, response) {
response.seizePower();
callLater(1, function()
{
callLater(1, function() {
response.write("async seizure passed");
response.bodyOutputStream.close();
callLater(1, function()
{
callLater(1, function() {
response.finish();
});
});
}
function handleSeizeAfterAsync(request, response)
{
function handleSeizeAfterAsync(request, response) {
response.setStatusLine(request.httpVersion, 200, "async seizure pass");
response.processAsync();
checkException(function() { response.seizePower(); },
Cr.NS_ERROR_NOT_AVAILABLE,
"seizePower should throw not-available after processAsync");
callLater(1, function()
{
callLater(1, function() {
response.finish();
});
}
/***************
/** *************
* BEGIN TESTS *
***************/
@ -148,35 +133,30 @@ XPCOMUtils.defineLazyGetter(this, "tests", function() {
var data0 = "GET /raw-data HTTP/1.0\r\n" +
"\r\n";
function checkRawData(data)
{
function checkRawData(data) {
Assert.equal(data, "Raw data!");
}
var data1 = "GET /called-too-late HTTP/1.0\r\n" +
"\r\n";
function checkTooLate(data)
{
function checkTooLate(data) {
Assert.equal(LineIterator(data).next().value, "too-late passed");
}
var data2 = "GET /exceptions HTTP/1.0\r\n" +
"\r\n";
function checkExceptions(data)
{
function checkExceptions(data) {
Assert.equal("exceptions test passed", data);
}
var data3 = "GET /async-seizure HTTP/1.0\r\n" +
"\r\n";
function checkAsyncSeizure(data)
{
function checkAsyncSeizure(data) {
Assert.equal(data, "async seizure passed");
}
var data4 = "GET /seize-after-async HTTP/1.0\r\n" +
"\r\n";
function checkSeizeAfterAsync(data)
{
function checkSeizeAfterAsync(data) {
Assert.equal(LineIterator(data).next().value, "HTTP/1.0 200 async seizure pass");
}

View file

@ -8,8 +8,7 @@
var srv, serverBasePath;
function run_test()
{
function run_test() {
srv = createServer();
serverBasePath = do_get_profile();
srv.registerDirectory("/", serverBasePath);
@ -26,38 +25,32 @@ XPCOMUtils.defineLazyGetter(this, "URL", function() {
XPCOMUtils.defineLazyGetter(this, "tests", function() {
return [
new Test(URL, init, startCustomIndexHandler, stopCustomIndexHandler),
new Test(URL, init, startDefaultIndexHandler, stopDefaultIndexHandler)
new Test(URL, init, startDefaultIndexHandler, stopDefaultIndexHandler),
];
});
function init(ch)
{
function init(ch) {
ch.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE; // important!
}
function startCustomIndexHandler(ch, cx)
{
function startCustomIndexHandler(ch, cx) {
Assert.equal(ch.getResponseHeader("Content-Length"), "10");
srv.setIndexHandler(null);
}
function stopCustomIndexHandler(ch, cx, status, data)
{
function stopCustomIndexHandler(ch, cx, status, data) {
Assert.ok(Components.isSuccessCode(status));
Assert.equal(String.fromCharCode.apply(null, data), "directory!");
}
function startDefaultIndexHandler(ch, cx)
{
function startDefaultIndexHandler(ch, cx) {
Assert.equal(ch.responseStatus, 200);
}
function stopDefaultIndexHandler(ch, cx, status, data)
{
function stopDefaultIndexHandler(ch, cx, status, data) {
Assert.ok(Components.isSuccessCode(status));
}
// PATH HANDLERS
function myIndexHandler(metadata, response)
{
function myIndexHandler(metadata, response) {
var dir = metadata.getProperty("directory");
Assert.ok(dir != null);
Assert.ok(dir instanceof Ci.nsIFile);

View file

@ -13,8 +13,7 @@ XPCOMUtils.defineLazyGetter(this, "URL", function() {
var srv;
function run_test()
{
function run_test() {
srv = createServer();
srv.registerPathHandler("/no/setstatusline", noSetstatusline);
@ -32,12 +31,11 @@ function run_test()
}
/*************
/** ***********
* UTILITIES *
*************/
function checkStatusLine(channel, httpMaxVer, httpMinVer, httpCode, statusText)
{
function checkStatusLine(channel, httpMaxVer, httpMinVer, httpCode, statusText) {
Assert.equal(channel.responseStatus, httpCode);
Assert.equal(channel.responseStatusText, statusText);
@ -48,7 +46,7 @@ function checkStatusLine(channel, httpMaxVer, httpMinVer, httpCode, statusText)
}
/*********
/** *******
* TESTS *
*********/
@ -61,112 +59,88 @@ XPCOMUtils.defineLazyGetter(this, "tests", function() {
new Test(URL + "/invalidStatus", null, startPassedTrue, stop),
new Test(URL + "/invalidDescription", null, startPassedTrue, stop),
new Test(URL + "/crazyCode", null, startCrazy, stop),
new Test(URL + "/nullVersion", null, startNullVersion, stop)
new Test(URL + "/nullVersion", null, startNullVersion, stop),
];
});
// /no/setstatusline
function noSetstatusline(metadata, response)
{
function noSetstatusline(metadata, response) {
}
function startNoSetStatusLine(ch, cx)
{
function startNoSetStatusLine(ch, cx) {
checkStatusLine(ch, 1, 1, 200, "OK");
}
function stop(ch, cx, status, data)
{
function stop(ch, cx, status, data) {
Assert.ok(Components.isSuccessCode(status));
}
// /http1_0
function http1_0(metadata, response)
{
function http1_0(metadata, response) {
response.setStatusLine("1.0", 200, "OK");
}
function startHttp1_0(ch, cx)
{
function startHttp1_0(ch, cx) {
checkStatusLine(ch, 1, 0, 200, "OK");
}
// /http1_1
function http1_1(metadata, response)
{
function http1_1(metadata, response) {
response.setStatusLine("1.1", 200, "OK");
}
function startHttp1_1(ch, cx)
{
function startHttp1_1(ch, cx) {
checkStatusLine(ch, 1, 1, 200, "OK");
}
// /invalidVersion
function invalidVersion(metadata, response)
{
try
{
function invalidVersion(metadata, response) {
try {
response.setStatusLine(" 1.0", 200, "FAILED");
}
catch (e)
{
} catch (e) {
response.setHeader("Passed", "true", false);
}
}
function startPassedTrue(ch, cx)
{
function startPassedTrue(ch, cx) {
checkStatusLine(ch, 1, 1, 200, "OK");
Assert.equal(ch.getResponseHeader("Passed"), "true");
}
// /invalidStatus
function invalidStatus(metadata, response)
{
try
{
function invalidStatus(metadata, response) {
try {
response.setStatusLine("1.0", 1000, "FAILED");
}
catch (e)
{
} catch (e) {
response.setHeader("Passed", "true", false);
}
}
// /invalidDescription
function invalidDescription(metadata, response)
{
try
{
function invalidDescription(metadata, response) {
try {
response.setStatusLine("1.0", 200, "FAILED\x01");
}
catch (e)
{
} catch (e) {
response.setHeader("Passed", "true", false);
}
}
// /crazyCode
function crazyCode(metadata, response)
{
function crazyCode(metadata, response) {
response.setStatusLine("1.1", 617, "Crazy");
}
function startCrazy(ch, cx)
{
function startCrazy(ch, cx) {
checkStatusLine(ch, 1, 1, 617, "Crazy");
}
// /nullVersion
function nullVersion(metadata, response)
{
function nullVersion(metadata, response) {
response.setStatusLine(null, 255, "NULL");
}
function startNullVersion(ch, cx)
{
function startNullVersion(ch, cx) {
// currently, this server implementation defaults to 1.1
checkStatusLine(ch, 1, 1, 255, "NULL");
}

View file

@ -21,22 +21,20 @@ var test;
var tests = [];
/*********************
/** *******************
* UTILITY FUNCTIONS *
*********************/
function bytesToString(bytes)
{
function bytesToString(bytes) {
return bytes.map(function(v) { return String.fromCharCode(v); }).join("");
}
function skipCache(ch)
{
function skipCache(ch) {
ch.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE;
}
/********************
/** ******************
* DEFINE THE TESTS *
********************/
@ -45,20 +43,17 @@ function skipCache(ch)
* SJS which throws an exception and creates a server error and tests with a
* normal, successful SJS.
*/
function setupTests(throwing)
{
function setupTests(throwing) {
const TEST_URL = BASE + "/cgi.sjs" + (throwing ? "?throw" : "");
// registerFile with SJS => raw text
function setupFile(ch)
{
function setupFile(ch) {
srv.registerFile("/cgi.sjs", sjs);
skipCache(ch);
}
function verifyRawText(channel, cx, status, bytes)
{
function verifyRawText(channel, cx, status, bytes) {
dumpn(channel.originalURI.spec);
Assert.equal(bytesToString(bytes), fileContents(sjs));
}
@ -69,27 +64,21 @@ function setupTests(throwing)
// add mapping, => interpreted
function addTypeMapping(ch)
{
function addTypeMapping(ch) {
srv.registerContentType("sjs", "sjs");
skipCache(ch);
}
function checkType(ch, cx)
{
if (throwing)
{
function checkType(ch, cx) {
if (throwing) {
Assert.ok(!ch.requestSucceeded);
Assert.equal(ch.responseStatus, 500);
}
else
{
} else {
Assert.equal(ch.contentType, "text/plain");
}
}
function checkContents(ch, cx, status, data)
{
function checkContents(ch, cx, status, data) {
if (!throwing)
Assert.equal("PASS", bytesToString(data));
}
@ -100,8 +89,7 @@ function setupTests(throwing)
// remove file/type mapping, map containing directory => raw text
function setupDirectoryAndRemoveType(ch)
{
function setupDirectoryAndRemoveType(ch) {
dumpn("removing type mapping");
srv.registerContentType("sjs", null);
srv.registerFile("/cgi.sjs", null);
@ -114,9 +102,8 @@ function setupTests(throwing)
// add mapping, => interpreted
function contentAndCleanup(ch, cx, status, data)
{
function contentAndCleanup(ch, cx, status, data) {
checkContents(ch, cx, status, data);
// clean up state we've set up
@ -133,7 +120,7 @@ function setupTests(throwing)
}
/*****************
/** ***************
* ADD THE TESTS *
*****************/
@ -144,16 +131,14 @@ setupTests(false);
// treated as an extension -- there must be at least one dot for a filename to
// match an extension.
function init(ch)
{
function init(ch) {
// clean up state we've set up
srv.registerDirectory("/", sjs.parent);
srv.registerContentType("sjs", "sjs");
skipCache(ch);
}
function checkNotSJS(ch, cx, status, data)
{
function checkNotSJS(ch, cx, status, data) {
Assert.notEqual("FAIL", bytesToString(data));
}
@ -163,23 +148,17 @@ tests.push(test);
// Test that Range requests are passed through to the SJS file without
// bounds checking.
function rangeInit(expectedRangeHeader)
{
return function setupRangeRequest(ch)
{
function rangeInit(expectedRangeHeader) {
return function setupRangeRequest(ch) {
ch.setRequestHeader("Range", expectedRangeHeader, false);
};
}
function checkRangeResult(ch, cx)
{
try
{
function checkRangeResult(ch, cx) {
try {
var val = ch.getResponseHeader("Content-Range");
}
catch (e) { /* IDL doesn't specify a particular exception to require */ }
if (val !== undefined)
{
} catch (e) { /* IDL doesn't specify a particular exception to require */ }
if (val !== undefined) {
do_throw("should not have gotten a Content-Range header, but got one " +
"with this value: " + val);
}
@ -212,18 +191,15 @@ tests.push(test);
// extension of the file on the server, not by the extension of the requested
// path.
function setupFileMapping(ch)
{
function setupFileMapping(ch) {
srv.registerFile("/script.html", sjs);
}
function onStart(ch, cx)
{
function onStart(ch, cx) {
Assert.equal(ch.contentType, "text/plain");
}
function onStop(ch, cx, status, data)
{
function onStop(ch, cx, status, data) {
Assert.equal("PASS", bytesToString(data));
}
@ -231,20 +207,16 @@ test = new Test(BASE + "/script.html", setupFileMapping, onStart, onStop);
tests.push(test);
/*****************
/** ***************
* RUN THE TESTS *
*****************/
function run_test()
{
function run_test() {
// Test for a content-type which isn't a field-value
try
{
try {
srv.registerContentType("foo", "bar\nbaz");
throw "this server throws on content-types which aren't field-values";
}
catch (e)
{
} catch (e) {
isException(e, Cr.NS_ERROR_INVALID_ARG);
}
runHttpTests(tests, testComplete(srv));

View file

@ -15,8 +15,7 @@ XPCOMUtils.defineLazyGetter(this, "PATH", function() {
var srv;
function run_test()
{
function run_test() {
srv = createServer();
var sjsDir = do_get_file("data/sjs/");
srv.registerDirectory("/", sjsDir);
@ -28,7 +27,7 @@ function run_test()
new HTTPTestLoader(PATH + "?state=initial", initialStart, initialStop);
}
/********************
/** ******************
* OBSERVER METHODS *
********************/
@ -58,8 +57,7 @@ function run_test()
*/
var initialStarted = false;
function initialStart(ch, cx)
{
function initialStart(ch, cx) {
dumpn("*** initialStart");
if (initialStarted)
@ -72,8 +70,7 @@ function initialStart(ch, cx)
}
var initialStopped = false;
function initialStop(ch, cx, status, data)
{
function initialStop(ch, cx, status, data) {
dumpn("*** initialStop");
Assert.equal(data.map(function(v) { return String.fromCharCode(v); }).join(""),
@ -96,8 +93,7 @@ function initialStop(ch, cx, status, data)
}
var intermediateStarted = false;
function intermediateStart(ch, cx)
{
function intermediateStart(ch, cx) {
dumpn("*** intermediateStart");
Assert.notEqual(srv.getObjectState("object-state-test"), null);
@ -111,8 +107,7 @@ function intermediateStart(ch, cx)
}
var intermediateStopped = false;
function intermediateStop(ch, cx, status, data)
{
function intermediateStop(ch, cx, status, data) {
dumpn("*** intermediateStop");
Assert.equal(data.map(function(v) { return String.fromCharCode(v); }).join(""),
@ -134,8 +129,7 @@ function intermediateStop(ch, cx, status, data)
}
var triggerStarted = false;
function triggerStart(ch, cx)
{
function triggerStart(ch, cx) {
dumpn("*** triggerStart");
if (!initialStarted)
@ -151,8 +145,7 @@ function triggerStart(ch, cx)
}
var triggerStopped = false;
function triggerStop(ch, cx, status, data)
{
function triggerStop(ch, cx, status, data) {
dumpn("*** triggerStop");
Assert.equal(data.map(function(v) { return String.fromCharCode(v); }).join(""),
@ -175,25 +168,18 @@ function triggerStop(ch, cx, status, data)
}
var finished = false;
function checkForFinish()
{
if (finished)
{
try
{
function checkForFinish() {
if (finished) {
try {
do_throw("uh-oh, how are we being finished twice?!?!");
}
finally
{
} finally {
quit(1);
}
}
if (triggerStopped && initialStopped)
{
if (triggerStopped && initialStopped) {
finished = true;
try
{
try {
Assert.equal(srv.getObjectState("object-state-test"), null);
if (!initialStarted)
@ -204,22 +190,19 @@ function checkForFinish()
do_throw("checkForFinish: intermediateStopped is false?!?!");
if (!triggerStarted)
do_throw("checkForFinish: triggerStarted is false?!?!");
}
finally
{
} finally {
srv.stop(do_test_finished);
}
}
}
/*********************************
/** *******************************
* UTILITY OBSERVABLE URL LOADER *
*********************************/
/** Stream listener for the channels. */
function HTTPTestLoader(path, start, stop)
{
function HTTPTestLoader(path, start, stop) {
/** Path to load. */
this._path = path;
@ -237,39 +220,30 @@ function HTTPTestLoader(path, start, stop)
}
HTTPTestLoader.prototype =
{
onStartRequest: function(request, cx)
{
onStartRequest(request, cx) {
dumpn("*** HTTPTestLoader.onStartRequest for " + this._path);
var ch = request.QueryInterface(Ci.nsIHttpChannel)
.QueryInterface(Ci.nsIHttpChannelInternal);
try
{
try
{
try {
try {
this._start(ch, cx);
}
catch (e)
{
} catch (e) {
do_throw(this._path + ": error in onStartRequest: " + e);
}
}
catch (e)
{
} catch (e) {
dumpn("!!! swallowing onStartRequest exception so onStopRequest is " +
"called...");
}
},
onDataAvailable: function(request, cx, inputStream, offset, count)
{
onDataAvailable(request, cx, inputStream, offset, count) {
dumpn("*** HTTPTestLoader.onDataAvailable for " + this._path);
Array.prototype.push.apply(this._data,
makeBIS(inputStream).readByteArray(count));
},
onStopRequest: function(request, cx, status)
{
onStopRequest(request, cx, status) {
dumpn("*** HTTPTestLoader.onStopRequest for " + this._path);
var ch = request.QueryInterface(Ci.nsIHttpChannel)
@ -277,14 +251,5 @@ HTTPTestLoader.prototype =
this._stop(ch, cx, status, this._data);
},
QueryInterface: function(aIID)
{
dumpn("*** QueryInterface: " + aIID);
if (aIID.equals(Ci.nsIStreamListener) ||
aIID.equals(Ci.nsIRequestObserver) ||
aIID.equals(Ci.nsISupports))
return this;
throw Cr.NS_ERROR_NO_INTERFACE;
}
QueryInterface: ChromeUtils.generateQI(["nsIStreamListener", "nsIRequestObserver"]),
};

View file

@ -12,8 +12,7 @@ XPCOMUtils.defineLazyGetter(this, "URL", function() {
var srv;
function run_test()
{
function run_test() {
srv = createServer();
var sjsDir = do_get_file("data/sjs/");
srv.registerDirectory("/", sjsDir);
@ -21,8 +20,7 @@ function run_test()
srv.registerPathHandler("/path-handler", pathHandler);
srv.start(-1);
function done()
{
function done() {
Assert.equal(srv.getSharedState("shared-value"), "done!");
Assert.equal(srv.getState("/path-handler", "private-value"),
"pathHandlerPrivate2");
@ -38,14 +36,13 @@ function run_test()
}
/************
/** **********
* HANDLERS *
************/
var firstTime = true;
function pathHandler(request, response)
{
function pathHandler(request, response) {
response.setHeader("Cache-Control", "no-cache", false);
response.setHeader("X-Old-Shared-Value", srv.getSharedState("shared-value"),
@ -54,14 +51,11 @@ function pathHandler(request, response)
false);
var privateValue, sharedValue;
if (firstTime)
{
if (firstTime) {
firstTime = false;
privateValue = "pathHandlerPrivate";
sharedValue = "pathHandlerShared";
}
else
{
} else {
privateValue = "pathHandlerPrivate2";
sharedValue = "";
}
@ -74,7 +68,7 @@ function pathHandler(request, response)
}
/***************
/** *************
* BEGIN TESTS *
***************/
@ -104,21 +98,16 @@ XPCOMUtils.defineLazyGetter(this, "tests", function() {
null, start_other_set_new, null),
new Test(URL + "/state1.sjs?" +
"newShared=done!&newPrivate=",
null, start_set_remove_original, null)
null, start_set_remove_original, null),
];
});
/* Hack around bug 474845 for now. */
function getHeaderFunction(ch)
{
function getHeader(name)
{
try
{
function getHeaderFunction(ch) {
function getHeader(name) {
try {
return ch.getResponseHeader(name);
}
catch (e)
{
} catch (e) {
if (e.result !== Cr.NS_ERROR_NOT_AVAILABLE)
throw e;
}
@ -127,8 +116,7 @@ function getHeaderFunction(ch)
return getHeader;
}
function expectValues(ch, oldShared, newShared, oldPrivate, newPrivate)
{
function expectValues(ch, oldShared, newShared, oldPrivate, newPrivate) {
var getHeader = getHeaderFunction(ch);
Assert.equal(ch.responseStatus, 200);
@ -138,49 +126,40 @@ function expectValues(ch, oldShared, newShared, oldPrivate, newPrivate)
Assert.equal(getHeader("X-New-Private-Value"), newPrivate);
}
function start_initial(ch, cx)
{
function start_initial(ch, cx) {
dumpn("XXX start_initial");
expectValues(ch, "", "newShared", "", "newPrivate");
}
function start_overwrite(ch, cx)
{
function start_overwrite(ch, cx) {
expectValues(ch, "newShared", "newShared2", "newPrivate", "newPrivate2");
}
function start_remove(ch, cx)
{
function start_remove(ch, cx) {
expectValues(ch, "newShared2", "", "newPrivate2", "newPrivate3");
}
function start_handler(ch, cx)
{
function start_handler(ch, cx) {
expectValues(ch, "", "pathHandlerShared", "", "pathHandlerPrivate");
}
function start_handler_again(ch, cx)
{
function start_handler_again(ch, cx) {
expectValues(ch, "pathHandlerShared", "",
"pathHandlerPrivate", "pathHandlerPrivate2");
}
function start_other_initial(ch, cx)
{
function start_other_initial(ch, cx) {
expectValues(ch, "", "newShared4", "", "newPrivate4");
}
function start_other_remove_ignore(ch, cx)
{
function start_other_remove_ignore(ch, cx) {
expectValues(ch, "newShared4", "", "newPrivate4", "");
}
function start_other_set_new(ch, cx)
{
function start_other_set_new(ch, cx) {
expectValues(ch, "", "newShared5", "newPrivate4", "newPrivate5");
}
function start_set_remove_original(ch, cx)
{
function start_set_remove_original(ch, cx) {
expectValues(ch, "newShared5", "done!", "newPrivate3", "");
}

View file

@ -16,16 +16,14 @@ XPCOMUtils.defineLazyGetter(this, "URL", function() {
var srv;
function run_test()
{
function run_test() {
srv = createServer();
var sjsDir = do_get_file("data/sjs/");
srv.registerDirectory("/", sjsDir);
srv.registerContentType("sjs", "sjs");
srv.start(-1);
function done()
{
function done() {
do_test_pending();
srv.stop(function() { do_test_finished(); });
Assert.equal(gStartCount, TEST_RUNS);
@ -35,7 +33,7 @@ function run_test()
runHttpTests(tests, done);
}
/***************
/** *************
* BEGIN TESTS *
***************/
@ -55,16 +53,14 @@ XPCOMUtils.defineLazyGetter(this, "tests", function() {
return _tests;
});
function start_thrower(ch, cx)
{
function start_thrower(ch, cx) {
Assert.equal(ch.responseStatus, 500);
Assert.ok(!ch.requestSucceeded);
gStartCount++;
}
function start_last(ch, cx)
{
function start_last(ch, cx) {
Assert.equal(ch.responseStatus, 200);
Assert.ok(ch.requestSucceeded);

View file

@ -18,10 +18,8 @@ XPCOMUtils.defineLazyGetter(this, "PREPATH", function() {
var srv, srv2;
function run_test()
{
if (mozinfo.os == "win")
{
function run_test() {
if (mozinfo.os == "win") {
dumpn("*** not running test_start_stop.js on Windows for now, because " +
"Windows is dumb");
return;
@ -32,33 +30,25 @@ function run_test()
srv = createServer();
srv.start(-1);
try
{
try {
srv.start(PORT);
do_throw("starting a started server");
}
catch (e)
{
} catch (e) {
isException(e, Cr.NS_ERROR_ALREADY_INITIALIZED);
}
do_test_pending();
srv.stop(function()
{
try
{
srv.stop(function() {
try {
do_test_pending();
run_test_2();
}
finally
{
} finally {
do_test_finished();
}
});
}
function run_test_2()
{
function run_test_2() {
dumpn("*** run_test_2");
do_test_finished();
@ -66,41 +56,30 @@ function run_test_2()
srv.start(PORT);
srv2 = createServer();
try
{
try {
srv2.start(PORT);
do_throw("two servers on one port?");
}
catch (e)
{
} catch (e) {
isException(e, Cr.NS_ERROR_NOT_AVAILABLE);
}
do_test_pending();
try
{
srv.stop({onStopped: function()
{
try
{
try {
srv.stop({onStopped() {
try {
do_test_pending();
run_test_3();
}
finally
{
} finally {
do_test_finished();
}
}
},
});
}
catch (e)
{
} catch (e) {
do_throw("error stopping with an object: " + e);
}
}
function run_test_3()
{
function run_test_3() {
dumpn("*** run_test_3");
do_test_finished();
@ -108,10 +87,8 @@ function run_test_3()
srv.start(PORT);
do_test_pending();
try
{
srv.stop().then(function()
{
try {
srv.stop().then(function() {
try {
do_test_pending();
run_test_4();
@ -119,15 +96,12 @@ function run_test_3()
do_test_finished();
}
});
}
catch (e)
{
} catch (e) {
do_throw("error stopping with an object: " + e);
}
}
function run_test_4()
{
function run_test_4() {
dumpn("*** run_test_4");
do_test_finished();
@ -145,8 +119,7 @@ function run_test_4()
var testsComplete = false;
function run_test_5()
{
function run_test_5() {
dumpn("*** run_test_5");
testsComplete = true;
@ -157,29 +130,23 @@ function run_test_5()
const INTERVAL = 500;
function handle(request, response)
{
function handle(request, response) {
response.processAsync();
dumpn("*** stopping server...");
srv.stop(serverStopped);
callLater(INTERVAL, function()
{
callLater(INTERVAL, function() {
Assert.ok(!stopped);
callLater(INTERVAL, function()
{
callLater(INTERVAL, function() {
Assert.ok(!stopped);
response.finish();
try
{
try {
response.processAsync();
do_throw("late processAsync didn't throw?");
}
catch (e)
{
} catch (e) {
isException(e, Cr.NS_ERROR_UNEXPECTED);
}
});
@ -187,8 +154,7 @@ function handle(request, response)
}
var stopped = false;
function serverStopped()
{
function serverStopped() {
dumpn("*** server really, fully shut down now");
stopped = true;
if (testsComplete)