Debugging http. Add simple test. (Does not pass.)
authorRyan <ry@tinyclouds.org>
Tue, 19 May 2009 12:49:28 +0000 (14:49 +0200)
committerRyan <ry@tinyclouds.org>
Tue, 19 May 2009 12:50:09 +0000 (14:50 +0200)
src/http.js
src/net.cc
test/test-http.js [new file with mode: 0644]
test/test-pingpong.js
test_http.js [deleted file]

index 869d725..a2642cc 100644 (file)
@@ -250,6 +250,7 @@ node.http.ServerResponse = function (connection, responses) {
 node.http.Server = function (RequestHandler, options) {
   if (!(this instanceof node.http.Server))
     throw Error("Constructor called as a function");
+  var server = this;
 
   function ConnectionHandler (connection) {
     // An array of responses for each connection. In pipelined connections
@@ -301,7 +302,7 @@ node.http.Server = function (RequestHandler, options) {
 
         res.should_keep_alive = this.should_keep_alive;
 
-        return RequestHandler(req, res);
+        return RequestHandler.apply(server, [req, res]);
       };
 
       this.onBody = function (chunk) {
@@ -329,8 +330,8 @@ node.http.Server = function (RequestHandler, options) {
     new node.http.LowLevelServer(ConnectionHandler, options);
 };
 
-node.http.Client = function (port, host, options) {
-  var connection = new node.http.LowLevelClient(options);
+node.http.Client = function (port, host) {
+  var connection = new node.http.LowLevelClient();
   var requests  = [];
 
   function ClientRequest (method, uri, header_lines) {
@@ -440,15 +441,16 @@ node.http.Client = function (port, host, options) {
   }
   
   connection.onConnect = function () {
-    puts("HTTP CLIENT: connected");
+    //node.debug("HTTP CLIENT: connected");
     requests[0].flush();
   };
 
   connection.onDisconnect = function () {
+    //node.debug("HTTP CLIENT: disconnect");
     // If there are more requests to handle, reconnect.
     if (requests.length > 0) {
-      puts("HTTP CLIENT: reconnecting");
-      connection.connect(port, host);
+      //node.debug("HTTP CLIENT: reconnecting");
+      this.connect(port, host);
     }
   };
 
index a83885e..4a81a6f 100644 (file)
@@ -28,7 +28,7 @@ using namespace node;
 #define TIMEOUT_SYMBOL        String::NewSymbol("timeout")
 #define SERVER_SYMBOL         String::NewSymbol("server")
 
-#define PROTOCOL_SYMBOL String::NewSymbol("protocol")
+#define PROTOCOL_SYMBOL           String::NewSymbol("protocol")
 #define CONNECTION_HANDLER_SYMBOL String::NewSymbol("connection_handler")
 
 #define READY_STATE_SYMBOL  String::NewSymbol("readyState")
@@ -67,12 +67,12 @@ Connection::Initialize (v8::Handle<v8::Object> target)
   NODE_SET_PROTOTYPE_METHOD(constructor_template, "fullClose", FullClose);
   NODE_SET_PROTOTYPE_METHOD(constructor_template, "forceClose", ForceClose);
 
-  constructor_template->PrototypeTemplate()->SetAccessor(
+  constructor_template->InstanceTemplate()->SetAccessor(
       ENCODING_SYMBOL,
       EncodingGetter,
       EncodingSetter);
 
-  constructor_template->PrototypeTemplate()->SetAccessor(
+  constructor_template->InstanceTemplate()->SetAccessor(
       READY_STATE_SYMBOL,
       ReadyStateGetter);
 
@@ -217,7 +217,6 @@ Connection::Connect (const Arguments& args)
             );
 
   connection->Attach();
-
   return Undefined();
 }
 
diff --git a/test/test-http.js b/test/test-http.js
new file mode 100644 (file)
index 0000000..13e52a0
--- /dev/null
@@ -0,0 +1,67 @@
+include("mjsunit");
+
+var port = 8000;
+
+function onLoad() {
+
+  var request_number = 0;
+
+  new node.http.Server(function (req, res) {
+    var server = this;
+    req.id = request_number++;
+
+    if (req.id == 0) {
+      puts("get req");
+      assertEquals("GET", req.method);
+      assertEquals("/hello", req.uri.path);
+    }
+
+    if (req.id == 1) {
+      puts("post req");
+      assertEquals("POST", req.method);
+      assertEquals("/quit", req.uri.path);
+      server.close();
+      puts("server closed");
+    }
+
+    setTimeout(function () {
+      res.sendHeader(200, [["Content-Type", "text/plain"]]);
+      res.sendBody(req.uri.path);
+      res.finish();
+    }, 1);
+
+  }).listen(port);
+
+  var c = new node.tcp.Connection();
+  var req_sent = 0;
+  c.onConnect = function () {
+    puts("send get");
+    c.send( "GET /hello HTTP/1.1\r\n\r\n" );
+    req_sent += 1;
+  };
+  var total = "";
+
+  c.onReceive = function (chunk) {
+    total += chunk.encodeUtf8();
+    puts("total: " + JSON.stringify(total));
+
+    if ( req_sent == 1) {
+      puts("send post");
+      c.send("POST /quit HTTP/1.1\r\n\r\n");
+      c.close();
+      req_sent += 1;
+    }
+  };
+
+  c.onDisconnect = function () {
+    puts("client disocnnected");
+
+    var hello = new RegExp("/hello");
+    assertTrue(hello.exec(total) != null);
+
+    var quit = new RegExp("/quit");
+    assertTrue(quit.exec(total) != null);
+  };
+
+  c.connect(port);
+}
index 28724b7..79cc84c 100644 (file)
@@ -68,4 +68,5 @@ function onLoad() {
   };
 
   client.connect(port);
+  assertEquals("closed", client.readyState);
 }
diff --git a/test_http.js b/test_http.js
deleted file mode 100644 (file)
index 5e5da57..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-new node.http.Server(function (req, res) {
-  puts("got req to " + req.uri.path);
-  setTimeout(function () {
-    res.sendHeader(200, [["Content-Type", "text/plain"]]);
-    res.sendBody(JSON.stringify(req.uri));
-    res.finish();
-  }, 1);
-}).listen(8000, "localhost");