UNPKG

1.01 kBJavaScriptView Raw
1'use strict';
2
3const methods = require('methods');
4const EggTest = require('./http_test');
5const mockHttpServer = require('./mock_http_server');
6const pkg = require('../package.json');
7
8// patch from https://github.com/visionmedia/supertest/blob/199506d8dbfe0bb1434fc07c38cdcd1ab4c7c926/index.js#L19
9
10/**
11 * Test against the given `app`,
12 * returning a new `Test`.
13 *
14 * @param {Application} app
15 * @return {Test}
16 * @public
17 */
18
19module.exports = app => {
20 const server = mockHttpServer(app);
21 const obj = {};
22 for (const method of methods) {
23 obj[method] = url => {
24 // support pathFor(url)
25 if (url[0] !== '/') {
26 const realUrl = app.router.pathFor(url);
27 if (!realUrl) throw new Error(`Can\'t find router:${url}, please check your \'app/router.js\'`);
28 url = realUrl;
29 }
30
31 const eggTest = new EggTest(server, method, url);
32 eggTest.set('user-agent', `egg-mock/${pkg.version}`);
33 return eggTest;
34 };
35 }
36 obj.del = obj.delete;
37 return obj;
38};