Skip to content

Commit

Permalink
Code coveraging
Browse files Browse the repository at this point in the history
  • Loading branch information
Georges committed Jan 23, 2015
1 parent 5b7c631 commit a90dc13
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 4 deletions.
9 changes: 9 additions & 0 deletions .travis.yml
@@ -0,0 +1,9 @@
language: node_js
node_js:
- "0.10"

before_script:
- npm install --dev

script:
- npm test
16 changes: 16 additions & 0 deletions README.md
@@ -0,0 +1,16 @@
# Express HTTPTables

##### Important Note

Do not use yet in production
This module won't probably be heavily maintained.
Fork at will or claim ownership !
[Same as this](https://github.com/SimpliField/express-httptables)

##### Module Status

[![NPM version](https://badge.fury.io/js/express-httptables.png)](https://npmjs.org/package/express-httptables) [![Build status](https://secure.travis-ci.org/SimpliField/express-httptables.png)](https://travis-ci.org/SimpliField/express-httptables) [![Dependency Status](https://david-dm.org/SimpliField/express-httptables.png)](https://david-dm.org/SimpliField/express-httptables) [![devDependency Status](https://david-dm.org/SimpliField/express-httptables/dev-status.png)](https://david-dm.org/SimpliField/express-httptables#info=devDependencies) [![Coverage Status](https://coveralls.io/repos/SimpliField/express-httptables/badge.svg)](https://coveralls.io/r/SimpliField/express-httptables) [![Code Climate](https://codeclimate.com/github/SimpliField/express-httptables.png)](https://codeclimate.com/github/SimpliField/express-httptables)

##### Description

Just a use possible use case of HTTPTables in an express app.
2 changes: 1 addition & 1 deletion lib/express-httptables.js
Expand Up @@ -16,7 +16,7 @@ function ExpressHTTPTablesMiddleware(options) {
var httptables = HTTPTables(options.rulesOptions);
var defaultErrorStatus = options.errorStatus || 403;
httptables.setAccessFieldFunction(function expressHeaderAccess(req, field) {
var _field = (field || "").toUpperCase();
var _field = field.toString().toUpperCase();
if(_field === 'URL') {
return req.url;
} else if(_field === 'METHOD') {
Expand Down
15 changes: 12 additions & 3 deletions package.json
Expand Up @@ -4,7 +4,9 @@
"description": "Httptables usecase for express",
"main": "index.js",
"scripts": {
"test": "mocha test"
"test": "mocha test",
"coveralls": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha --report lcovonly -- test/*.js -R spec -",
"cover": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha --report html -- test/*.js -R spec -t 5000"
},
"repository": {
"type": "git",
Expand All @@ -21,6 +23,13 @@
"httptables": "git+https://github.com/SimpliField/httptables.git"
},
"devDependencies": {
"express": "^4.11.1"
}
"coveralls": "^2.11.2",
"express": "^4.11.1",
"istanbul": "^0.3.5",
"mocha": "^2.1.0",
"supertest": "^0.15.0"
},
"files": [
"README.md"
]
}
124 changes: 124 additions & 0 deletions test/index.js
@@ -0,0 +1,124 @@
var http = require('supertest');
var express = require('express');
var HTTPTables = require('httptables');
var httptablesMiddle = require('../lib/express-httptables');
var app = null;
var server = null;

var OKMsg = 'OK';
var OKUrl = '/test';
var KOMsg = 'KO';
var KOUrl = '/blop';

var allRules = [{
policy : HTTPTables.policies.DROP,
conditions : {
'url' : OKUrl
}
}, {
policy : HTTPTables.policies.ACCEPT,
conditions : {
'url' : OKUrl
}
}];

var rulesToTestCustomAccessor = [{
policy : HTTPTables.policies.DROP,
conditions : {
'url' : OKUrl,
'Method' : 'GET',
'Accept-Encoding' : /.*/
}
}];

function setRulesOnProperty(prop, rules) {
return function (req, res, next) {
req[prop] = rules;
next();
};
}

function setTestRouteOnApp(app) {
app.get('/test', function(req, res, next) {
res.send(OKMsg);
});
app.get('/blop', function(req, res, next) {
res.send(OKMsg);
});
app.use(function (err, req, res, next) {
var httpCode = (res.statusCode !== 200) ? res.statusCode : 500;
res.send(KOMsg);
});
return app;
}

describe('Express/HTTPTables', function() {
beforeEach(function () {
app = express();
});

afterEach(function () {
try {
server.close();
} catch(err) {}
server = null;
app = null;
});


it('should let through non matching request by default', function (done) {
app.use(setRulesOnProperty('rules', [allRules[0]]));
app.use(httptablesMiddle());
app = setTestRouteOnApp(app);
server = app.listen();
http(app).get(KOUrl).expect(200, OKMsg, done);
});

it('should apply correct policy', function (done) {
app.use(setRulesOnProperty('rules', [allRules[0]]));
app.use(httptablesMiddle({rulesOptions : {defaultPolicy : HTTPTables.policies.DROP}}));
app = setTestRouteOnApp(app);
server = app.listen();
http(app).get(OKUrl).expect(403, KOMsg, done);
});

it('should have "rules" as the default property', function (done) {
app.use(setRulesOnProperty('rules', allRules));
app.use(httptablesMiddle());
app = setTestRouteOnApp(app);
server = app.listen();
http(app).get(OKUrl).expect(403, KOMsg, done);
});

it('should be possible to change the rules default property', function (done) {
app.use(setRulesOnProperty('_rules', allRules));
app.use(httptablesMiddle({rulesPropertyName : '_rules'}));
app = setTestRouteOnApp(app);
server = app.listen();
http(app).get(OKUrl).expect(403, KOMsg, done);
});

it('should be possible to change internal HttpTable instance', function (done) {
app.use(setRulesOnProperty('rules', [allRules[1]]));
app.use(httptablesMiddle({rulesOptions : {defaultPolicy : HTTPTables.policies.DROP}}));
app = setTestRouteOnApp(app);
server = app.listen();
http(app).get(KOUrl).expect(403, KOMsg, done);
});

it('should be able to change HTTP error code', function (done) {
app.use(setRulesOnProperty('rules', [allRules[0]]));
app.use(httptablesMiddle({errorStatus : 401}));
app = setTestRouteOnApp(app);
server = app.listen();
http(app).get(OKUrl).expect(401, KOMsg, done);
});

it('should be able to access all headers', function (done) {
app.use(setRulesOnProperty('rules', rulesToTestCustomAccessor));
app.use(httptablesMiddle());
app = setTestRouteOnApp(app);
server = app.listen();
http(app).get(OKUrl).expect(403, KOMsg, done);
});
});

0 comments on commit a90dc13

Please sign in to comment.