Browse Source

add test/test_http_server_echo and 'make test'

v0.7.4-release
Ryan 16 years ago
parent
commit
9e5eff3b27
  1. 11
      Makefile
  2. 17
      README
  3. 2
      node.cc
  4. 4
      node_timer.cc
  5. 29
      test/common.rb
  6. 54
      test/test_http_server_echo.rb

11
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 ebb_request_parser.c: deps/ebb/ebb_request_parser.rl
ragel -s -G2 $< -o $@ 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: clean:
rm -f ebb_request_parser.c rm -f ebb_request_parser.c
rm -f *.o rm -f *.o

17
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, 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 WHEREAS, The libev event loop abstraction provides access to the best event loop interface on each system.
access.
Now, therefore: Now, therefore:

2
node.cc

@ -118,7 +118,7 @@ LogCallback (const Arguments& args)
printf("%s\n", *value); printf("%s\n", *value);
fflush(stdout); fflush(stdout);
return v8::Undefined(); return Undefined();
} }
static void static void

4
node_timer.cc

@ -1,5 +1,6 @@
#include "node.h" #include "node.h"
#include "node_timer.h" #include "node_timer.h"
#include <assert.h>
using namespace v8; using namespace v8;
@ -111,6 +112,8 @@ setTimeout(const Arguments& args)
ev_tstamp after = (double)delay / 1000.0; ev_tstamp after = (double)delay / 1000.0;
if (args.Length() > 2)
assert(0 && "extra params to setTimeout not yet implemented.");
int argc = 0; int argc = 0;
Handle<Value> argv[] = {}; Handle<Value> argv[] = {};
/* /*
@ -162,6 +165,7 @@ static Handle<Value> setInterval
( const Arguments& args ( const Arguments& args
) )
{ {
assert(0 && "not implemented");
} }
void void

29
test/common.rb

@ -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
test/test_http_server_echo.rb

@ -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 + "/");
Loading…
Cancel
Save