forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test/test_http_server_echo and 'make test'
- Loading branch information
Showing
6 changed files
with
113 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "/"); |