Skip to content

Commit

Permalink
add test/test_http_server_echo and 'make test'
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Mar 4, 2009
1 parent 559c198 commit 9e5eff3
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 4 deletions.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions README
Original file line number Diff line number Diff line change
@@ -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:

Expand Down
2 changes: 1 addition & 1 deletion node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ LogCallback (const Arguments& args)
printf("%s\n", *value);
fflush(stdout);

return v8::Undefined();
return Undefined();
}

static void
Expand Down
4 changes: 4 additions & 0 deletions node_timer.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "node.h"
#include "node_timer.h"
#include <assert.h>

using namespace v8;

Expand Down Expand Up @@ -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[] = {};
/*
Expand Down Expand Up @@ -162,6 +165,7 @@ static Handle<Value> setInterval
( const Arguments& args
)
{
assert(0 && "not implemented");
}

void
Expand Down
29 changes: 29 additions & 0 deletions test/common.rb
Original file line number Diff line number Diff line change
@@ -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

54 changes: 54 additions & 0 deletions test/test_http_server_echo.rb
Original file line number Diff line number Diff line change
@@ -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 + "/");

0 comments on commit 9e5eff3

Please sign in to comment.