Skip to content

Commit

Permalink
chore: use app.httpRequest() instead of supertest (#1041)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored and dead-horse committed Jun 13, 2017
1 parent 78a13d5 commit 4e510b2
Show file tree
Hide file tree
Showing 35 changed files with 153 additions and 186 deletions.
5 changes: 2 additions & 3 deletions docs/source/en/intro/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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.
Expand Down
19 changes: 9 additions & 10 deletions docs/source/en/tutorials/restful.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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 });
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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', () => {
Expand All @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions docs/source/zh-cn/advanced/framework.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -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);
});
Expand Down
5 changes: 2 additions & 3 deletions docs/source/zh-cn/intro/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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
```

执行测试:
Expand Down
5 changes: 2 additions & 3 deletions docs/source/zh-cn/tutorials/restful.md
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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',
Expand All @@ -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',
Expand Down
5 changes: 2 additions & 3 deletions test/app/extend/application.test.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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');
});
Expand Down Expand Up @@ -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');
Expand Down
7 changes: 3 additions & 4 deletions test/app/extend/context.jsonp.test.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand Down
33 changes: 16 additions & 17 deletions test/app/extend/context.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');

Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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);

Expand All @@ -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');
});
Expand All @@ -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}');
Expand All @@ -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,
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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,
Expand All @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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({
Expand All @@ -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({
Expand Down
Loading

0 comments on commit 4e510b2

Please sign in to comment.