Skip to content

Commit

Permalink
feat: mm.app() support server event (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored and popomore committed Feb 28, 2018
1 parent 1f3bc47 commit f1820d7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/supertest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const methods = require('methods');
const Test = require('supertest').Test;
const http = require('http');
const _server = Symbol('_server');

// patch from https://github.com/visionmedia/supertest/blob/199506d8dbfe0bb1434fc07c38cdcd1ab4c7c926/index.js#L19

Expand All @@ -16,9 +17,14 @@ const http = require('http');
*/

module.exports = app => {
let server = app.callback();
let server = app[_server] || app.callback();
if (typeof server === 'function') {
server = http.createServer(server);
// cache server, avoid create many times
app[_server] = server;
// emit server event just like egg-cluster does
// https://github.com/eggjs/egg-cluster/blob/master/lib/app_worker.js#L52
app.emit('server', server);
}
const obj = {};
for (const method of methods) {
Expand Down
9 changes: 9 additions & 0 deletions test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ function call(method) {
.expect('foo')
.expect(200, done);
});

it('should emit server event on app', () => {
return app.httpRequest()
.get('/keepAliveTimeout')
.expect(200)
.expect({
keepAliveTimeout: 5000,
});
});
});

describe(`mm.${method}({ baseDir, plugin=string })`, () => {
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/app/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = app => {
app.on('server', server => {
app.serverKeepAliveTimeout = server.keepAliveTimeout || 5000;
});
};
8 changes: 7 additions & 1 deletion test/fixtures/app/app/router.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
'use strict';

module.exports = function(app) {
module.exports = app => {
app.get('/', function* () {
this.body = 'foo';
});

app.get('/keepAliveTimeout', function* () {
this.body = {
keepAliveTimeout: this.app.serverKeepAliveTimeout,
};
});
};

0 comments on commit f1820d7

Please sign in to comment.