Bug 460258 Provide text debug facilities for fake servers. r=jcranmer
authorMark Banner <bugzilla@standard8.plus.com>
Thu, 30 Oct 2008 08:53:49 +0000
changeset 771 dc4edb5fe229fe1c743e4f421b2e55d2e526e068
parent 770 5709136d4b9c095176da25177cfc64b8034cd3a6
child 772 4fd3dba65a6e6b89fab64d25d5e8958d98ab545b
push idunknown
push userunknown
push dateunknown
reviewersjcranmer
bugs460258
Bug 460258 Provide text debug facilities for fake servers. r=jcranmer
mailnews/test/fakeserver/maild.js
--- a/mailnews/test/fakeserver/maild.js
+++ b/mailnews/test/fakeserver/maild.js
@@ -42,16 +42,20 @@
 var Cc = Components.classes;
 var Ci = Components.interfaces;
 var Cr = Components.results;
 var CC = Components.Constructor;
 
 /** The XPCOM thread manager. */
 var gThreadManager = null;
 
+const fsDebugNone = 0;
+const fsDebugRecv = 1;
+const fsDebugRecvSend = 2;
+const fsDebugAll = 3;
 
 /**
  * JavaScript constructors for commonly-used classes; precreating these is a
  * speedup over doing the same from base principles.  See the docs at
  * http://developer.mozilla.org/en/docs/Components.Constructor for details.
  */
 const ServerSocket = CC("@mozilla.org/network/server-socket;1",
                         "nsIServerSocket",
@@ -94,16 +98,18 @@ const TIMEOUT = 3*60*1000;
  *   thread.processNextEvent(true);
  *
  * do_test_finished();
  *****************************************************************************/
 function nsMailServer(handler) {
   if (!gThreadManager)
     gThreadManager = Cc["@mozilla.org/thread-manager;1"].getService();
 
+  this._debug = fsDebugNone;
+
   /** The port on which this server listens. */
   this._port = undefined;
 
   /** The socket associated with this. */
   this._socket = null;
 
   /**
    * True if the socket in this is closed (and closure notifications have been
@@ -112,35 +118,47 @@ function nsMailServer(handler) {
   this._socketClosed = true;
 
   this._handler = handler;
   this._readers = [];
   this._test = false;
 }
 nsMailServer.prototype = {
   onSocketAccepted : function (socket, trans) {
+    if (this._debug != fsDebugNone)
+      print("Received Connection from " + trans.host + ":" + trans.port);
+
     const SEGMENT_SIZE = 1024;
     const SEGMENT_COUNT = 1024;
     var input = trans.openInputStream(0, SEGMENT_SIZE, SEGMENT_COUNT)
                      .QueryInterface(Ci.nsIAsyncInputStream);
 
-    var reader = new nsMailReader(this, this._handler, trans);
+    var reader = new nsMailReader(this, this._handler, trans, this._debug);
     this._readers.push(reader);
 
     // Note: must use main thread here, or we might get a GC that will cause
     //       threadsafety assertions.  We really need to fix XPConnect so that
     //       you can actually do things in multi-threaded JS.  :-(
     input.asyncWait(reader, 0, 0, gThreadManager.mainThread);
     this._test = true;
   },
 
   onStopListening : function (socket, status) {
+    if (this._debug != fsDebugNone)
+      print("Connection Lost " + status);
+
     this._socketClosed = true;
   },
 
+  setDebugLevel : function (debug) {
+    this._debug = debug;
+    if (this._reader)
+      this._reader.setDebugLevel(debug);
+  },
+
   start : function (port) {
     if (this._socket)
       throw Cr.NS_ERROR_ALREADY_INITIALIZED;
 
     this._port = port;
     this._socketClosed = false;
 
     var socket = new ServerSocket(this._port,
@@ -265,17 +283,18 @@ function readTo(input, count, arr) {
  * The return of postCommand is ignored. The return of onMultiline is a bit
  * complicated: it may or may not return a response string (returning one is
  * necessary to trigger the postCommand handler).
  *
  * This object has the following supplemental functions for use by handlers:
  * closeSocket  Performs a server-side socket closing
  * setMultiline Sets the multiline mode based on the argument
  *****************************************************************************/
-function nsMailReader(server, handler, transport) {
+function nsMailReader(server, handler, transport, debug) {
+  this._debug = debug;
   this._server = server;
   this._buffer = [];
   this._lines = [];
   this._handler = handler;
   this._transport = transport;
   var output = transport.openOutputStream(Ci.nsITransport.OPEN_BLOCKING, 0, 0);
   this._output = output;
   this.transaction = { us : [], them : [] };
@@ -345,16 +364,19 @@ nsMailReader.prototype = {
       return;
     }
     readTo(stream, bytes, this._buffer);
     this._findLines();
 
     while (this._lines.length > 0) {
       var line = this._lines.shift();
 
+      if (this._debug == fsDebugAll)
+        print("RECV: " + line);
+
       var response;
       try {
         if (this._multiline) {
           response = this._handler.onMultiline(line);
 
           if (response === undefined)
             continue;
         } else {
@@ -363,16 +385,20 @@ nsMailReader.prototype = {
 
           // Find the command and splice it out...
           var splitter = line.indexOf(" ");
           var command = splitter == -1 ? line : line.substring(0,splitter);
           var args = splitter == -1 ? "" : line.substring(splitter+1);
 
           // By convention, commands are uppercase
           command = command.toUpperCase();
+
+          if (this._debug == fsDebugRecv || this._debug == fsDebugRecvSend)
+            print("RECV: " + command);
+
           if (command in this._handler)
             response = this._handler[command](args);
           else
             response = this._handler.onError(command, args);
         }
 
         this._preventLFMunge = false;
         this._handler.postCommand(this);
@@ -387,16 +413,25 @@ nsMailReader.prototype = {
         }
       }
 
       if (!this._preventLFMunge)
         response = response.replace(/([^\r])\n/g,"$1\r\n");
 
       if (response.charAt(response.length-1) != '\n')
        response = response + "\r\n";
+
+      if (this._debug == fsDebugRecvSend) {
+        print("SEND: " + response.split(" ", 1)[0]);
+      }
+      else if (this._debug == fsDebugAll) {
+        var responses = response.split("\n");
+        responses.forEach(function (line) { print("SEND: " + line); });
+      }
+
       this.transaction.us.push(response);
       this._output.write(response, response.length);
       this._output.flush();
 
       if (this._signalStop)
         this._realCloseSocket();
     }
 
@@ -415,16 +450,20 @@ nsMailReader.prototype = {
     this._transport.close(Cr.NS_OK);
     this._server.stopTest();
   },
 
   setMultiline : function (multi) {
     this._multiline = multi;
   },
 
+  setDebugLevel : function (debug) {
+    this._debug = debug;
+  },
+
   preventLFMunge : function () {
     this._preventLFMunge = true;
   },
 
   get watchWord () {
     return this._server._watchWord;
   },