mirror of https://github.com/nodejs/node.git
Implementation of node.http.cat
parent
79010540fc
commit
ce85f84d15
19
src/http.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
|
||||
|
||||
|
|
|
@ -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()
|
||||
})
|
||||
}
|
||||
|
|
|
@ -15,4 +15,6 @@ function onLoad() {
|
|||
assertInstanceof(a.A, Function);
|
||||
assertEquals("A", a.A());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Add tests for remote require using a timeout
|
Loading…
Reference in New Issue