-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstracted out some of the unit testing
- Loading branch information
Showing
4 changed files
with
181 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
var test = require("unit.js"); | ||
var request = require("supertest"); | ||
var Q = require("q"); | ||
|
||
var should = test.should; | ||
|
||
var utilities = require("./utilities.js"); | ||
//server setup stuff | ||
var routes = require("../../index.js"); | ||
|
||
var test1 = require("./testconfigs/test1.js"); | ||
|
||
var express = require("express"); | ||
var socket = require("socket.io"); | ||
|
||
var app = express(); | ||
var server = require("http").Server(app); | ||
var io = socket(server); | ||
|
||
app.engine('html', require('ejs').renderFile); | ||
|
||
routes(test1, app, io); | ||
|
||
//if (!module.parent) { | ||
server.listen(3000); | ||
console.log("Express started on port 3000"); | ||
//} | ||
////////////////////////////////////////////////// | ||
|
||
var clients = []; | ||
//alias for clients[0] | ||
var client; | ||
|
||
var routeCode = function(route, status) { | ||
status = (status ? status : 200); | ||
it(route, function(done) { | ||
request(app) | ||
.get(route) | ||
.expect(status) | ||
.end(function(err, res) { | ||
if (err) return done(err); | ||
done(); | ||
}); | ||
}); | ||
}; | ||
|
||
var socketReturn = function(route) { | ||
it(route, function(done) { | ||
client.emit(route, {}); | ||
client.once(route, function(data) { | ||
data.should.exist; | ||
done(); | ||
}); | ||
}); | ||
}; | ||
|
||
describe("Set up should work", function() { | ||
it("should require a json config", function() { | ||
routes(null, app, io).should.equal(false); | ||
}); | ||
it("should require a json config with a routes array", function() { | ||
routes({}, app, io).should.equal(false); | ||
}); | ||
it("should return a router if express not passed in", function() { | ||
routes(test1); | ||
}); | ||
it("Should connect to the socket", function(done) { | ||
var deffers = []; | ||
for (var i = 0; i < 5; i++) { | ||
deffers.push(utilities.createSocket()); | ||
} | ||
Q.all(deffers).then(function(data) { | ||
clients = data; | ||
client = clients[0]; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("There should be a route that returns all of the current routes", function() { | ||
routeCode("/routes"); | ||
socketReturn("routes"); | ||
}); | ||
|
||
describe("Only configured routes should return 200 code (express)", function() { | ||
routeCode("/test"); | ||
routeCode("/hello"); | ||
routeCode("/sup"); | ||
routeCode("/render"); | ||
routeCode("/sendfile"); | ||
routeCode("/google", 302); | ||
}); | ||
|
||
describe("Only configured routes should return 200 code (socket)", function() { | ||
socketReturn("test/get"); | ||
socketReturn("hello"); | ||
socketReturn("bye"); | ||
socketReturn("render"); | ||
socketReturn("sendfile"); | ||
socketReturn("google"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
var test = require("unit.js"); | ||
var request = require("supertest"); | ||
var Q = require("q"); | ||
|
||
var should = test.should; | ||
|
||
var socketClient = require("socket.io-client"); | ||
|
||
var socketURL = "http://127.0.0.1:3000"; | ||
//var client1 = require('socket.io-client')('http://localhost:3000'); | ||
var socketOptions = { | ||
forceNew: true | ||
}; | ||
|
||
module.exports = { | ||
createSocket: function() { | ||
var defer = Q.defer(); | ||
var client = socketClient.connect(socketURL, socketOptions); | ||
client.once("connect", function() { | ||
defer.resolve(client); | ||
}); | ||
return defer.promise; | ||
}, | ||
socketReturn: function(client, route) { | ||
it(route, function(done) { | ||
client.emit(route, {}); | ||
client.once(route, function(data) { | ||
data.should.exist; | ||
done(); | ||
}); | ||
}); | ||
}, | ||
routeCode: function(app, route, status) { | ||
status = (status ? status : 200); | ||
it(route, function(done) { | ||
request(app) | ||
.get(route) | ||
.expect(status) | ||
.end(function(err, res) { | ||
if (err) return done(err); | ||
done(); | ||
}); | ||
}); | ||
} | ||
}; |