From ce85f84d152dde9dc2cead5a8e4889710b3f0a69 Mon Sep 17 00:00:00 2001 From: Urban Hafner Date: Wed, 17 Jun 2009 08:52:47 +0200 Subject: [PATCH] Implementation of node.http.cat --- src/http.js | 19 +++++++++++++++++++ test/test-http-cat.js | 18 ++++++++---------- test/test-remote-module-loading.js | 4 +++- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/http.js b/src/http.js index ccbb994fe4..1f3ee39cc6 100644 --- a/src/http.js +++ b/src/http.js @@ -516,4 +516,23 @@ node.http.Client = function (port, host) { }; }; +node.http.cat = function(url, encoding, callback) { + var uri = node.http.parseUri(url) + uri.port = uri.port == "" ? 80 : Number(uri.port) + uri.path = uri.path == "" ? "/" : uri.path + var req = new node.http.Client(uri.port, uri.host).get(uri.path) + req.finish(function(res) { + var status = res.statusCode; + res.setBodyEncoding(encoding) + var content = "" + res.onBody = function(chunk) { + content += chunk; + } + res.onBodyComplete = function() { + callback(status, content); + } + }) +} + })(); // anonymous namespace + diff --git a/test/test-http-cat.js b/test/test-http-cat.js index ef002a2e08..7632ac2692 100644 --- a/test/test-http-cat.js +++ b/test/test-http-cat.js @@ -2,22 +2,20 @@ include("mjsunit.js"); PORT = 8888; var body = "exports.A = function() { return 'A';}"; -new node.http.Server(function (req, res) { +var server = new node.http.Server(function (req, res) { res.sendHeader(200, [ ["Content-Length", body.length], ["Content-Type", "text/plain"] ]); res.sendBody(body); res.finish(); -}).listen(PORT); +}); +server.listen(PORT); function onLoad() { - try { - node.http.cat("http://localhost:"+PORT, "utf8", function(status, content) { - assertEquals(body, content); - // TODO: Test the status code - }) - } catch(e) { - assertUnreachable(e) - } + node.http.cat("http://localhost:"+PORT, "utf8", function(status, content) { + assertEquals(body, content); + assertEquals(200, status) + server.close() + }) } diff --git a/test/test-remote-module-loading.js b/test/test-remote-module-loading.js index a913dabfd9..b71a9389c0 100644 --- a/test/test-remote-module-loading.js +++ b/test/test-remote-module-loading.js @@ -15,4 +15,6 @@ function onLoad() { assertInstanceof(a.A, Function); assertEquals("A", a.A()); -} \ No newline at end of file +} + +// TODO: Add tests for remote require using a timeout \ No newline at end of file