B2G/gecko/netwerk/test/httpserver/test/head_utils.js
/**
 * Runs all the tests in testArray, an array of RawTests.
 *
 * @param testArray : [RawTest]
 *   an array of RawTests to run, in order
 * @param done
 *   function to call when all tests have run (e.g. to shut down the server)
 */
Runs all the tests in testArray, an array of RawTests.
@param testArray : [RawTest]
an array of RawTests to run, in order
@param done
function to call when all tests have run
Runs all the tests in testArray an array of RawTests . @param testArray : [ RawTest ] an array of RawTests to run in order @param done function to call when all tests have run
function runRawTests(testArray, done)
{
  do_test_pending();
  var sts = Cc["@mozilla.org/network/socket-transport-service;1"]
              .getService(Ci.nsISocketTransportService);
  var currentThread = Cc["@mozilla.org/thread-manager;1"]
                        .getService()
                        .currentThread;
  function performNextTest()
  {
    if (++testIndex == testArray.length)
    {
      do_test_finished();
      try
      {
        done();
      }
      catch (e)
      {
        do_report_unexpected_exception(e, "running test-completion callback");
      }
      return;
    }
    var rawTest = testArray[testIndex];
    var transport =
      sts.createTransport(null, 0, rawTest.host, rawTest.port, null);
    var inStream = transport.openInputStream(0, 0, 0);
    var outStream  = transport.openOutputStream(0, 0, 0);
    dataIndex = 0;
    received = "";
    waitForMoreInput(inStream);
    waitToWriteOutput(outStream);
  }
  function waitForMoreInput(stream)
  {
    reader.stream = stream;
    stream = stream.QueryInterface(Ci.nsIAsyncInputStream);
    stream.asyncWait(reader, 0, 0, currentThread);
  }
  function waitToWriteOutput(stream)
  {
    stream = stream.QueryInterface(Ci.nsIAsyncOutputStream);
    stream.asyncWait(writer, 0, testArray[testIndex].data[dataIndex].length,
                     currentThread);
  }
  var testIndex = -1;
  var dataIndex = 0;
  var received = "";
  var reader =
    {
      onInputStreamReady: function(stream)
      {
        do_check_true(stream === this.stream);
        try
        {
          var bis = new BinaryInputStream(stream);
          var av = 0;
          try
          {
            av = bis.available();
          }
          catch (e)
          {
            do_note_exception(e);
          }
          if (av > 0)
          {
            var quantum = 262144;
            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)
        {
          do_report_unexpected_exception(e);
        }
        var rawTest = testArray[testIndex];
        try
        {
          rawTest.responseCheck(received);
        }
        catch (e)
        {
          do_report_unexpected_exception(e);
        }
        finally
        {
          try
          {
            stream.close();
            performNextTest();
          }
          catch (e)
          {
            do_report_unexpected_exception(e);
          }
        }
      }
    };
  var writer = 
    {
      onOutputStreamReady: function(stream)
      {
        var str = testArray[testIndex].data[dataIndex];
        var written = 0;
        try
        {
          written = stream.write(str, str.length);
          if (written == str.length)
            dataIndex++;
          else
            testArray[testIndex].data[dataIndex] = str.substring(written);
        }
        catch (e)
        {
          do_note_exception(e);
        }
        try
        {
          if (written > 0 && dataIndex < testArray[testIndex].data.length)
            waitToWriteOutput(stream);
          else
            stream.close();
        }
        catch (e)
        {
          do_report_unexpected_exception(e);
        }
      }
    };
  performNextTest();
}
function runRawTests ( testArray , done ) { do_test_pending ( ) ; var sts = Cc [ "@mozilla.org/network/socket-transport-service;1" ] . getService ( Ci . nsISocketTransportService ) ; var currentThread = Cc [ "@mozilla.org/thread-manager;1" ] . getService ( ) . currentThread ; function performNextTest ( ) { if ( ++ testIndex == testArray . length ) { do_test_finished ( ) ; try { done ( ) ; } catch ( e ) { do_report_unexpected_exception ( e , "running test-completion callback" ) ; } return ; } var rawTest = testArray [ testIndex ] ; var transport = sts . createTransport ( null , 0 , rawTest . host , rawTest . port , null ) ; var inStream = transport . openInputStream ( 0 , 0 , 0 ) ; var outStream = transport . openOutputStream ( 0 , 0 , 0 ) ; dataIndex = 0 ; received = "" ; waitForMoreInput ( inStream ) ; waitToWriteOutput ( outStream ) ; } function waitForMoreInput ( stream ) { reader . stream = stream ; stream = stream . QueryInterface ( Ci . nsIAsyncInputStream ) ; stream . asyncWait ( reader , 0 , 0 , currentThread ) ; } function waitToWriteOutput ( stream ) { stream = stream . QueryInterface ( Ci . nsIAsyncOutputStream ) ; stream . asyncWait ( writer , 0 , testArray [ testIndex ] . data [ dataIndex ] . length , currentThread ) ; } var testIndex = - 1 ; var dataIndex = 0 ; var received = "" ; var reader = { onInputStreamReady : function ( stream ) { do_check_true ( stream === this . stream ) ; try { var bis = new BinaryInputStream ( stream ) ; var av = 0 ; try { av = bis . available ( ) ; } catch ( e ) { do_note_exception ( e ) ; } if ( av > 0 ) { var quantum = 262144 ; 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 ) { do_report_unexpected_exception ( e ) ; } var rawTest = testArray [ testIndex ] ; try { rawTest . responseCheck ( received ) ; } catch ( e ) { do_report_unexpected_exception ( e ) ; } finally { try { stream . close ( ) ; performNextTest ( ) ; } catch ( e ) { do_report_unexpected_exception ( e ) ; } } } } ; var writer = { onOutputStreamReady : function ( stream ) { var str = testArray [ testIndex ] . data [ dataIndex ] ; var written = 0 ; try { written = stream . write ( str , str . length ) ; if ( written == str . length ) dataIndex ++ ; else testArray [ testIndex ] . data [ dataIndex ] = str . substring ( written ) ; } catch ( e ) { do_note_exception ( e ) ; } try { if ( written > 0 && dataIndex < testArray [ testIndex ] . data . length ) waitToWriteOutput ( stream ) ; else stream . close ( ) ; } catch ( e ) { do_report_unexpected_exception ( e ) ; } } } ; performNextTest ( ) ; }
Runs all the tests in testArray, an array of RawTests.
Runs all the tests in testArray an array of RawTests .
/** Kicks off running the next test in the array. */ // reset // Do the QueryInterface here, not earlier, because there is no // guarantee that 'stream' passed in here been QIed to nsIAsyncOutputStream // since the last GC. /** Index of the test being run. */ /**
   * Index of remaining data strings to be written to the socket in current
   * test.
   */ /** Data received so far from the server. */ /** Reads data from the socket. */ /* default to 0 */ /** Writes data to the socket. */ /* stream could have been closed, just ignore */ // Keep writing data while we can write and  // until there's no more data to read