Skip to content

Commit

Permalink
using http.request to perform tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Clarence Leung committed Jun 5, 2012
1 parent 0f29c9f commit b0aa820
Showing 1 changed file with 76 additions and 18 deletions.
94 changes: 76 additions & 18 deletions test/messenger.js
@@ -1,42 +1,100 @@
"use strict";

var http = require("http");
var vows = require("vows");
var assert = require("assert");
var pact = require("pact");

var yeti = require("../lib/yeti");
var Hub = require("../lib/hub");

vows.describe("Yeti Hub HTTP errors").addBatch({
"Hub setup and connection" : {
topic : pact.httpify(yeti.createHub()),
topic : function () {
var vow = this,
hub = new Hub();
hub.listen(function () {
vow.callback(null, hub);
});
},
teardown: function (hub) {
hub.close();
},
"when a non-GET or HEAD request is used" : {
topic : pact.request({
url : "http://localhost:9000",
method : "PUT"
}),
"should return a 405 error" : pact.code(405),
topic : function (hub) {
var vow = this;
var req = http.request({
host : hub.server.address().address,
port : hub.server.address().port,
method : "PUT"
});
req.on("response", function (res) {
res.setEncoding("utf8");
res.on("data", function (chunk) {
vow.callback(null, {
body : chunk,
status : res.statusCode,
headers : res.headers
});
});
});
req.end();
},
"should return a 405 error" : function (topic) {
assert.equal(topic.status, 405);
},
"should return the proper error message" : function (topic) {
assert.equal(topic.body, "Method Not Allowed\n" +
"GET or HEAD method required.");
}
},
"when the desired route is not found" : {
topic : pact.request({
url : "http://localhost:9000/foobar"
}),
"should return a 404 error" : pact.code(404),
topic : function (hub) {
var vow = this;
var req = http.request({
host : hub.server.address().address,
port : hub.server.address().port,
path : "/foobar",
method : "GET"
});
req.on("response", function (res) {
res.setEncoding("utf8");
res.on("data", function (chunk) {
vow.callback(null, {
body : chunk,
status : res.statusCode,
headers : res.headers
});
});
});
req.end();
},
"should return a 404 error" : function (topic) {
assert.equal(topic.status, 404);
},
"should return the proper error message" : function (topic) {
assert.equal(topic.body, "Not Found\n" +
assert.equal(topic.body, "Not Found\n" +
"Unable to find what you're looking for.");
}
},
"when a HEAD request is not found" : {
topic : pact.request({
url : "http://localhost:9000/foobar",
method : "HEAD"
}),
"should end with no message" : function (topic) {
assert.equal(topic.body, "");
topic : function (hub) {
var vow = this;
var req = http.request({
host : hub.server.address().address,
port : hub.server.address().port,
path : "/foobar",
method : "HEAD"
});
req.on("response", function (res) {
vow.callback(null, {
status : res.statusCode,
headers : res.headers
});
});
req.end();
},
"should return a 404 error" : function (topic) {
assert.equal(topic.status, 404);
}
}
}
Expand Down

0 comments on commit b0aa820

Please sign in to comment.