Skip to content

Commit

Permalink
ejs
Browse files Browse the repository at this point in the history
  • Loading branch information
openhoat committed Feb 27, 2015
1 parent fbcbf86 commit 03c6745
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-factory",
"version": "0.0.4",
"version": "0.0.5",
"description": "Easy setup an Express instance",
"main": "lib/express-factory.js",
"author": "Olivier Penhoat <openhoat@gmail.com> (http://headwood.net/)",
Expand Down
2 changes: 1 addition & 1 deletion samples/sample6.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ expressInstance = expressFactory({
'views': path.join(__dirname, '..', 'templates')
},
engine: { // express app engine
'html': ejs.renderFile
html: ejs.renderFile
},
routers: {
routes: {
Expand Down
223 changes: 219 additions & 4 deletions spec/expressFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('express factory', function () {
});
});

it('should get not found response', function () {
it('should get not found text response', function () {
return requestAsync(
{
method: 'GET',
Expand All @@ -79,6 +79,40 @@ describe('express factory', function () {
});
});

it('should get not found html response', function () {
return requestAsync(
{
method: 'GET',
url: util.format('http://localhost:%s/%s', port, 'notfound'),
headers: {
accept: 'text/html'
}
})
.spread(function (res, body) {
should.exist(res);
res.should.have.property('statusCode', 404);
res.headers.should.have.property('content-type', 'text/html; charset=utf-8');
should.exist(body);
body.should.equal('<html><body><h3>resource not found</h3></body></html>');
});
});

it('should get not found json response', function () {
return requestAsync(
{
method: 'GET',
url: util.format('http://localhost:%s/%s', port, 'notfound'),
json: true
})
.spread(function (res, body) {
should.exist(res);
res.should.have.property('statusCode', 404);
res.headers.should.have.property('content-type', 'application/json; charset=utf-8');
should.exist(body);
body.should.eql({'message': 'resource not found'});
});
});

it('should stop', function () {
return expressInstance.stop();
});
Expand Down Expand Up @@ -161,7 +195,7 @@ describe('express factory', function () {
});
});

it('should get an error', function () {
it('should get a text error', function () {
return requestAsync(
{
method: 'GET',
Expand All @@ -176,6 +210,40 @@ describe('express factory', function () {
});
});

it('should get a html error', function () {
return requestAsync(
{
method: 'GET',
url: util.format('http://localhost:%s/%s', port, 'error'),
headers: {
accept: 'text/html'
}
})
.spread(function (res, body) {
should.exist(res);
res.should.have.property('statusCode', 500);
res.headers.should.have.property('content-type', 'text/html; charset=utf-8');
should.exist(body);
body.should.equal('<html><body><h3>Error: test</h3></body></html>');
});
});

it('should get a json error', function () {
return requestAsync(
{
method: 'GET',
url: util.format('http://localhost:%s/%s', port, 'error'),
json: true
})
.spread(function (res, body) {
should.exist(res);
res.should.have.property('statusCode', 500);
res.headers.should.have.property('content-type', 'application/json; charset=utf-8');
should.exist(body);
body.should.eql({error: 'Error: test'});
});
});

});

describe('use express instance with a unix socket', function () {
Expand Down Expand Up @@ -380,6 +448,72 @@ describe('express factory', function () {

});

describe('use express instance and a global body parser middleware', function () {

var expressInstance
, port = 3001;

before(function () {
var bodyParser = require('body-parser')
, contact;
expressInstance = expressFactory({
port: port,
use: bodyParser.json(),
routers: {
routes: [{
method: 'GET',
path: '/contact',
middleware: function (req, res) {
res.json(contact);
}
}, {
method: 'POST',
path: '/contact',
middleware: function (req, res) {
contact = req.body;
res.status(201).end();
}
}]
}
});
return expressInstance.start();
});

after(function () {
return expressInstance.stop();
});

it('should get contact resource', function () {
var contact = {firstName: 'John', lastName: 'Doe', email: 'john@doe.com'};
return requestAsync(
{
method: 'POST',
url: util.format('http://localhost:%s/%s', port, 'contact'),
json: true,
body: contact
})
.spread(function (res/*, body*/) {
should.exist(res);
res.should.have.property('statusCode', 201);
})
.then(function () {
return requestAsync({
method: 'GET',
url: util.format('http://localhost:%s/%s', port, 'contact'),
json: true
});
})
.spread(function (res, body) {
should.exist(res);
res.should.have.property('statusCode', 200);
res.headers.should.have.property('content-type', 'application/json; charset=utf-8');
should.exist(body);
body.should.eql(contact);
});
});

});

describe('use express instance and body parser middleware', function () {

var expressInstance
Expand Down Expand Up @@ -460,7 +594,7 @@ describe('express factory', function () {
'view engine': 'html',
'views': path.join(__dirname, '..', 'templates')
},
engine: {'html': ejs.renderFile},
engine: {html: ejs.renderFile},
routers: {
routes: {
path: '/',
Expand All @@ -477,7 +611,58 @@ describe('express factory', function () {
return expressInstance.stop();
});

it('should get contact resource', function () {
it('should get hello view', function () {
var contact = {firstName: 'John', lastName: 'Doe', email: 'john@doe.com'};
return requestAsync('http://localhost:3000/')
.spread(function (res, body) {
should.exist(res);
res.should.have.property('statusCode', 200);
should.exist(body);
body.should.equal('<!DOCTYPE html>\n' +
'<html>\n' +
'<head>\n' +
' <meta charset="UTF-8">\n' +
' <title>Hello!</title>\n' +
'</head>\n' +
'<body>\n' +
'<h3>Hello!</h3>\n' +
'</body>\n' +
'</html>');
});
});

});

describe('use express with direct start', function () {

var path = require('path')
, ejs = require('ejs')
, expressInstance;

before(function () {
expressInstance = expressFactory();
return expressInstance.start({
set: {
'view engine': 'html',
'views': path.join(__dirname, '..', 'templates')
},
engine: {html: ejs.renderFile},
routers: {
routes: {
path: '/',
middleware: function (req, res) {
res.render('home', {content: 'Hello!'});
}
}
}
});
});

after(function () {
return expressInstance.stop();
});

it('should get contact resource with direct start', function () {
var contact = {firstName: 'John', lastName: 'Doe', email: 'john@doe.com'};
return requestAsync('http://localhost:3000/')
.spread(function (res/*, body*/) {
Expand All @@ -488,4 +673,34 @@ describe('express factory', function () {

});

describe('use express with callback', function () {

var expressInstance;

before(function (done) {
expressInstance = expressFactory();
return expressInstance.start(done);
});

after(function () {
return expressInstance.stop();
});

it('should get welcome response with callback', function () {
return requestAsync(
{
method: 'GET',
url: util.format('http://localhost:3000')
})
.spread(function (res, body) { // Use of spread to have 2 arguments instead of an array
should.exist(res);
res.should.have.property('statusCode', 200);
res.headers.should.have.property('content-type', 'text/plain; charset=utf-8');
should.exist(body);
body.should.equal('It works!');
});
});

});

});

0 comments on commit 03c6745

Please sign in to comment.