Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var server = createServer(router)
router.all('/foo', saw)
request(server)
.get('/foo')
.expect(200, 'saw GET /foo', cb)
request(server)
.get('/foo/')
.expect(404, cb)
})
})
})
methods.slice().sort().forEach(function (method) {
if (method === 'connect') {
// CONNECT is tricky and supertest doesn't support it
return
}
var body = method !== 'head'
? 'hello, world'
: ''
describe('.' + method + '(path, ...fn)', function () {
it('should be chainable', function () {
var router = new Router()
assert.equal(router[method]('/', helloWorld), router)
})
it('should respond to a ' + method.toUpperCase() + ' request', function (done) {
var router = new Router()
var route = router.route('/foo')
var server = createServer(router)
route.all([[createHitHandle(1), createHitHandle(2)], createHitHandle(3)], helloWorld)
request(server)
.get('/foo')
.expect('x-fn-1', 'hit')
.expect('x-fn-2', 'hit')
.expect('x-fn-3', 'hit')
.expect(200, 'hello, world', done)
})
})
methods.slice().sort().forEach(function (method) {
if (method === 'connect') {
// CONNECT is tricky and supertest doesn't support it
return
}
var body = method !== 'head'
? 'hello, world'
: ''
describe('.' + method + '(...fn)', function () {
it('should respond to a ' + method.toUpperCase() + ' request', function (done) {
var router = new Router()
var route = router.route('/')
var server = createServer(router)
route[method](helloWorld)
res.statusCode = 500
res.end(err.message)
return
}
res.statusCode = req.headers['x-expect-method'] === req.method
? req.body.user === 'tobi'
? 201
: 400
: 405
res.end()
})
})
})
methods.slice().sort().forEach(function (method) {
if (method === 'connect') {
// except CONNECT
return
}
it('should support ' + method.toUpperCase() + ' requests', function (done) {
request(this.server)[method]('/')
.set('Content-Type', 'application/json')
.set('Content-Length', '15')
.set('X-Expect-Method', method.toUpperCase())
.send('{"user":"tobi"}')
.expect(201, done)
})
})
})
var assert = require('assert'),
express = require('express'),
request = require('supertest'),
methods = require('methods'),
AsyncRouter = require('../');
methods.filter(function(method) {
// todo: how do we test CONNECT requests?
return method !== 'connect';
}).concat('all').forEach(function(method) {
describe('router.' + method + '(path, fn)', function() {
it('should support async functions', function(done) {
var app = express(),
router = AsyncRouter();
router[method]('/', async function(req, res, next) {
res.send(method + ' successful');
});
app.use(router);
request(app)[method === 'all' ? 'get' : method]('/')
.end(function(err, res) {
// These aren't supposed to have any body
return false;
}
// header content is a string, and distinction between 0 and no information is crucial
if ('0' === res.headers['content-length']) {
// We know that the body is empty (unfortunately, this check does not cover chunked encoding)
return false;
}
// console.log(res);
return /^\s*(?:deflate|gzip)\s*$/.test(res.headers['content-encoding']);
};
// generate HTTP verb methods
if (methods.indexOf('del') == -1) {
// create a copy so we don't cause conflicts with
// other packages using the methods package and
// npm 3.x
methods = methods.slice(0);
methods.push('del');
}
methods.forEach(method => {
const name = method;
method = 'del' == method ? 'delete' : method;
method = method.toUpperCase();
request[name] = (url, data, fn) => {
const req = request(method, url);
if ('function' == typeof data) (fn = data), (data = null);
if (data) {
if (method === 'GET' || method === 'HEAD') {
if ('0' === res.headers['content-length']) {
// We know that the body is empty (unfortunately, this check does not cover chunked encoding)
return false;
}
// console.log(res);
return /^\s*(?:deflate|gzip)\s*$/.test(res.headers['content-encoding']);
};
// generate HTTP verb methods
if (methods.indexOf('del') == -1) {
// create a copy so we don't cause conflicts with
// other packages using the methods package and
// npm 3.x
methods = methods.slice(0);
methods.push('del');
}
methods.forEach(method => {
const name = method;
method = 'del' == method ? 'delete' : method;
method = method.toUpperCase();
request[name] = (url, data, fn) => {
const req = request(method, url);
if ('function' == typeof data) (fn = data), (data = null);
if (data) {
if (method === 'GET' || method === 'HEAD') {
req.query(data);
} else {
req.send(data);
}
}
if ('0' === res.headers['content-length']) {
// We know that the body is empty (unfortunately, this check does not cover chunked encoding)
return false;
}
// console.log(res);
return /^\s*(?:deflate|gzip)\s*$/.test(res.headers['content-encoding']);
};
// generate HTTP verb methods
if (methods.indexOf('del') == -1) {
// create a copy so we don't cause conflicts with
// other packages using the methods package and
// npm 3.x
methods = methods.slice(0);
methods.push('del');
}
methods.forEach(method => {
const name = method;
method = 'del' == method ? 'delete' : method;
method = method.toUpperCase();
request[name] = (url, data, fn) => {
const req = request(method, url);
if ('function' == typeof data) (fn = data), (data = null);
if (data) {
if (method === 'GET' || method === 'HEAD') {
req.query(data);
} else {
req.send(data);
}
}
if ('0' === res.headers['content-length']) {
// We know that the body is empty (unfortunately, this check does not cover chunked encoding)
return false;
}
// console.log(res);
return /^\s*(?:deflate|gzip)\s*$/.test(res.headers['content-encoding']);
};
// generate HTTP verb methods
if (methods.indexOf('del') == -1) {
// create a copy so we don't cause conflicts with
// other packages using the methods package and
// npm 3.x
methods = methods.slice(0);
methods.push('del');
}
methods.forEach(method => {
const name = method;
method = 'del' == method ? 'delete' : method;
method = method.toUpperCase();
request[name] = (url, data, fn) => {
const req = request(method, url);
if ('function' == typeof data) (fn = data), (data = null);
if (data) {
if (method === 'GET' || method === 'HEAD') {
req.query(data);
} else {
req.send(data);
}
}
// header content is a string, and distinction between 0 and no information is crucial
if ('0' === res.headers['content-length']) {
// We know that the body is empty (unfortunately, this check does not cover chunked encoding)
return false;
}
// console.log(res);
return /^\s*(?:deflate|gzip)\s*$/.test(res.headers['content-encoding']);
};
// generate HTTP verb methods
if (methods.indexOf('del') == -1) {
// create a copy so we don't cause conflicts with
// other packages using the methods package and
// npm 3.x
methods = methods.slice(0);
methods.push('del');
}
methods.forEach(method => {
const name = method;
method = 'del' == method ? 'delete' : method;
method = method.toUpperCase();
request[name] = (url, data, fn) => {
const req = request(method, url);
if ('function' == typeof data) (fn = data), (data = null);
if (data) {
if (method === 'GET' || method === 'HEAD') {
req.query(data);
} else {
req.send(data);
}
// header content is a string, and distinction between 0 and no information is crucial
if ('0' === res.headers['content-length']) {
// We know that the body is empty (unfortunately, this check does not cover chunked encoding)
return false;
}
// console.log(res);
return /^\s*(?:deflate|gzip)\s*$/.test(res.headers['content-encoding']);
};
// generate HTTP verb methods
if (methods.indexOf('del') == -1) {
// create a copy so we don't cause conflicts with
// other packages using the methods package and
// npm 3.x
methods = methods.slice(0);
methods.push('del');
}
methods.forEach(method => {
const name = method;
method = 'del' == method ? 'delete' : method;
method = method.toUpperCase();
request[name] = (url, data, fn) => {
const req = request(method, url);
if ('function' == typeof data) (fn = data), (data = null);
if (data) {
if (method === 'GET' || method === 'HEAD') {
req.query(data);
} else {
req.send(data);
}