0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-28 14:33:11 +01:00
nodejs/test/test_http_server_echo.rb
2009-03-04 12:04:04 +01:00

56 lines
1.3 KiB
Ruby
Executable File

#!/usr/bin/env ruby
require 'net/http'
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 + "/");