add test/test_http_server_echo and 'make test'
authorRyan <ry@tinyclouds.org>
Wed, 4 Mar 2009 10:29:39 +0000 (11:29 +0100)
committerRyan <ry@tinyclouds.org>
Wed, 4 Mar 2009 10:35:43 +0000 (11:35 +0100)
Makefile
README
node.cc
node_timer.cc
test/common.rb [new file with mode: 0644]
test/test_http_server_echo.rb [new file with mode: 0755]

index b1e812e..de5cfae 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -40,6 +40,17 @@ ebb_request_parser.o: ebb_request_parser.c deps/ebb/ebb_request_parser.h
 ebb_request_parser.c: deps/ebb/ebb_request_parser.rl
        ragel -s -G2 $< -o $@
 
+PASS="\033[1;32mPASS\033[0m\n" 
+FAIL="\033[1;31mFAIL\033[0m\n" 
+
+test: node test/test_*
+       @for i in test/test_*; do \
+               if [ -x $$i ]; then \
+                       echo "\n\033[1m$$i\033[0m";     \
+                       ./$$i && echo $(PASS) || echo $(FAIL); \
+               fi \
+       done 
+
 clean:
        rm -f ebb_request_parser.c
        rm -f *.o 
diff --git a/README b/README
index 58c89a8..5ae2e68 100644 (file)
--- a/README
+++ b/README
@@ -1,9 +1,20 @@
+WHEREAS, Evented, asynchornous programming better models reality; and
+
+WHEREAS, Servers organized around event loops are more efficent; and
+
 WHEREAS, The usage of threads has complicated computer programming; and
 
-WHEREAS, V8 javascript comes free of I/O and threads; and
+WHEREAS, Most operating systems do not provide asynchonous file system access.
+
+WHEREAS, Javascript is a language without a concept of I/O or threads; and
+
+WHEREAS, Users familar with using Javascript in the web browser already program using events and callbacks; and
+
+WHEREAS, The V8 javascript comes free of I/O and threads; and
+
+WHEREAS, The V8 javascript compiles Javascript code directly to Assembler; and
 
-WHEREAS, Most operating systems do not provide asynchonous file system
-access.
+WHEREAS, The libev event loop abstraction provides access to the best event loop interface on each system.
 
 Now, therefore:
 
diff --git a/node.cc b/node.cc
index 598ae7e..2eb189c 100644 (file)
--- a/node.cc
+++ b/node.cc
@@ -118,7 +118,7 @@ LogCallback (const Arguments& args)
   printf("%s\n", *value);
   fflush(stdout);
 
-  return v8::Undefined();
+  return Undefined();
 }
 
 static void
index 9091dc9..f06542c 100644 (file)
@@ -1,5 +1,6 @@
 #include "node.h"
 #include "node_timer.h"
+#include <assert.h>
 
 using namespace v8;
 
@@ -111,6 +112,8 @@ setTimeout(const Arguments& args)
 
   ev_tstamp after = (double)delay / 1000.0;
 
+  if (args.Length() > 2)
+    assert(0 && "extra params to setTimeout not yet implemented.");
   int argc = 0;
   Handle<Value> argv[] = {};
   /*
@@ -162,6 +165,7 @@ static Handle<Value> setInterval
   ( const Arguments& args
   ) 
 {
+  assert(0 && "not implemented");
 }
 
 void
diff --git a/test/common.rb b/test/common.rb
new file mode 100644 (file)
index 0000000..484b7bd
--- /dev/null
@@ -0,0 +1,29 @@
+require 'net/http'
+require 'tempfile'
+
+$node = File.join(File.dirname(__FILE__), "../node")
+$tf = Tempfile.open("node")
+$tf.puts(DATA.read)
+$tf.close
+
+def assert(x, msg = "")
+  raise(msg) unless x
+end
+
+def assert_equal(x, y, msg = "")
+  assert(x == y, "#{x.inspect} != #{y.inspect} #{msg}")
+end
+
+def wait_for_server(host, port)
+  loop do
+    begin
+      socket = ::TCPSocket.open(host, port)
+      return
+    rescue Errno::ECONNREFUSED
+      $stderr.print "." if $DEBUG
+      $stderr.flush
+      sleep 0.2
+    end
+  end
+end
+
diff --git a/test/test_http_server_echo.rb b/test/test_http_server_echo.rb
new file mode 100755 (executable)
index 0000000..7b5f8d4
--- /dev/null
@@ -0,0 +1,54 @@
+#!/usr/bin/env ruby
+require File.dirname(__FILE__) + "/common"
+
+pid = fork do
+  exec($node, $tf.path)
+end
+
+begin 
+  wait_for_server("localhost", 8000)
+  connection = Net::HTTP.new("localhost", 8000)
+
+  response, body = connection.get("/")
+  assert_equal(response.code, "200")
+  assert(response.chunked?)
+  assert_equal(body, "\n")
+  assert_equal(response.content_type, "text/plain")
+
+  response, body = connection.post("/", "hello world")
+  assert_equal(response.code, "200")
+  assert(response.chunked?)
+  assert_equal(body, "hello world\n")
+  assert_equal(response.content_type, "text/plain")
+ensure
+  `kill -9 #{pid}`
+end
+
+__END__
+function encode(data) { 
+  var chunk = data.toString();
+  return chunk.length.toString(16) + "\r\n" + chunk + "\r\n";
+}
+
+var port = 8000;
+var server = new HTTP.Server("localhost", port);
+
+server.onRequest = function (request) {
+
+  // onBody sends null on the last chunk.
+  request.onBody = function (chunk) {
+    if(chunk) { 
+      this.respond(encode(chunk));
+    } else {
+      this.respond(encode("\n"));
+      this.respond("0\r\n\r\n");
+      this.respond(null); // signals end-of-request
+    }
+  }
+  request.respond("HTTP/1.0 200 OK\r\n");
+  request.respond("Content-Type: text/plain\r\n");
+  request.respond("Transfer-Encoding: chunked\r\n");
+  request.respond("\r\n");
+};
+
+log("Running at http://localhost:" + port + "/");