Bug 381598 - HTTP server tests cleanup, round two. r=me because the old tests are difficult to understand and it's not worth making someone read them to do a review

This commit is contained in:
Jeff Walden 2008-12-18 11:05:40 -08:00
parent 5dfe4f0055
commit 6ff11cc4e7
3 changed files with 231 additions and 297 deletions

View File

@ -40,13 +40,74 @@
// escaping checks -- highly dependent on the default index handler output
// format
var paths =
[
"http://localhost:4444/", // check top-level directory listing
"http://localhost:4444/foo/", // check non-top-level, too
"http://localhost:4444/bar/folder^/" // trailing-caret leaf with hidden files
];
var currPathIndex = 0;
var srv, dir, dirEntries;
function run_test()
{
createTestDirectory();
srv = createServer();
srv.registerDirectory("/", dir);
var nameDir = do_get_file("netwerk/test/httpserver/test/data/name-scheme/");
srv.registerDirectory("/bar/", nameDir);
srv.start(4444);
runHttpTests(tests, function() { srv.stop(); destroyTestDirectory(); });
}
function createTestDirectory()
{
dir = Cc["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIProperties)
.get("TmpD", Ci.nsIFile);
dir.append("index_handler_test_" + Math.random());
dir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0744);
// populate with test directories, files, etc.
// Files must be in expected order of display on the index page!
files = [];
makeFile("aa_directory", true, dir, files);
makeFile("Ba_directory", true, dir, files);
makeFile("bb_directory", true, dir, files);
makeFile("foo", true, dir, files);
makeFile("a_file", false, dir, files);
makeFile("B_file", false, dir, files);
makeFile("za'z", false, dir, files);
makeFile("zb&z", false, dir, files);
makeFile("zc<q", false, dir, files);
makeFile('zd"q', false, dir, files);
makeFile("ze%g", false, dir, files);
makeFile("zf%200h", false, dir, files);
makeFile("zg>m", false, dir, files);
dirEntries = [files];
var subdir = dir.clone();
subdir.append("foo");
files = [];
makeFile("aa_dir", true, subdir, files);
makeFile("b_dir", true, subdir, files);
makeFile("AA_file.txt", false, subdir, files);
makeFile("test.txt", false, subdir, files);
dirEntries.push(files);
}
function destroyTestDirectory()
{
dir.remove(true);
}
/*************
* UTILITIES *
*************/
/** Verifies data in bytes for the trailing-caret path above. */
function hiddenDataCheck(bytes, uri, path)
@ -116,7 +177,6 @@ function hiddenDataCheck(bytes, uri, path)
}
}
/**
* Verifies data in bytes (an array of bytes) represents an index page for the
* given URI and path, which should be a page listing the given directory
@ -177,7 +237,7 @@ function dataCheck(bytes, uri, path, dirEntries)
var ios = Cc["@mozilla.org/network/io-service;1"]
.getService(Ci.nsIIOService);
var top = ios.newURI(uri, null, null);
var dirURI = ios.newURI(uri, null, null);
for (var i = 0; i < items.length; i++)
{
@ -197,130 +257,6 @@ function dataCheck(bytes, uri, path, dirEntries)
}
}
var listener =
{
// data from request
_data: [],
onStartRequest: function(request, cx)
{
var ch = request.QueryInterface(Ci.nsIHttpChannel);
do_check_eq(ch.getResponseHeader("Content-Type"), "text/html");
this._data.length = 0;
this._uri = paths[currPathIndex];
this._path = this._uri.substring(paths[0].length - 1);
this._dirEntries = dirEntries[currPathIndex];
},
onDataAvailable: function(request, cx, inputStream, offset, count)
{
var bytes = makeBIS(inputStream).readByteArray(count);
Array.prototype.push.apply(this._data, bytes);
},
onStopRequest: function(request, cx, status)
{
if (currPathIndex == 2)
hiddenDataCheck(this._data, this._uri, this._path);
else
dataCheck(this._data, this._uri, this._path, this._dirEntries);
if (++currPathIndex == paths.length)
{
srv.stop();
destroyTestDirectory();
}
else
{
performNextTest();
}
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;
}
};
var srv, dir, dirEntries;
function run_test()
{
createTestDirectory();
srv = createServer();
srv.registerDirectory("/", dir);
var nameDir = do_get_file("netwerk/test/httpserver/test/data/name-scheme/");
srv.registerDirectory("/bar/", nameDir);
srv.start(4444);
performNextTest();
}
function performNextTest()
{
do_test_pending();
var ch = makeChannel(paths[currPathIndex]);
ch.asyncOpen(listener, null);
}
function createTestDirectory()
{
dir = Cc["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIProperties)
.get("TmpD", Ci.nsIFile);
dir.append("index_handler_test_" + Math.random());
dir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0744);
// populate with test directories, files, etc.
// Files must be in expected order of display on the index page!
files = [];
makeFile("aa_directory", true, dir, files);
makeFile("Ba_directory", true, dir, files);
makeFile("bb_directory", true, dir, files);
makeFile("foo", true, dir, files);
makeFile("a_file", false, dir, files);
makeFile("B_file", false, dir, files);
makeFile("za'z", false, dir, files);
makeFile("zb&z", false, dir, files);
makeFile("zc<q", false, dir, files);
makeFile('zd"q', false, dir, files);
makeFile("ze%g", false, dir, files);
makeFile("zf%200h", false, dir, files);
makeFile("zg>m", false, dir, files);
dirEntries = [files];
var subdir = dir.clone();
subdir.append("foo");
files = [];
makeFile("aa_dir", true, subdir, files);
makeFile("b_dir", true, subdir, files);
makeFile("AA_file.txt", false, subdir, files);
makeFile("test.txt", false, subdir, files);
dirEntries.push(files);
}
function destroyTestDirectory()
{
dir.remove(true);
}
// utilities
/**
* Create a file/directory with the given name underneath parentDir, and
* append an object with name/isDirectory properties to lst corresponding
@ -328,10 +264,7 @@ function destroyTestDirectory()
*/
function makeFile(name, isDirectory, parentDir, lst)
{
var type = isDirectory
? Ci.nsIFile.DIRECTORY_TYPE
: Ci.nsIFile.NORMAL_FILE_TYPE;
var type = Ci.nsIFile[isDirectory ? "DIRECTORY_TYPE" : "NORMAL_FILE_TYPE"];
var file = parentDir.clone();
try
@ -342,3 +275,43 @@ function makeFile(name, isDirectory, parentDir, lst)
}
catch (e) { /* OS probably doesn't like file name, skip */ }
}
/*********
* TESTS *
*********/
var tests = [];
var test;
// check top-level directory listing
test = new Test("http://localhost:4444/",
null, start, stopRootDirectory),
tests.push(test);
function start(ch)
{
do_check_eq(ch.getResponseHeader("Content-Type"), "text/html");
}
function stopRootDirectory(ch, cx, status, data)
{
dataCheck(data, "http://localhost:4444/", "/", dirEntries[0]);
}
// check non-top-level, too
test = new Test("http://localhost:4444/foo/",
null, start, stopFooDirectory),
tests.push(test);
function stopFooDirectory(ch, cx, status, data)
{
dataCheck(data, "http://localhost:4444/foo/", "/foo/", dirEntries[1]);
}
// trailing-caret leaf with hidden files
test = new Test("http://localhost:4444/bar/folder^/",
null, start, stopTrailingCaretDirectory),
tests.push(test);
function stopTrailingCaretDirectory(ch, cx, status, data)
{
hiddenDataCheck(data, "http://localhost:4444/bar/folder^/", "/bar/folder^/");
}

View File

@ -38,67 +38,6 @@
// Make sure setIndexHandler works as expected
var paths =
[
"http://localhost:4444/",
"http://localhost:4444/"
];
var currPathIndex = 0;
var listener =
{
// NSISTREAMLISTENER
onDataAvailable: function(request, cx, inputStream, offset, count)
{
makeBIS(inputStream).readByteArray(count); // required by API
},
// NSIREQUESTOBSERVER
onStartRequest: function(request, cx)
{
var ch = request.QueryInterface(Ci.nsIHttpChannel)
.QueryInterface(Ci.nsIHttpChannelInternal);
switch (currPathIndex)
{
case 0:
do_check_eq(ch.getResponseHeader("Content-Length"), "10");
srv.setIndexHandler(null);
break;
case 1:
do_check_eq(ch.responseStatus, 200); // default index handler
break;
}
},
onStopRequest: function(request, cx, status)
{
do_check_true(Components.isSuccessCode(status));
if (++currPathIndex == paths.length)
srv.stop();
else
performNextTest();
do_test_finished();
},
// NSISUPPORTS
QueryInterface: function(aIID)
{
if (aIID.equals(Ci.nsIStreamListener) ||
aIID.equals(Ci.nsIRequestObserver) ||
aIID.equals(Ci.nsISupports))
return this;
throw Cr.NS_ERROR_NO_INTERFACE;
}
};
function performNextTest()
{
do_test_pending();
var ch = makeChannel(paths[currPathIndex]);
ch.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE; // important!
ch.asyncOpen(listener, null);
}
var srv, serverBasePath;
function run_test()
@ -109,7 +48,41 @@ function run_test()
srv.setIndexHandler(myIndexHandler);
srv.start(4444);
performNextTest();
runHttpTests(tests, function() { srv.stop(); });
}
var tests = [];
var test;
test = new Test("http://localhost:4444/",
init, startCustomIndexHandler, stopCustomIndexHandler);
tests.push(test);
function init(ch)
{
ch.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE; // important!
}
function startCustomIndexHandler(ch, cx)
{
do_check_eq(ch.getResponseHeader("Content-Length"), "10");
srv.setIndexHandler(null);
}
function stopCustomIndexHandler(ch, cx, status, data)
{
do_check_true(Components.isSuccessCode(status));
do_check_eq(String.fromCharCode.apply(null, data), "directory!");
}
test = new Test("http://localhost:4444/",
init, startDefaultIndexHandler, stopDefaultIndexHandler);
tests.push(test);
function startDefaultIndexHandler(ch, cx)
{
do_check_eq(ch.responseStatus, 200);
}
function stopDefaultIndexHandler(ch, cx, status, data)
{
do_check_true(Components.isSuccessCode(status));
}
// PATH HANDLERS

View File

@ -39,102 +39,6 @@
// exercise nsIHttpResponse.setStatusLine, ensure its atomicity, and ensure the
// specified behavior occurs if it's not called
var paths =
[
"http://localhost:4444/no/setstatusline",
"http://localhost:4444/http1_0",
"http://localhost:4444/http1_1",
"http://localhost:4444/invalidVersion",
"http://localhost:4444/invalidStatus",
"http://localhost:4444/invalidDescription",
"http://localhost:4444/crazyCode",
"http://localhost:4444/nullVersion"
];
var currPathIndex = 0;
var listener =
{
// NSISTREAMLISTENER
onDataAvailable: function(request, cx, inputStream, offset, count)
{
makeBIS(inputStream).readByteArray(count); // required by API
},
// NSIREQUESTOBSERVER
onStartRequest: function(request, cx)
{
var ch = request.QueryInterface(Ci.nsIHttpChannel)
.QueryInterface(Ci.nsIHttpChannelInternal);
switch (currPathIndex)
{
case 0:
checkStatusLine(ch, 1, 1, 200, "OK");
break;
case 1:
checkStatusLine(ch, 1, 0, 200, "OK");
break;
case 2:
checkStatusLine(ch, 1, 1, 200, "OK");
break;
case 3:
case 4:
case 5:
checkStatusLine(ch, 1, 1, 200, "OK");
do_check_eq(ch.getResponseHeader("Passed"), "true");
break;
case 6:
checkStatusLine(ch, 1, 1, 617, "Crazy");
break;
case 7:
// currently, this server implementation defaults to 1.1
checkStatusLine(ch, 1, 1, 255, "NULL");
break;
}
},
onStopRequest: function(request, cx, status)
{
do_check_true(Components.isSuccessCode(status));
if (++currPathIndex == paths.length)
srv.stop();
else
performNextTest();
do_test_finished();
},
// NSISUPPORTS
QueryInterface: function(aIID)
{
if (aIID.equals(Ci.nsIStreamListener) ||
aIID.equals(Ci.nsIRequestObserver) ||
aIID.equals(Ci.nsISupports))
return this;
throw Cr.NS_ERROR_NO_INTERFACE;
}
};
function checkStatusLine(channel, httpMaxVer, httpMinVer, httpCode, statusText)
{
do_check_eq(channel.responseStatus, httpCode);
do_check_eq(channel.responseStatusText, statusText);
var respMaj = {}, respMin = {};
channel.getResponseVersion(respMaj, respMin);
do_check_eq(respMaj.value, httpMaxVer);
do_check_eq(respMin.value, httpMinVer);
}
function performNextTest()
{
do_test_pending();
var ch = makeChannel(paths[currPathIndex]);
ch.asyncOpen(listener, null);
}
var srv;
function run_test()
@ -152,27 +56,77 @@ function run_test()
srv.start(4444);
performNextTest();
runHttpTests(tests, function() { srv.stop() });
}
// PATH HANDLERS
/*************
* UTILITIES *
*************/
function checkStatusLine(channel, httpMaxVer, httpMinVer, httpCode, statusText)
{
do_check_eq(channel.responseStatus, httpCode);
do_check_eq(channel.responseStatusText, statusText);
var respMaj = {}, respMin = {};
channel.getResponseVersion(respMaj, respMin);
do_check_eq(respMaj.value, httpMaxVer);
do_check_eq(respMin.value, httpMinVer);
}
/*********
* TESTS *
*********/
var tests = [];
var test;
// /no/setstatusline
function noSetstatusline(metadata, response)
{
}
test = new Test("http://localhost:4444/no/setstatusline",
null, startNoSetStatusLine, stop);
tests.push(test);
function startNoSetStatusLine(ch, cx)
{
checkStatusLine(ch, 1, 1, 200, "OK");
}
function stop(ch, cx, status, data)
{
do_check_true(Components.isSuccessCode(status));
}
// /http1_0
function http1_0(metadata, response)
{
response.setStatusLine("1.0", 200, "OK");
}
test = new Test("http://localhost:4444/http1_0",
null, startHttp1_0, stop);
tests.push(test);
function startHttp1_0(ch, cx)
{
checkStatusLine(ch, 1, 0, 200, "OK");
}
// /http1_1
function http1_1(metadata, response)
{
response.setStatusLine("1.1", 200, "OK");
}
test = new Test("http://localhost:4444/http1_1",
null, startHttp1_1, stop);
tests.push(test);
function startHttp1_1(ch, cx)
{
checkStatusLine(ch, 1, 1, 200, "OK");
}
// /invalidVersion
function invalidVersion(metadata, response)
@ -186,6 +140,15 @@ function invalidVersion(metadata, response)
response.setHeader("Passed", "true", false);
}
}
test = new Test("http://localhost:4444/invalidVersion",
null, startPassedTrue, stop);
tests.push(test);
function startPassedTrue(ch, cx)
{
checkStatusLine(ch, 1, 1, 200, "OK");
do_check_eq(ch.getResponseHeader("Passed"), "true");
}
// /invalidStatus
function invalidStatus(metadata, response)
@ -199,6 +162,11 @@ function invalidStatus(metadata, response)
response.setHeader("Passed", "true", false);
}
}
test = new Test("http://localhost:4444/invalidStatus",
null, startPassedTrue, stop);
tests.push(test);
// /invalidDescription
function invalidDescription(metadata, response)
{
@ -211,15 +179,35 @@ function invalidDescription(metadata, response)
response.setHeader("Passed", "true", false);
}
}
test = new Test("http://localhost:4444/invalidDescription",
null, startPassedTrue, stop);
tests.push(test);
// /crazyCode
function crazyCode(metadata, response)
{
response.setStatusLine("1.1", 617, "Crazy");
}
test = new Test("http://localhost:4444/crazyCode",
null, startCrazy, stop);
tests.push(test);
function startCrazy(ch, cx)
{
checkStatusLine(ch, 1, 1, 617, "Crazy");
}
// /nullVersion
function nullVersion(metadata, response)
{
response.setStatusLine(null, 255, "NULL");
}
test = new Test("http://localhost:4444/nullVersion",
null, startNullVersion, stop);
tests.push(test);
function startNullVersion(ch, cx)
{
// currently, this server implementation defaults to 1.1
checkStatusLine(ch, 1, 1, 255, "NULL");
}