Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Georges
committed
Jan 23, 2015
1 parent
5b7c631
commit a90dc13
Showing
5 changed files
with
162 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
language: node_js | ||
node_js: | ||
- "0.10" | ||
|
||
before_script: | ||
- npm install --dev | ||
|
||
script: | ||
- npm test |
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,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. |
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
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,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); | ||
}); | ||
}); |