From 4e510b22836096a47d562dbd5ca8affd28f94f9e Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Tue, 13 Jun 2017 18:03:53 +0800 Subject: [PATCH] chore: use app.httpRequest() instead of supertest (#1041) --- docs/source/en/intro/quickstart.md | 5 ++- docs/source/en/tutorials/restful.md | 19 +++++------ docs/source/zh-cn/advanced/framework.md | 4 +-- docs/source/zh-cn/intro/quickstart.md | 5 ++- docs/source/zh-cn/tutorials/restful.md | 5 ++- test/app/extend/application.test.js | 5 ++- test/app/extend/context.jsonp.test.js | 7 ++-- test/app/extend/context.test.js | 33 +++++++++---------- test/app/extend/helper.test.js | 13 ++++---- test/app/extend/request.test.js | 15 ++++----- test/app/extend/response.test.js | 3 +- test/app/middleware/body_parser.test.js | 17 +++++----- test/app/middleware/meta.test.js | 3 +- test/app/middleware/notfound.test.js | 17 +++++----- test/async/_async.js | 3 +- .../apps/schedule/app/schedule/sub/cron.js | 4 +-- test/lib/agent.test.js | 5 ++- test/lib/application.test.js | 5 ++- test/lib/core/cookies.test.js | 25 +++++++------- test/lib/core/dnscache_httpclient.test.js | 13 ++++---- test/lib/core/httpclient_tracer_demo.test.js | 3 +- test/lib/core/loader/load_router.test.js | 5 ++- test/lib/core/loader/load_service.test.js | 7 ++-- test/lib/core/logger.test.js | 3 +- test/lib/core/router.test.js | 30 ++++++++--------- test/lib/core/view.test.js | 21 ++++++------ test/lib/egg.test.js | 13 ++++---- test/lib/plugins/development.test.js | 9 +++-- test/lib/plugins/i18n.test.js | 7 ++-- test/lib/plugins/multipart.test.js | 1 + test/lib/plugins/onerror.test.js | 3 +- test/lib/plugins/schedule.test.js | 5 +-- test/lib/plugins/session.test.js | 9 +++-- test/lib/plugins/static.test.js | 3 +- test/lib/plugins/watcher.test.js | 14 +++----- 35 files changed, 153 insertions(+), 186 deletions(-) diff --git a/docs/source/en/intro/quickstart.md b/docs/source/en/intro/quickstart.md index fd76e4c5ef..117bd81bf1 100644 --- a/docs/source/en/intro/quickstart.md +++ b/docs/source/en/intro/quickstart.md @@ -432,7 +432,6 @@ All the test files should place at `{app_root}/test/**/*.test.js`. // test/app/middleware/robot.test.js const assert = require('assert'); const mock = require('egg-mock'); -const request = require('supertest'); describe('test/app/middleware/robot.test.js', () => { let app; @@ -444,7 +443,7 @@ describe('test/app/middleware/robot.test.js', () => { afterEach(mock.restore); it('should block robot', () => { - return request(app.callback()) + return app.httpRequest() .get('/') .set('User-Agent', "Baiduspider") .expect(403); @@ -467,7 +466,7 @@ Then add `npm scripts`. Also install dependencies. ```bash -$ npm i egg-mock supertest --save-dev +$ npm i egg-mock --save-dev ``` Run it. diff --git a/docs/source/en/tutorials/restful.md b/docs/source/en/tutorials/restful.md index 6059301eb3..0ca04441ba 100644 --- a/docs/source/en/tutorials/restful.md +++ b/docs/source/en/tutorials/restful.md @@ -6,12 +6,12 @@ CNode currently use v1 interface is not fully consistent with the RESTful semant ## Response Formatting -Designing a RESTful-style API, we will identify the status of response by the response status code, keeping the response body simply and only the interface data is returned. +Designing a RESTful-style API, we will identify the status of response by the response status code, keeping the response body simply and only the interface data is returned. A example of `topics` is shown below: ### Get topics list -- `GET /api/v2/topics` +- `GET /api/v2/topics` - status code: 200 - response body: @@ -87,7 +87,7 @@ A example of `topics` is shown below: When an error is occurring, 4xx status code is returned if occurred by client-side request parameters and 5xx status code is returned if occurred by server-side logic processing. All error objects are used as the description for status exceptions. -For example, passing invalided parameters from the client may return a response with status code 422, the response body as shown below: +For example, passing invalided parameters from the client may return a response with status code 422, the response body as shown below: ```json { "error": "Validation Failed", @@ -201,7 +201,7 @@ module.exports = app => { this.ctx.throw(result.status, errorMsg); } if (!result.data.success) { - // remote response error + // remote response error this.ctx.throw(500, 'remote response error', { data: result.data }); } } @@ -252,7 +252,7 @@ module.exports = () => { }; ``` -We can catch all exceptions and follow the expected format to encapsulate the response through the middleware. It can be loaded into application using configuration file (`config/config.default.js`) +We can catch all exceptions and follow the expected format to encapsulate the response through the middleware. It can be loaded into application using configuration file (`config/config.default.js`) ```js // config/config.default.js @@ -275,7 +275,6 @@ Completing the coding just the first step, furthermore we need to add [Unit Test Let's start writing the unit test for the controller. We can simulate the implementation of the service layer in an appropriate way because the most important part is to test the logic as for controller. And mocking up the service layer according the convention of interface, so we can develop layered testing because the service layer itself can also covered by service unit test. ```js -const request = require('supertest'); const mock = require('egg-mock'); describe('test/app/controller/topics.test.js', () => { @@ -291,7 +290,7 @@ describe('test/app/controller/topics.test.js', () => { // test the response of passing the error parameters it('should POST /api/v2/topics/ 422', function* () { app.mockCsrf(); - yield request(app.callback()) + yield app.httpRequest() .post('/api/v2/topics') .send({ accesstoken: '123', @@ -307,7 +306,7 @@ describe('test/app/controller/topics.test.js', () => { it('should POST /api/v2/topics/ 201', function* () { app.mockCsrf(); app.mockService('topics', 'create', 123); - yield request(app.callback()) + yield app.httpRequest() .post('/api/v2/topics') .send({ accesstoken: '123', @@ -376,8 +375,8 @@ describe('test/app/service/topics.test.js', () => { }); }); ``` -In the testing of service layer above, we create a context object using the `app.createContext()` which provided by egg-mock and call the service method on context object to test directly. It can use `app.mockHttpclient()` to simulate the response of calling HTTP request, which allows us to focus on the logic testing of service layer without the impact of environment. +In the testing of service layer above, we create a context object using the `app.createContext()` which provided by egg-mock and call the service method on context object to test directly. It can use `app.mockHttpclient()` to simulate the response of calling HTTP request, which allows us to focus on the logic testing of service layer without the impact of environment. ------ -Details of code implementation and unit test are available in [eggjs/examples/cnode-api](https://github.com/eggjs/examples/tree/master/cnode-api) +Details of code implementation and unit test are available in [eggjs/examples/cnode-api](https://github.com/eggjs/examples/tree/master/cnode-api) diff --git a/docs/source/zh-cn/advanced/framework.md b/docs/source/zh-cn/advanced/framework.md index fa4bd803f0..e6200c6d31 100644 --- a/docs/source/zh-cn/advanced/framework.md +++ b/docs/source/zh-cn/advanced/framework.md @@ -280,7 +280,7 @@ describe('test/index.test.js', () => { afterEach(mock.restore); it('should success', () => { - return request(app.callback()) + return app.httpRequest() .get('/') .expect(200); }); @@ -344,7 +344,7 @@ describe('/test/index.test.js', () => { after(() => app.close()); afterEach(mock.restore); it('should success', () => { - return request(app.callback()) + return app.httpRequest() .get('/') .expect(200); }); diff --git a/docs/source/zh-cn/intro/quickstart.md b/docs/source/zh-cn/intro/quickstart.md index 820eb02d3b..b890481589 100644 --- a/docs/source/zh-cn/intro/quickstart.md +++ b/docs/source/zh-cn/intro/quickstart.md @@ -403,7 +403,6 @@ module.exports = app => { // test/app/middleware/robot.test.js const assert = require('assert'); const mock = require('egg-mock'); -const request = require('supertest'); describe('test/app/middleware/robot.test.js', () => { let app; @@ -417,7 +416,7 @@ describe('test/app/middleware/robot.test.js', () => { afterEach(mock.restore); it('should block robot', () => { - return request(app.callback()) + return app.httpRequest() .get('/') .set('User-Agent', "Baiduspider") .expect(403); @@ -438,7 +437,7 @@ describe('test/app/middleware/robot.test.js', () => { ``` ```bash -$ npm i egg-mock supertest --save-dev +$ npm i egg-mock --save-dev ``` 执行测试: diff --git a/docs/source/zh-cn/tutorials/restful.md b/docs/source/zh-cn/tutorials/restful.md index 7c9a4101a0..b37dc1bb6b 100644 --- a/docs/source/zh-cn/tutorials/restful.md +++ b/docs/source/zh-cn/tutorials/restful.md @@ -276,7 +276,6 @@ module.exports = { 我们先来编写 controller 代码的单元测试。在写 controller 单测的时候,我们可以适时的模拟 service 层的实现,因为对 controller 的单元测试而言,最重要的部分是测试自身的逻辑,而 service 层按照约定的接口 mock 掉,service 自身的逻辑可以让 service 的单元测试来覆盖,这样我们开发的时候也可以分层进行开发测试。 ```js -const request = require('supertest'); const mock = require('egg-mock'); describe('test/app/controller/topics.test.js', () => { @@ -292,7 +291,7 @@ describe('test/app/controller/topics.test.js', () => { // 测试请求参数错误时应用的响应 it('should POST /api/v2/topics/ 422', function* () { app.mockCsrf(); - yield request(app.callback()) + yield app.httpRequest() .post('/api/v2/topics') .send({ accesstoken: '123', @@ -308,7 +307,7 @@ describe('test/app/controller/topics.test.js', () => { it('should POST /api/v2/topics/ 201', function* () { app.mockCsrf(); app.mockService('topics', 'create', 123); - yield request(app.callback()) + yield app.httpRequest() .post('/api/v2/topics') .send({ accesstoken: '123', diff --git a/test/app/extend/application.test.js b/test/app/extend/application.test.js index 56f0b04bae..de4a0fdcdb 100644 --- a/test/app/extend/application.test.js +++ b/test/app/extend/application.test.js @@ -1,7 +1,6 @@ 'use strict'; const assert = require('assert'); -const request = require('supertest'); const sleep = require('mz-modules/sleep'); const fs = require('fs'); const path = require('path'); @@ -74,7 +73,7 @@ describe('test/app/extend/application.test.js', () => { after(() => app.close()); it('should app.locals is same ref', () => { - return request(app.callback()) + return app.httpRequest() .get('/app_same_ref') .expect('true'); }); @@ -133,7 +132,7 @@ describe('test/app/extend/application.test.js', () => { after(() => app.close()); it('should run background task success', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/app_background') .expect(200) .expect('hello app'); diff --git a/test/app/extend/context.jsonp.test.js b/test/app/extend/context.jsonp.test.js index a89aebde2f..2b5319b2e2 100644 --- a/test/app/extend/context.jsonp.test.js +++ b/test/app/extend/context.jsonp.test.js @@ -1,7 +1,6 @@ 'use strict'; const mm = require('egg-mock'); -const request = require('supertest'); const utils = require('../../utils'); describe('test/app/extend/context.jsonp.test.js', () => { @@ -14,7 +13,7 @@ describe('test/app/extend/context.jsonp.test.js', () => { afterEach(mm.restore); it('should response jsonp', () => { - return request(app.callback()) + return app.httpRequest() .get('/user.json?_callback=$jQuery110208780175377614796_1406016639408&ctoken=123') .set('Cookie', 'ctoken=123') .expect('Content-Type', 'application/javascript; charset=utf-8') @@ -24,7 +23,7 @@ describe('test/app/extend/context.jsonp.test.js', () => { }); it('should response json body when callback empty', () => { - return request(app.callback()) + return app.httpRequest() .get('/user.json?_callback=&ctoken=123') .set('Cookie', 'ctoken=123') .expect('Content-Type', 'application/json; charset=utf-8') @@ -33,7 +32,7 @@ describe('test/app/extend/context.jsonp.test.js', () => { }); it('should response json body when callback missing', () => { - return request(app.callback()) + return app.httpRequest() .get('/user.json?callback=&ctoken=123') .set('Cookie', 'ctoken=123') .expect('Content-Type', 'application/json; charset=utf-8') diff --git a/test/app/extend/context.test.js b/test/app/extend/context.test.js index b321483eea..e239459171 100644 --- a/test/app/extend/context.test.js +++ b/test/app/extend/context.test.js @@ -3,7 +3,6 @@ const fs = require('fs'); const path = require('path'); const mm = require('egg-mock'); -const request = require('supertest'); const sleep = require('mz-modules/sleep'); const assert = require('assert'); const utils = require('../../utils'); @@ -22,7 +21,7 @@ describe('test/app/extend/context.test.js', () => { yield app.ready(); const logdir = app.config.logger.dir; - yield request(app.callback()) + yield app.httpRequest() .get('/logger?message=foo') .expect('logger'); @@ -54,7 +53,7 @@ describe('test/app/extend/context.test.js', () => { userId: '123123', }); - yield request(app.callback()) + yield app.httpRequest() .get('/logger?message=foo') .expect('logger'); @@ -83,7 +82,7 @@ describe('test/app/extend/context.test.js', () => { yield app.ready(); const logdir = app.config.logger.dir; - yield request(app.callback()) + yield app.httpRequest() .get('/logger?message=foo') .expect('logger'); @@ -114,13 +113,13 @@ describe('test/app/extend/context.test.js', () => { after(() => app.close()); it('should return null when logger is not found', () => { - return request(app.callback()) + return app.httpRequest() .get('/noExistLogger') .expect('null'); }); it('should log with padding message', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/logger') .expect(200); @@ -141,7 +140,7 @@ describe('test/app/extend/context.test.js', () => { after(() => app.close()); it('should log with custom logger', () => { - return request(app.callback()) + return app.httpRequest() .get('/') .expect('work, logger: exists'); }); @@ -157,7 +156,7 @@ describe('test/app/extend/context.test.js', () => { describe('ctx.router', () => { it('should work', () => { - return request(app.callback()) + return app.httpRequest() .get('/') .expect(200) .expect('{"path":"/","foo":1,"bar":2}'); @@ -174,13 +173,13 @@ describe('test/app/extend/context.test.js', () => { after(() => app.close()); it('should same this.locals ref on every request ', () => { - return request(app.callback()) + return app.httpRequest() .get('/ctx_same_ref') .expect('true'); }); it('should this.locals merge app.locals data', () => { - return request(app.callback()) + return app.httpRequest() .get('/ctx_merge_app') .expect({ a: 1, @@ -189,7 +188,7 @@ describe('test/app/extend/context.test.js', () => { }); it('should this.locals cover app.locals data', () => { - return request(app.callback()) + return app.httpRequest() .get('/ctx_override_app') .expect({ a: 'ctx.a', @@ -198,7 +197,7 @@ describe('test/app/extend/context.test.js', () => { }); it('should not change this.locals data when app.locals change again', () => { - return request(app.callback()) + return app.httpRequest() .get('/ctx_app_update_can_not_affect_ctx') .expect({ a: 'app.a', @@ -208,7 +207,7 @@ describe('test/app/extend/context.test.js', () => { }); it('should locals only support object format', () => { - return request(app.callback()) + return app.httpRequest() .get('/set_only_support_object') .expect({ 'ctx.locals.object': true, @@ -234,7 +233,7 @@ describe('test/app/extend/context.test.js', () => { after(() => app.close()); it('should run background task success', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/') .expect(200) .expect('hello'); @@ -249,7 +248,7 @@ describe('test/app/extend/context.test.js', () => { it('should run background task error', function* () { mm.consoleLevel('NONE'); - yield request(app.callback()) + yield app.httpRequest() .get('/error') .expect(200) .expect('hello error'); @@ -322,7 +321,7 @@ describe('test/app/extend/context.test.js', () => { describe('ctx.ip', () => { it('should get current request ip', () => { - return request(app.callback()) + return app.httpRequest() .get('/ip') .expect(200) .expect({ @@ -331,7 +330,7 @@ describe('test/app/extend/context.test.js', () => { }); it('should set current request ip', () => { - return request(app.callback()) + return app.httpRequest() .get('/ip?set_ip=10.2.2.2') .expect(200) .expect({ diff --git a/test/app/extend/helper.test.js b/test/app/extend/helper.test.js index 2d3894fcc9..5a8d4e9412 100644 --- a/test/app/extend/helper.test.js +++ b/test/app/extend/helper.test.js @@ -1,6 +1,5 @@ 'use strict'; -const request = require('supertest'); const utils = require('../../utils'); describe('test/app/extend/helper.test.js', () => { @@ -13,14 +12,14 @@ describe('test/app/extend/helper.test.js', () => { describe('pathFor()', () => { it('should get home path url', () => { - return request(app.callback()) + return app.httpRequest() .get('/pathFor') .expect('/home') .expect(200); }); it('should get home path with params', () => { - return request(app.callback()) + return app.httpRequest() .get('/pathFor?foo=bar') .expect('/home?foo=bar') .expect(200); @@ -29,14 +28,14 @@ describe('test/app/extend/helper.test.js', () => { describe('urlFor()', () => { it('should get full home url', () => { - return request(app.callback()) + return app.httpRequest() .get('/urlFor') .expect(/^http:\/\/127\.0\.0\.1:\d+\/home$/) .expect(200); }); it('should get full home url with params', () => { - return request(app.callback()) + return app.httpRequest() .get('/urlFor?foo=1') .expect(/^http:\/\/127\.0\.0\.1:\d+\/home\?foo=1$/) .expect(200); @@ -46,7 +45,7 @@ describe('test/app/extend/helper.test.js', () => { describe('escape()', () => { it('should escape script', () => { - return request(app.callback()) + return app.httpRequest() .get('/escape') .expect('<script>') .expect(200); @@ -55,7 +54,7 @@ describe('test/app/extend/helper.test.js', () => { describe('shtml()', () => { it('should ignore attribute if domain not in domainWhiteList', () => { - return request(app.callback()) + return app.httpRequest() .get('/shtml-not-in-domain-whitelist') .expect('true') .expect(200); diff --git a/test/app/extend/request.test.js b/test/app/extend/request.test.js index c4b9ca5ed0..0ad60fa03b 100644 --- a/test/app/extend/request.test.js +++ b/test/app/extend/request.test.js @@ -3,7 +3,6 @@ const assert = require('assert'); const mm = require('egg-mock'); const urllib = require('urllib'); -const request = require('supertest'); const utils = require('../../utils'); describe('test/app/extend/request.test.js', () => { @@ -97,14 +96,14 @@ describe('test/app/extend/request.test.js', () => { describe('req.protocol', () => { it('should return http when it not config and no protocol header', () => { mm(app.config, 'protocol', null); - return request(app.callback()) + return app.httpRequest() .get('/protocol') .expect('http'); }); it('should return value of X-Custom-Proto', () => { mm(app.config, 'protocolHeaders', 'X-Forwarded-Proto, X-Custom-Proto'); - return request(app.callback()) + return app.httpRequest() .get('/protocol') .set('X-Custom-Proto', 'https') .expect('https'); @@ -112,14 +111,14 @@ describe('test/app/extend/request.test.js', () => { it('should ignore X-Client-Scheme', () => { mm(app.config, 'protocolHeaders', 'X-Forwarded-Proto'); - return request(app.callback()) + return app.httpRequest() .get('/protocol') .set('X-Client-Scheme', 'https') .expect('http'); }); it('should return value of X-Forwarded-Proto', () => { - return request(app.callback()) + return app.httpRequest() .get('/protocol') .set('x-forwarded-proto', 'https') .expect('https'); @@ -127,7 +126,7 @@ describe('test/app/extend/request.test.js', () => { it('should ignore X-Forwarded-Proto when proxy=false', () => { mm(app.config, 'proxy', false); - return request(app.callback()) + return app.httpRequest() .get('/protocol') .set('x-forwarded-proto', 'https') .expect('http'); @@ -135,7 +134,7 @@ describe('test/app/extend/request.test.js', () => { it('should ignore X-Forwarded-Proto', () => { mm(app.config, 'protocolHeaders', ''); - return request(app.callback()) + return app.httpRequest() .get('/protocol') .set('x-forwarded-proto', 'https') .expect('http'); @@ -143,7 +142,7 @@ describe('test/app/extend/request.test.js', () => { it('should return value from config', () => { mm(app.config, 'protocol', 'https'); - return request(app.callback()) + return app.httpRequest() .get('/protocol') .expect('https'); }); diff --git a/test/app/extend/response.test.js b/test/app/extend/response.test.js index 56fe59d0e5..2c5ef3174a 100644 --- a/test/app/extend/response.test.js +++ b/test/app/extend/response.test.js @@ -1,6 +1,5 @@ 'use strict'; -const request = require('supertest'); const assert = require('assert'); const mm = require('egg-mock'); const utils = require('../../utils'); @@ -17,7 +16,7 @@ describe('test/app/extend/response.test.js', () => { after(() => app.close()); it('should get lower case header', () => { - return request(app.callback()) + return app.httpRequest() .get('/') .expect(200) .expect(res => { diff --git a/test/app/middleware/body_parser.test.js b/test/app/middleware/body_parser.test.js index 866eeeca61..80886149f0 100644 --- a/test/app/middleware/body_parser.test.js +++ b/test/app/middleware/body_parser.test.js @@ -2,7 +2,6 @@ const assert = require('assert'); const querystring = require('querystring'); -const request = require('supertest'); const utils = require('../../utils'); describe('test/app/middleware/body_parser.test.js', () => { @@ -13,7 +12,7 @@ describe('test/app/middleware/body_parser.test.js', () => { before(done => { app = utils.app('apps/body_parser_testapp'); app.ready(() => { - request(app.callback()) + app.httpRequest() .get('/test/body_parser/user') .expect(200, (err, res) => { assert(!err); @@ -29,7 +28,7 @@ describe('test/app/middleware/body_parser.test.js', () => { afterEach(() => app1 && app1.close()); it('should 200 when post form body below the limit', done => { - request(app.callback()) + app.httpRequest() .post('/test/body_parser/user') .set('Cookie', cookies) .set('Content-Type', 'application/x-www-form-urlencoded') @@ -41,7 +40,7 @@ describe('test/app/middleware/body_parser.test.js', () => { }); it('should 200 when post json body below the limit', done => { - request(app.callback()) + app.httpRequest() .post('/test/body_parser/user') .set('Cookie', cookies) .set('Content-Type', 'application/json') @@ -54,7 +53,7 @@ describe('test/app/middleware/body_parser.test.js', () => { app1 = utils.app('apps/body_parser_testapp_disable'); yield app1.ready(); - yield request(app1.callback()) + yield app1.httpRequest() .post('/test/body_parser/foo.json') .send({ foo: 'bar', ']': 'toString' }) .expect(204); @@ -64,12 +63,12 @@ describe('test/app/middleware/body_parser.test.js', () => { app1 = utils.app('apps/body_parser_testapp_ignore'); yield app1.ready(); - yield request(app1.callback()) + yield app1.httpRequest() .post('/test/body_parser/foo.json') .send({ foo: 'bar', ']': 'toString' }) .expect(204); - yield request(app1.callback()) + yield app1.httpRequest() .post('/test/body_parser/form.json') .set('Content-Type', 'application/x-www-form-urlencoded') .send({ foo: 'bar', ']': 'toString' }) @@ -80,12 +79,12 @@ describe('test/app/middleware/body_parser.test.js', () => { app1 = utils.app('apps/body_parser_testapp_match'); yield app1.ready(); - yield request(app1.callback()) + yield app1.httpRequest() .post('/test/body_parser/foo.json') .send({ foo: 'bar', ']': 'toString' }) .expect({ foo: 'bar', ']': 'toString' }); - yield request(app1.callback()) + yield app1.httpRequest() .post('/test/body_parser/form.json') .set('Content-Type', 'application/x-www-form-urlencoded') .send({ foo: 'bar', ']': 'toString' }) diff --git a/test/app/middleware/meta.test.js b/test/app/middleware/meta.test.js index d2f23d5fc1..28f330acde 100644 --- a/test/app/middleware/meta.test.js +++ b/test/app/middleware/meta.test.js @@ -1,7 +1,6 @@ 'use strict'; const mm = require('egg-mock'); -const request = require('supertest'); const utils = require('../../utils'); describe('test/app/middleware/meta.test.js', () => { @@ -16,7 +15,7 @@ describe('test/app/middleware/meta.test.js', () => { afterEach(mm.restore); it('should get X-Readtime header', () => { - return request(app.callback()) + return app.httpRequest() .get('/') .expect('X-Readtime', /\d+/) .expect(200); diff --git a/test/app/middleware/notfound.test.js b/test/app/middleware/notfound.test.js index e5182d29e8..d32c73c48c 100644 --- a/test/app/middleware/notfound.test.js +++ b/test/app/middleware/notfound.test.js @@ -1,6 +1,5 @@ 'use strict'; -const request = require('supertest'); const mm = require('egg-mock'); const utils = require('../../utils'); @@ -15,7 +14,7 @@ describe('test/app/middleware/notfound.test.js', () => { afterEach(mm.restore); it('should 302 redirect to 404.html', () => { - return request(app.callback()) + return app.httpRequest() .get('/test/404') .set('Accept', 'test/html') .expect('Location', 'https://eggjs.org/404') @@ -23,7 +22,7 @@ describe('test/app/middleware/notfound.test.js', () => { }); it('should 404 json response', () => { - return request(app.callback()) + return app.httpRequest() .get('/test/404.json?ctoken=404') .set('Cookie', 'ctoken=404') .expect({ @@ -33,7 +32,7 @@ describe('test/app/middleware/notfound.test.js', () => { }); it('should 404 json response on rest api', () => { - return request(app.callback()) + return app.httpRequest() .get('/api/404.json?ctoken=404') .set('Cookie', 'ctoken=404') .expect({ @@ -44,7 +43,7 @@ describe('test/app/middleware/notfound.test.js', () => { it('should show 404 page content when antx notfound.pageUrl not set', () => { mm(app.config.notfound, 'pageUrl', ''); - return request(app.callback()) + return app.httpRequest() .get('/foo') .expect('

404 Not Found

') .expect(404); @@ -61,13 +60,13 @@ describe('test/app/middleware/notfound.test.js', () => { afterEach(mm.restore); it('should 302 redirect to custom /404 when required html', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/test/404') .set('Accept', 'test/html') .expect('Location', '/404') .expect(302); - yield request(app.callback()) + yield app.httpRequest() .get('/404') .expect('Hi, this is 404') .expect(200); @@ -76,13 +75,13 @@ describe('test/app/middleware/notfound.test.js', () => { it('should not avoid circular redirects', function* () { mm(app.config.notfound, 'pageUrl', '/notfound'); - yield request(app.callback()) + yield app.httpRequest() .get('/test/404') .set('Accept', 'test/html') .expect('Location', '/notfound') .expect(302); - yield request(app.callback()) + yield app.httpRequest() .get('/notfound') .expect('

404 Not Found

config.notfound.pageUrl(/notfound)
is unimplemented

') .expect(404); diff --git a/test/async/_async.js b/test/async/_async.js index ef60969e21..4405a08bef 100644 --- a/test/async/_async.js +++ b/test/async/_async.js @@ -1,7 +1,6 @@ 'use strict'; const assert = require('assert'); -const request = require('supertest'); const mm = require('egg-mock'); const utils = require('../utils'); @@ -20,7 +19,7 @@ describe('test/async.test.js', () => { }); it('middleware, controller and service support async functions', async () => { - await request(app.callback()) + await app.httpRequest() .get('/api') .expect(200) .expect([ 'service', 'controller', 'router', 'middleware' ]); diff --git a/test/fixtures/apps/schedule/app/schedule/sub/cron.js b/test/fixtures/apps/schedule/app/schedule/sub/cron.js index 7965e22e75..04f0b1e35b 100644 --- a/test/fixtures/apps/schedule/app/schedule/sub/cron.js +++ b/test/fixtures/apps/schedule/app/schedule/sub/cron.js @@ -5,6 +5,6 @@ exports.schedule = { cron: '*/5 * * * * *', }; -exports.task = function*(ctx) { - ctx.logger.info('cron'); +exports.task = function* (ctx) { + ctx.logger.warn('cron'); }; diff --git a/test/lib/agent.test.js b/test/lib/agent.test.js index 85d83235f7..4ceb1e8368 100644 --- a/test/lib/agent.test.js +++ b/test/lib/agent.test.js @@ -3,7 +3,6 @@ const assert = require('assert'); const fs = require('fs'); const path = require('path'); -const request = require('supertest'); const execSync = require('child_process').execSync; const mm = require('egg-mock'); const utils = require('../utils'); @@ -21,7 +20,7 @@ describe('test/lib/agent.test.js', () => { after(() => app.close()); it('should catch exeption', done => { - request(app.callback()) + app.httpRequest() .get('/agent-throw') .expect(200, err => { assert(!err); @@ -35,7 +34,7 @@ describe('test/lib/agent.test.js', () => { }); it('should catch uncaughtException string error', done => { - request(app.callback()) + app.httpRequest() .get('/agent-throw-string') .expect(200, err => { assert(!err); diff --git a/test/lib/application.test.js b/test/lib/application.test.js index dac1a18a90..8a1c454683 100644 --- a/test/lib/application.test.js +++ b/test/lib/application.test.js @@ -2,7 +2,6 @@ const assert = require('assert'); const mm = require('egg-mock'); -const request = require('supertest'); const sleep = require('mz-modules/sleep'); const fs = require('fs'); const path = require('path'); @@ -122,7 +121,7 @@ describe('test/lib/application.test.js', () => { after(() => app.close()); it('should handle uncaughtException and log it', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/throw') .expect('foo') .expect(200); @@ -194,7 +193,7 @@ describe('test/lib/application.test.js', () => { describe('class style controller', () => { it('should work with class style controller', () => { - return request(app.callback()) + return app.httpRequest() .get('/class-controller') .expect('this is bar!') .expect(200); diff --git a/test/lib/core/cookies.test.js b/test/lib/core/cookies.test.js index 8b9740081b..591313fed8 100644 --- a/test/lib/core/cookies.test.js +++ b/test/lib/core/cookies.test.js @@ -2,7 +2,6 @@ const assert = require('assert'); const mm = require('egg-mock'); -const request = require('supertest'); const utils = require('../../utils'); const fs = require('fs'); const path = require('path'); @@ -70,7 +69,7 @@ describe('test/lib/core/cookies.test.js', () => { }); it('should not set secure when request protocol is http', done => { - request(app.callback()) + app.httpRequest() .get('/?setCookieValue=foobar') .set('Host', 'demo.eggjs.org') .set('X-Forwarded-Proto', 'http') @@ -85,7 +84,7 @@ describe('test/lib/core/cookies.test.js', () => { }); it('should set secure:true and httponly cookie', done => { - request(app.callback()) + app.httpRequest() .get('/?setCookieValue=foobar') .set('Host', 'demo.eggjs.org') .set('X-Forwarded-Proto', 'https') @@ -100,7 +99,7 @@ describe('test/lib/core/cookies.test.js', () => { }); it('should set cookie with path: /cookiepath/ok', done => { - request(app.callback()) + app.httpRequest() .get('/?cookiepath=/cookiepath/ok') .set('Host', 'demo.eggjs.org') .set('X-Forwarded-Proto', 'https') @@ -115,7 +114,7 @@ describe('test/lib/core/cookies.test.js', () => { }); it('should delete cookie', done => { - request(app.callback()) + app.httpRequest() .get('/?cookiedel=true') .set('Host', 'demo.eggjs.org') .set('Cookie', 'cookiedel=true') @@ -133,7 +132,7 @@ describe('test/lib/core/cookies.test.js', () => { }); it('should delete cookie with options', done => { - request(app.callback()) + app.httpRequest() .get('/?cookiedel=true&opts=true') .set('Host', 'demo.eggjs.org') .set('Cookie', 'cookiedel=true; path=/hello; domain=eggjs.org; expires=30') @@ -150,7 +149,7 @@ describe('test/lib/core/cookies.test.js', () => { }); it('should set cookie with domain: okcookie.eggjs.org', done => { - request(app.callback()) + app.httpRequest() .get('/?cookiedomain=okcookie.eggjs.org&cookiepath=/') .set('Host', 'demo.eggjs.org') .set('X-Forwarded-Proto', 'https') @@ -165,7 +164,7 @@ describe('test/lib/core/cookies.test.js', () => { }); it('should not set domain and path', done => { - request(app.callback()) + app.httpRequest() .get('/?notSetPath=okok') .set('Host', 'demo.eggjs.org') .set('X-Forwarded-Proto', 'https') @@ -189,7 +188,7 @@ describe('test/lib/core/cookies.test.js', () => { after(() => app.close()); it('should set secure:false cookie', done => { - request(app.callback()) + app.httpRequest() .get('/hello') .set('Host', 'demo.eggjs.org') .expect('hello') @@ -213,7 +212,7 @@ describe('test/lib/core/cookies.test.js', () => { after(() => app.close()); it('should get encrypt cookie', done => { - request(app.callback()) + app.httpRequest() .get('/') .expect({ set: 'bar 中文', @@ -228,7 +227,7 @@ describe('test/lib/core/cookies.test.js', () => { assert(plainCookie); assert.equal(plainCookie, 'plain=text ok; path=/; httponly'); - request(app.callback()) + app.httpRequest() .get('/') .set('Cookie', res.headers['set-cookie'].join(';')) .expect({ @@ -242,7 +241,7 @@ describe('test/lib/core/cookies.test.js', () => { }); it('should decode encrypt value fail', done => { - request(app.callback()) + app.httpRequest() .get('/') .expect({ set: 'bar 中文', @@ -253,7 +252,7 @@ describe('test/lib/core/cookies.test.js', () => { assert(encryptCookie); assert.equal(encryptCookie, 'foo=B9om8kiaZ7Xg9dzTUoH-Pw==; path=/; httponly'); - request(app.callback()) + app.httpRequest() .get('/') .set('Cookie', 'foo=123123; plain=text ok') .expect({ diff --git a/test/lib/core/dnscache_httpclient.test.js b/test/lib/core/dnscache_httpclient.test.js index 1ea120ac5a..5c90e068b2 100644 --- a/test/lib/core/dnscache_httpclient.test.js +++ b/test/lib/core/dnscache_httpclient.test.js @@ -1,7 +1,6 @@ 'use strict'; const mm = require('egg-mock'); -const request = require('supertest'); const assert = require('assert'); const dns = require('dns'); const urlparse = require('url').parse; @@ -23,36 +22,36 @@ describe('test/lib/core/dnscache_httpclient.test.js', () => { afterEach(mm.restore); it('should ctx.curl work and set host', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/?url=' + encodeURIComponent(url + '/get_headers')) .expect(200) .expect(/"host":"localhost:\d+"/); - yield request(app.callback()) + yield app.httpRequest() .get('/?url=' + encodeURIComponent(url + '/get_headers') + '&host=localhost.foo.com') .expect(200) .expect(/"host":"localhost\.foo\.com"/); - yield request(app.callback()) + yield app.httpRequest() .get('/?url=' + encodeURIComponent(url + '/get_headers') + '&Host=localhost2.foo.com') .expect(200) .expect(/"host":"localhost2\.foo\.com"/); }); it('should throw error when the first dns lookup fail', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/?url=' + encodeURIComponent('http://notexists-1111111local-domain.com')) .expect(500) .expect(/getaddrinfo ENOTFOUND notexists-1111111local-domain\.com/); }); it('should use local cache dns result when dns lookup error', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/?url=' + encodeURIComponent(url + '/get_headers')) .expect(200) .expect(/"host":"localhost:\d+"/); // mock local cache expires and mock dns lookup throw error app.httpclient.dnsCache.get('localhost').timestamp = 0; mm.error(dns, 'lookup', 'mock dns lookup error'); - yield request(app.callback()) + yield app.httpRequest() .get('/?url=' + encodeURIComponent(url + '/get_headers')) .expect(200) .expect(/"host":"localhost:\d+"/); diff --git a/test/lib/core/httpclient_tracer_demo.test.js b/test/lib/core/httpclient_tracer_demo.test.js index 11d70f679b..5cedc99361 100644 --- a/test/lib/core/httpclient_tracer_demo.test.js +++ b/test/lib/core/httpclient_tracer_demo.test.js @@ -1,7 +1,6 @@ 'use strict'; const assert = require('assert'); -const request = require('supertest'); const utils = require('../../utils'); describe('test/lib/core/httpclient_tracer_demo.test.js', () => { @@ -27,7 +26,7 @@ describe('test/lib/core/httpclient_tracer_demo.test.js', () => { }); it('should work with context httpclient', () => { - return request(app.callback()) + return app.httpRequest() .get('/?url=' + encodeURIComponent(url + '/get_headers')) .expect(res => { assert(res.body.url === url + '/get_headers'); diff --git a/test/lib/core/loader/load_router.test.js b/test/lib/core/loader/load_router.test.js index 19fabf8e3b..b96b340a06 100644 --- a/test/lib/core/loader/load_router.test.js +++ b/test/lib/core/loader/load_router.test.js @@ -1,6 +1,5 @@ 'use strict'; -const request = require('supertest'); const pedding = require('pedding'); const utils = require('../../../utils'); @@ -14,12 +13,12 @@ describe('test/lib/core/loader/load_router.test.js', () => { it('should load app/router.js', done => { done = pedding(2, done); - request(app.callback()) + app.httpRequest() .get('/') .expect(200) .expect('hello', done); - request(app.callback()) + app.httpRequest() .get('/home') .expect(200) .expect('hello', done); diff --git a/test/lib/core/loader/load_service.test.js b/test/lib/core/loader/load_service.test.js index b4f6025b53..90d51e0e37 100644 --- a/test/lib/core/loader/load_service.test.js +++ b/test/lib/core/loader/load_service.test.js @@ -1,7 +1,6 @@ 'use strict'; const assert = require('assert'); -const request = require('supertest'); const mm = require('egg-mock'); const utils = require('../../../utils'); @@ -26,7 +25,7 @@ describe('test/lib/core/loader/load_service.test.js', () => { assert(ctx.service.bar2); assert(ctx.service.foo4); - yield request(app.callback()) + yield app.httpRequest() .get('/') .expect({ foo2: 'foo2', @@ -48,7 +47,7 @@ describe('test/lib/core/loader/load_service.test.js', () => { app = utils.app('apps/service-app'); yield app.ready(); - yield request(app.callback()) + yield app.httpRequest() .get('/user') .expect(res => { assert(res.body.user); @@ -66,7 +65,7 @@ describe('test/lib/core/loader/load_service.test.js', () => { app = utils.app('apps/subdir-services'); yield app.ready(); - yield request(app.callback()) + yield app.httpRequest() .get('/') .expect({ user: { diff --git a/test/lib/core/logger.test.js b/test/lib/core/logger.test.js index efbc03c1c1..674f60bdd1 100644 --- a/test/lib/core/logger.test.js +++ b/test/lib/core/logger.test.js @@ -4,7 +4,6 @@ const assert = require('assert'); const path = require('path'); const fs = require('fs'); const mm = require('egg-mock'); -const request = require('supertest'); const Logger = require('egg-logger'); const sleep = require('mz-modules/sleep'); const utils = require('../../utils'); @@ -211,7 +210,7 @@ describe('test/lib/core/logger.test.js', () => { after(() => app.close()); it('should save debug log to file', done => { - request(app.callback()) + app.httpRequest() .get('/') .expect('ok') .end(err => { diff --git a/test/lib/core/router.test.js b/test/lib/core/router.test.js index 9b9bd23842..1124a6c4a7 100644 --- a/test/lib/core/router.test.js +++ b/test/lib/core/router.test.js @@ -1,8 +1,6 @@ 'use strict'; const assert = require('assert'); - -const request = require('supertest'); const mm = require('egg-mock'); const utils = require('../../utils'); @@ -21,49 +19,49 @@ describe('test/lib/core/router.test.js', () => { describe('router.resources', () => { describe('normal', () => { it('should GET /posts', () => { - return request(app.callback()) + return app.httpRequest() .get('/posts') .expect(200) .expect('index'); }); it('should GET /posts/new', () => { - return request(app.callback()) + return app.httpRequest() .get('/posts/new') .expect(200) .expect('new'); }); it('should POST /posts', () => { - return request(app.callback()) + return app.httpRequest() .post('/posts') .expect(200) .expect('create'); }); it('should GET /posts/:id', () => { - return request(app.callback()) + return app.httpRequest() .get('/posts/123') .expect(200) .expect('show - 123'); }); it('should GET /posts/:id/edit', () => { - return request(app.callback()) + return app.httpRequest() .get('/posts/123/edit') .expect(200) .expect('edit - 123'); }); it('should PUT /posts/:id', () => { - return request(app.callback()) + return app.httpRequest() .put('/posts/123') .expect(200) .expect('update - 123'); }); it('should DELETE /posts/:id', () => { - return request(app.callback()) + return app.httpRequest() .delete('/posts/123') .expect(200) .expect('destroy - 123'); @@ -72,47 +70,47 @@ describe('test/lib/core/router.test.js', () => { describe('controller url', () => { it('should GET /members', () => { - return request(app.callback()) + return app.httpRequest() .get('/members') .expect(200) .expect('index'); }); it('should GET /members/index', () => { - return request(app.callback()) + return app.httpRequest() .get('/members/index') .expect(200) .expect('index'); }); it('should GET /members/new', () => { - return request(app.callback()) + return app.httpRequest() .get('/members/new') .expect(200) .expect('new'); }); it('should GET /members/:id', () => { - return request(app.callback()) + return app.httpRequest() .get('/members/1231') .expect(200) .expect('show - 1231'); }); it('should POST /members', () => { - return request(app.callback()) + return app.httpRequest() .post('/members') .expect(404); }); it('should PUT /members/:id', () => { - return request(app.callback()) + return app.httpRequest() .put('/members/1231') .expect(404); }); it('should GET /POSTS', () => { - return request(app.callback()) + return app.httpRequest() .get('/POSTS') .expect(404); }); diff --git a/test/lib/core/view.test.js b/test/lib/core/view.test.js index fce6ba06b5..5c5746efcd 100644 --- a/test/lib/core/view.test.js +++ b/test/lib/core/view.test.js @@ -2,7 +2,6 @@ const assert = require('assert'); const path = require('path'); -const request = require('supertest'); const mock = require('egg-mock'); const utils = require('../../utils'); @@ -31,7 +30,7 @@ describe('test/lib/core/view.test.js', () => { describe('render', () => { it('should render ejs', function* () { - const res = yield request(app.callback()) + const res = yield app.httpRequest() .get('/render-ejs') .expect(200); @@ -42,7 +41,7 @@ describe('test/lib/core/view.test.js', () => { }); it('should render nunjucks', function* () { - const res = yield request(app.callback()) + const res = yield app.httpRequest() .get('/render-nunjucks') .expect(200); @@ -53,7 +52,7 @@ describe('test/lib/core/view.test.js', () => { }); it('should render with options.viewEngine', function* () { - const res = yield request(app.callback()) + const res = yield app.httpRequest() .get('/render-with-options') .expect(200); @@ -64,7 +63,7 @@ describe('test/lib/core/view.test.js', () => { describe('renderString', () => { it('should renderString', function* () { - const res = yield request(app.callback()) + const res = yield app.httpRequest() .get('/render-string') .expect(200); assert(res.body.tpl === 'hello world'); @@ -74,7 +73,7 @@ describe('test/lib/core/view.test.js', () => { }); it('should throw when no viewEngine', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/render-string-without-view-engine') .expect(500); }); @@ -94,34 +93,34 @@ describe('test/lib/core/view.test.js', () => { }); it('should render with options', function(done) { - request(app.callback()) + app.httpRequest() .get('/') .expect(200) .expect(`Hi, mk・2\ntest-app-helper: test-bar@${app.config.baseDir}\nraw:
dar
\n2014 @ mk2 <br>\n`, done); }); it('should render with async function controller', function(done) { - request(app.callback()) + app.httpRequest() .get('/async') .expect(200) .expect(`Hi, mk・2\ntest-app-helper: test-bar@${app.config.baseDir}\nraw:
dar
\n2014 @ mk2 <br>\n`, done); }); it('should render have helper instance', function(done) { - request(app.callback()) + app.httpRequest() .get('/') .expect(200, done); }); it('should render with empty', function(done) { - request(app.callback()) + app.httpRequest() .get('/empty') .expect(200) .expect(`Hi, \ntest-app-helper: test-bar@${app.config.baseDir}\nraw:
dar
\n2014 @ mk2 <br>\n`, done); }); it('should render template string', function(done) { - request(app.callback()) + app.httpRequest() .get('/string') .expect(200) .expect('templateString', done); diff --git a/test/lib/egg.test.js b/test/lib/egg.test.js index 3a93a4398a..e518dec41d 100644 --- a/test/lib/egg.test.js +++ b/test/lib/egg.test.js @@ -4,7 +4,6 @@ const mm = require('egg-mock'); const assert = require('assert'); const path = require('path'); const fs = require('fs'); -const request = require('supertest'); const sleep = require('mz-modules/sleep'); const spy = require('spy'); const Transport = require('egg-logger').Transport; @@ -161,15 +160,15 @@ describe('test/lib/egg.test.js', () => { }); it('should handle unhandledRejection and log it', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/throw-unhandledRejection') .expect('foo') .expect(200); - yield request(app.callback()) + yield app.httpRequest() .get('/throw-unhandledRejection-string') .expect('foo') .expect(200); - yield request(app.callback()) + yield app.httpRequest() .get('/throw-unhandledRejection-obj') .expect('foo') .expect(200); @@ -195,7 +194,7 @@ describe('test/lib/egg.test.js', () => { it('should access base context properties success', function* () { mm(app.config.logger, 'level', 'DEBUG'); - yield request(app.callback()) + yield app.httpRequest() .get('/') .expect('hello') .expect(200); @@ -212,14 +211,14 @@ describe('test/lib/egg.test.js', () => { }); it('should get pathName success', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/pathName') .expect('controller.home') .expect(200); }); it('should get config success', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/config') .expect('base-context-class') .expect(200); diff --git a/test/lib/plugins/development.test.js b/test/lib/plugins/development.test.js index 0dec795861..c9e1516b40 100644 --- a/test/lib/plugins/development.test.js +++ b/test/lib/plugins/development.test.js @@ -2,7 +2,6 @@ const fs = require('fs'); const path = require('path'); -const request = require('supertest'); const pedding = require('pedding'); const mm = require('egg-mock'); const utils = require('../../utils'); @@ -28,20 +27,20 @@ describe('test/lib/plugins/development.test.js', () => { } }); - request(app.callback()) + app.httpRequest() .get('/foo.js') .expect(200) .end(done); - request(app.callback()) + app.httpRequest() .get('/public/hello') .expect(404, done); - request(app.callback()) + app.httpRequest() .get('/assets/hello') .expect(404, done); - request(app.callback()) + app.httpRequest() .get('/__koa_mock_scene_toolbox/hello') .expect(404, done); }); diff --git a/test/lib/plugins/i18n.test.js b/test/lib/plugins/i18n.test.js index 9fbd0b9be5..66f0167863 100644 --- a/test/lib/plugins/i18n.test.js +++ b/test/lib/plugins/i18n.test.js @@ -1,6 +1,5 @@ 'use strict'; -const request = require('supertest'); const utils = require('../../utils'); describe('test/lib/plugins/i18n.test.js', () => { @@ -13,7 +12,7 @@ describe('test/lib/plugins/i18n.test.js', () => { describe('ctx.__(key, value)', () => { it('should return locale de', () => { - return request(app.callback()) + return app.httpRequest() .get('/message?locale=de') .expect(200) .expect('Set-Cookie', /locale=de; path=\/; expires=[^;]+ GMT/) @@ -34,7 +33,7 @@ describe('test/lib/plugins/i18n.test.js', () => { describe('view render with __(key, value)', () => { it('should render with default locale: en-US', () => { - return request(app.callback()) + return app.httpRequest() .get('/') .expect(200) .expect('Set-Cookie', /locale=en-us; path=\/; expires=[^;]+ GMT/) @@ -42,7 +41,7 @@ describe('test/lib/plugins/i18n.test.js', () => { }); it('should render with query locale: zh_CN', () => { - return request(app.callback()) + return app.httpRequest() .get('/?locale=zh_CN') .set('Host', 'foo.example.com') .expect(200) diff --git a/test/lib/plugins/multipart.test.js b/test/lib/plugins/multipart.test.js index c72c2e5229..28fbf9b3c4 100644 --- a/test/lib/plugins/multipart.test.js +++ b/test/lib/plugins/multipart.test.js @@ -30,6 +30,7 @@ describe('test/lib/plugins/multipart.test.js', () => { after(() => { server.close(); + return app.close(); }); it('should upload with csrf', done => { diff --git a/test/lib/plugins/onerror.test.js b/test/lib/plugins/onerror.test.js index 713799b7c5..277ecd0a90 100644 --- a/test/lib/plugins/onerror.test.js +++ b/test/lib/plugins/onerror.test.js @@ -1,6 +1,5 @@ 'use strict'; -const request = require('supertest'); const mm = require('egg-mock'); const utils = require('../../utils'); @@ -19,7 +18,7 @@ describe('test/lib/plugins/onerror.test.js', () => { it('should redirect to error page', () => { mm(app.config, 'env', 'test'); - return request(app.callback()) + return app.httpRequest() .get('/?status=500') .expect('Location', 'http://eggjs.org/500?real_status=500') .expect(302); diff --git a/test/lib/plugins/schedule.test.js b/test/lib/plugins/schedule.test.js index bf4237a16f..6b06cabd54 100644 --- a/test/lib/plugins/schedule.test.js +++ b/test/lib/plugins/schedule.test.js @@ -3,17 +3,18 @@ const assert = require('assert'); const path = require('path'); const fs = require('fs'); -const utils = require('../../utils'); const sleep = require('mz-modules/sleep'); +const utils = require('../../utils'); describe('test/lib/plugins/schedule.test.js', () => { it('should schedule work', function* () { const app = utils.cluster('apps/schedule', { workers: 2, }); + app.debug(); app.coverage(false); yield app.ready(); - yield sleep(5000); + yield sleep(7000); yield app.close(); const log = getLogContent('schedule'); const count = contains(log, 'cron'); diff --git a/test/lib/plugins/session.test.js b/test/lib/plugins/session.test.js index f38a683080..820713e8bd 100644 --- a/test/lib/plugins/session.test.js +++ b/test/lib/plugins/session.test.js @@ -1,7 +1,6 @@ 'use strict'; const assert = require('assert'); -const request = require('supertest'); const mm = require('egg-mock'); const utils = require('../../utils'); @@ -18,7 +17,7 @@ describe('test/lib/plugins/session.test.js', () => { app.mockContext({ userId: 's1', }); - request(app.callback()) + app.httpRequest() .get('/?uid=1') .expect({ userId: 's1', @@ -35,7 +34,7 @@ describe('test/lib/plugins/session.test.js', () => { app.mockContext({ userId: 's1', }); - request(app.callback()) + app.httpRequest() .get('/?uid=2&userId=s1') .set('Cookie', cookie) .expect({ @@ -51,7 +50,7 @@ describe('test/lib/plugins/session.test.js', () => { app.mockContext({ userId: 's2', }); - request(app.callback()) + app.httpRequest() .get('/?uid=2') .set('Cookie', cookie) .expect({ @@ -64,7 +63,7 @@ describe('test/lib/plugins/session.test.js', () => { }) .expect(200, err => { if (err) return done(err); - request(app.callback()) + app.httpRequest() .get('/clear') .set('Cookie', cookie) .expect('set-cookie', /EGG_SESS=;/, done); diff --git a/test/lib/plugins/static.test.js b/test/lib/plugins/static.test.js index a43445d1db..bdfd43c3dc 100644 --- a/test/lib/plugins/static.test.js +++ b/test/lib/plugins/static.test.js @@ -1,6 +1,5 @@ 'use strict'; -const request = require('supertest'); const utils = require('../../utils'); describe('test/lib/plugins/static.test.js', () => { @@ -11,7 +10,7 @@ describe('test/lib/plugins/static.test.js', () => { }); it('should get exists js file', () => { - return request(app.callback()) + return app.httpRequest() .get('/public/foo.js') .expect('alert(\'bar\');\n') .expect(200); diff --git a/test/lib/plugins/watcher.test.js b/test/lib/plugins/watcher.test.js index d1bfe9c7dd..18cf2f5ab8 100644 --- a/test/lib/plugins/watcher.test.js +++ b/test/lib/plugins/watcher.test.js @@ -3,7 +3,6 @@ const assert = require('assert'); const mm = require('egg-mock'); const fs = require('fs'); -const request = require('supertest'); const sleep = require('mz-modules/sleep'); const utils = require('../../utils'); const file_path1 = utils.getFilepath('apps/watcher-development-app/tmp.txt'); @@ -11,7 +10,6 @@ const file_path2 = utils.getFilepath('apps/watcher-development-app/tmp/tmp.txt') const file_path1_agent = utils.getFilepath('apps/watcher-development-app/tmp-agent.txt'); describe('test/lib/plugins/watcher.test.js', () => { - describe('default', () => { let app; beforeEach(() => { @@ -24,10 +22,9 @@ describe('test/lib/plugins/watcher.test.js', () => { afterEach(mm.restore); it('should app watcher work', function* () { - const server = app.callback(); let count = 0; - yield request(server) + yield app.httpRequest() .get('/app-watch') .expect(200) .expect('app watch success'); @@ -36,7 +33,7 @@ describe('test/lib/plugins/watcher.test.js', () => { fs.writeFileSync(file_path1, 'aaa'); yield sleep(3000); - yield request(server) + yield app.httpRequest() .get('/app-msg') .expect(200) .expect(function(res) { @@ -48,7 +45,7 @@ describe('test/lib/plugins/watcher.test.js', () => { fs.writeFileSync(file_path2, 'aaa'); yield sleep(3000); - yield request(server) + yield app.httpRequest() .get('/app-msg') .expect(200) .expect(function(res) { @@ -60,7 +57,7 @@ describe('test/lib/plugins/watcher.test.js', () => { it('should agent watcher work', function* () { let count = 0; - yield request(app.callback()) + yield app.httpRequest() .get('/agent-watch') .expect(200) .expect('agent watch success'); @@ -68,7 +65,7 @@ describe('test/lib/plugins/watcher.test.js', () => { fs.writeFileSync(file_path1_agent, 'bbb'); yield sleep(3000); - yield request(app.callback()) + yield app.httpRequest() .get('/agent-msg') .expect(200) .expect(res => { @@ -77,7 +74,6 @@ describe('test/lib/plugins/watcher.test.js', () => { assert(count > lastCount); }); }); - }); describe('config.watcher.type is default', () => {