Skip to content

Instantly share code, notes, and snippets.

@jas-
Last active December 28, 2015 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jas-/7546619 to your computer and use it in GitHub Desktop.
Save jas-/7546619 to your computer and use it in GitHub Desktop.
superagent test case
var chai = require('chai'),
expect = chai.expect,
config = require('../config/settings'),
app = require('../libs/app'),
https = require('https'),
test = require('supertest')
describe('app.js', function(){
describe('routes', function(){
before(function(){
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
this.server = https.createServer(config.Pki, app).listen(config.App.port)
config.Pki.port = 3000
conn = tlsClient.connect(config.Pki)
})
after(function(done){
this.server.close(done)
done()
})
it('known missing route(s)', function(done){
test(app).get('/wtf').expect(404)
test(app).post('/wtf')
.send({foo: 'bar'})
.expect(403)
done()
})
})
describe('routes', function(){
before(function(){
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
this.server = https.createServer(config.Pki, app).listen(config.App.port)
config.Pki.port = 3000
conn = tlsClient.connect(config.Pki)
})
after(function(done){
this.server.close(done)
done()
})
it('unkown routes & payloads', function(done){
var max = 128,
i = 0,
url = '',
payload = {}
do {
url = genRandom()
payload[genRandom(128)] = genRandom(128)
test(app).get(url).expect(404)
test(app).post(url)
.send(payload)
.expect(403)
i++
} while (i < max)
done()
})
})
})
function genRandom(len) {
len = len || 16
var text = '',
charset = 'abcdefghijklmnopqrstuvwxyz0123456789'
for(var i=0; i < len; i++)
text += charset.charAt(Math.floor(Math.random() * charset.length))
return text
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment