B2G/gecko/netwerk/test/httpserver/test/test_default_index_handler.js
/**
 * 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
 * entries, in order.
 *
 * @param bytes
 *   array of bytes representing the index page's contents
 * @param uri
 *   string which is the URI of the index page
 * @param path
 *   the path portion of uri
 * @param dirEntries
 *   sorted (in the manner the directory entries should be sorted) array of
 *   objects, each of which has a name property (whose value is the file's name,
 *   without / if it's a directory) and an isDirectory property (with expected
 *   value)
 */
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
entries, in order.

@param bytes
array of bytes representing the index page's contents
@param uri
string which is the URI of the index page
@param path
the path portion of uri
@param dirEntries
sorted (in the manner the directory entries should be sorted) array of
objects, each of which has a name property (whose value is the file's name,
without / if it's a directory) and an isDirectory property (with expected
value)
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 entries in order . @param bytes array of bytes representing the index page ' s contents @param uri string which is the URI of the index page @param path the path portion of uri @param dirEntries sorted ( in the manner the directory entries should be sorted ) array of objects each of which has a name property ( whose value is the file ' s name without / if it ' s a directory ) and an isDirectory property ( with expected value )
function dataCheck(bytes, uri, path, dirEntries)
{
  var data = String.fromCharCode.apply(null, bytes);
  var parser = Cc["@mozilla.org/xmlextras/domparser;1"]
                 .createInstance(Ci.nsIDOMParser);
  try
  {
    var doc = parser.parseFromString(data, "application/xml");
  }
  catch (e)
  {
    do_throw("document failed to parse as XML");
  }
  var body = doc.documentElement.getElementsByTagName("body");
  do_check_eq(body.length, 1);
  body = body.item(0);
  var header = body.QueryInterface(Ci.nsIDOMElement)
                   .getElementsByTagName("h1");
  do_check_eq(header.length, 1);
  do_check_eq(header.item(0).QueryInterface(Ci.nsIDOMNode).textContent, path);
  var lst = body.getElementsByTagName("ol");
  do_check_eq(lst.length, 1);
  var items = lst.item(0).QueryInterface(Ci.nsIDOMElement)
                         .getElementsByTagName("li");
  var ios = Cc["@mozilla.org/network/io-service;1"]
              .getService(Ci.nsIIOService);
  var dirURI = ios.newURI(uri, null, null);
  for (var i = 0; i < items.length; i++)
  {
    var link = items.item(i)
                    .childNodes
                    .item(0)
                    .QueryInterface(Ci.nsIDOMNode)
                    .QueryInterface(Ci.nsIDOMElement);
    var f = dirEntries[i];
    var sep = f.isDirectory ? "/" : "";
    do_check_eq(link.textContent, f.name + sep);
    uri = ios.newURI(link.getAttribute("href"), null, top);
    do_check_eq(decodeURIComponent(uri.path), path + f.name + sep);
  }
}
function dataCheck ( bytes , uri , path , dirEntries ) { var data = String . fromCharCode . apply ( null , bytes ) ; var parser = Cc [ "@mozilla.org/xmlextras/domparser;1" ] . createInstance ( Ci . nsIDOMParser ) ; try { var doc = parser . parseFromString ( data , "application/xml" ) ; } catch ( e ) { do_throw ( "document failed to parse as XML" ) ; } var body = doc . documentElement . getElementsByTagName ( "body" ) ; do_check_eq ( body . length , 1 ) ; body = body . item ( 0 ) ; var header = body . QueryInterface ( Ci . nsIDOMElement ) . getElementsByTagName ( "h1" ) ; do_check_eq ( header . length , 1 ) ; do_check_eq ( header . item ( 0 ) . QueryInterface ( Ci . nsIDOMNode ) . textContent , path ) ; var lst = body . getElementsByTagName ( "ol" ) ; do_check_eq ( lst . length , 1 ) ; var items = lst . item ( 0 ) . QueryInterface ( Ci . nsIDOMElement ) . getElementsByTagName ( "li" ) ; var ios = Cc [ "@mozilla.org/network/io-service;1" ] . getService ( Ci . nsIIOService ) ; var dirURI = ios . newURI ( uri , null , null ) ; for ( var i = 0 ; i < items . length ; i ++ ) { var link = items . item ( i ) . childNodes . item ( 0 ) . QueryInterface ( Ci . nsIDOMNode ) . QueryInterface ( Ci . nsIDOMElement ) ; var f = dirEntries [ i ] ; var sep = f . isDirectory ? "/" : "" ; do_check_eq ( link . textContent , f . name + sep ) ; uri = ios . newURI ( link . getAttribute ( "href" ) , null , top ) ; do_check_eq ( decodeURIComponent ( uri . path ) , path + f . name + sep ) ; } }
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
 entries, in order.
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 entries in order .
// 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. // See all the .QueryInterface()s and .item()s happening here?  That's because // xpcshell sucks and doesn't have classinfo, so no automatic interface // flattening or array-style access to items in NodeLists.  Suck. // header // files