Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function augmentVerbs() {
methods.forEach(function(method) {
var _fn = app[method];
app[method] = function(name, path) {
if ((method == 'get' && arguments.length == 1) ||
(typeof(path) != 'string' && !(path instanceof String)))
return _fn.apply(this, arguments);
if (app._namedRoutes && app._namedRoutes[name])
throw new Error('Route already defined: ' + name);
var args = Array.prototype.slice.call(arguments, 0);
args.shift();
var ret = _fn.apply(this, args);
if (!app._namedRoutes) app._namedRoutes = {};
app._namedRoutes[name] = (typeof this.route !== 'string')
? this.route(args[0]) // express 4.x
it('should support using .all to capture all http verbs', function(done){
var router = new Router();
var count = 0;
router.all('/foo', function(){ count++; });
var url = '/foo?bar=baz';
methods.forEach(function testMethod(method) {
router.handle({ url: url, method: method }, {}, function() {});
});
assert.equal(count, methods.length);
done();
})
it('should handle VERBS', function(done) {
var count = 0;
var route = new Route('/foo');
var cb = after(methods.length, function (err) {
if (err) return done(err);
count.should.equal(methods.length);
done();
});
route.all(function(req, res, next) {
count++;
next();
});
methods.forEach(function testMethod(method) {
var req = { method: method, url: '/' };
route.dispatch(req, {}, cb);
});
})
function wrap(factory, out) {
methods.forEach(function (method) {
out[method] = function () {
var test = factory[method].apply(this, arguments);
test.toPromise = toPromise;
test.then = then;
test.catch = _catch;
return test;
};
});
return out;
}
function rawrequest(server) {
var _headers = {}
var _method
var _path
var _test = {}
methods.forEach(function (method) {
_test[method] = go.bind(null, method)
})
function expect(status, body, callback) {
if (arguments.length === 2) {
_headers[status.toLowerCase()] = body
return this
}
var _server
if (!server.address()) {
_server = server.listen(0, onListening)
return
}
setup (routes) {
routes.call(this);
methods.forEach(method => {
this._compiledRoutes[method] = [];
});
this.routes.forEach(route => {
let [namespace, method] = route.target.split('#');
namespace = namespace.split('/');
let controller = namespace.pop();
namespace = namespace.join('/');
let target = route.target;
let path = route.path;
let keys = [];
let regexp = pathToRegexp(route.path, keys);
keys = keys.map(key => key.name);
if (arguments.length > 2) {
middleware = Array.prototype.slice.call(arguments, 1, arguments.length);
handle = middleware.pop();
middleware = utils.flatten(middleware);
}
var t = typeof handle;
if (t !== 'function') {
throw new TypeError('handle must be function, not ' + t);
}
localRoutes.push([utils.createRouter(urlpattern), handle, middleware]);
};
}
METHODS.forEach(function (method) {
methods[method] = createMethod(method);
});
methods.all = createMethod('all');
methods.redirect = function (urlpattern, to) {
if (!to || typeof to !== 'string') {
throw new TypeError(JSON.stringify(to) + ' must be string');
}
if (!routes.redirects) {
routes.redirects = [];
}
routes.redirects.push([utils.createRouter(urlpattern, true), function (req, res) {
redirect(res, to, 301);
}]);
};
import { agent } from 'supertest';
import { getError } from 'tcomb-pretty-match';
import methods from 'methods';
export default function Agent(app, server) {
if (!(this instanceof Agent)) {
return new Agent(app, server);
}
this.app = app;
this.agent = agent(server);
}
methods.forEach(method => {
Agent.prototype[method] = function(path, { query, params = {}, body } = {}) {
const route = findRoute(this.app, method, path);
if (!route) {
throw new Error(`Can't find route (${method.toUpperCase()} ${path})`);
}
const schema = route.schema;
if (!schema.response) {
return Promise.reject(new Error(`No Response Schema (${method.toUpperCase()} ${path})`));
}
const url = Object.keys(params).reduce(
(path, paramName) => path.replace(`:${paramName}`, encodeURIComponent(params[paramName])),
path
_applyMethods() {
const methodFn = methods => (path, opts, ...middlewares) => {
assert(typeof path === 'string' || Array.isArray(path), 'path must be a string or array.');
if (typeof opts === 'function') {
middlewares.unshift(opts);
opts = {};
}
this.register(path, methods, middlewares, opts);
return this;
};
allMethods.forEach((method) => {
this[method] = methodFn([method]);
});
this.del = this.delete;
this.all = methodFn(allMethods);
}
}
next();
};
route.use = function(method,handle_function){
//http 请求变大写
var layer = {'verb':method,'handler':handle_function};
this.stack.push(layer);
};
route.all = function(handle_function){
route.use("all",handle_function);
}
//定义route的各类方法
methods.forEach(function(method){
route[method] = function(handle_function){
route.use(method,handle_function);
return route;
};
});
//用于链式写法
return route;
}