Skip to content

Instantly share code, notes, and snippets.

@kinjouj
Created May 1, 2012 06:39
Show Gist options
  • Save kinjouj/2565623 to your computer and use it in GitHub Desktop.
Save kinjouj/2565623 to your computer and use it in GitHub Desktop.
node.js+express+tes-tcp+QUnit
var express = require("express");
var app = express.createServer(express.logger());
app.get("/", function(req, res) {
res.send("hoge");
});
module.exports = app;
QUnit.module("module", {
"setup": function() {
var TestTCP = require("test-tcp").TestTCP;
this.server = new TestTCP({ "code": require("app") });
},
"teardown": function() {
this.server.stop();
}
});
asyncTest("sample test", function() {
this.server.on("start", function() {
var port = this.port;
ok(port);
var request = function(path, callback) {
return require("http").request({
"scheme": "http",
"host": "localhost",
"port": port,
"path": path,
"method": "GET"
});
};
var queues = new Array();
queues.push({
"request": request("/"),
"callback": function(res, done) {
equal(res.statusCode, 200, "/ status code test");
done();
}
});
queues.push({
"request": request("/dummy"),
"callback": function(res, done) {
equal(res.statusCode, 404, "/dummy status code test");
done();
}
});
var len = queues.length;
var done = function() {
len--;
if (len <= 0) {
start();
}
};
queues.forEach(function(queue) {
queue.request.end();
queue.request.on("response", function(res) {
queue.callback(res, done);
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment