Browse Source

Implementation of node.http.cat

v0.7.4-release
Urban Hafner 16 years ago
parent
commit
ce85f84d15
  1. 19
      src/http.js
  2. 18
      test/test-http-cat.js
  3. 4
      test/test-remote-module-loading.js

19
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

18
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()
})
}

4
test/test-remote-module-loading.js

@ -15,4 +15,6 @@ function onLoad() {
assertInstanceof(a.A, Function);
assertEquals("A", a.A());
}
}
// TODO: Add tests for remote require using a timeout
Loading…
Cancel
Save