Skip to content

Commit

Permalink
test: add new test case for runInAnonymousContextScope (eggjs#5141)
Browse files Browse the repository at this point in the history
Since 'runInAnoymousContextScope' now has a new feature with a request
as a parameter, it lacks a new test case, this is the test case for it.

Refs: eggjs#5134
  • Loading branch information
SEWeiTung authored and iblogc committed Mar 13, 2023
1 parent 5d51e83 commit 7d741cf
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 2 deletions.
15 changes: 15 additions & 0 deletions test/app/extend/application.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,21 @@ describe('test/app/extend/application.test.js', () => {
});
});

describe('app.runInAnonymousContextScope(scope,request)', () => {
it('should run task in anonymous context scope with req success', async () => {
const app = utils.app('apps/app-runInAnonymousContextScope-withRequest');
await app.ready();
await app.close();
await utils.sleep(2100);
const logdir = app.config.logger.dir;
const logs = fs.readFileSync(path.join(logdir, 'app-runInAnonymousContextScope-withRequest-web.log'), { encoding: 'utf8' }).split('\n');

assert.match(logs[0], / INFO \d+ \[-\/127.0.0.2\/-\/\d+ms GET \/] inside before close on ctx logger/);
assert.match(logs[1], / INFO \d+ \[-\/127.0.0.2\/-\/\d+ms GET \/] inside before close on app logger/);
assert.match(logs[2], / INFO \d+ outside before close on app logger/);
});
});

describe('app.handleRequest(ctx, fnMiddleware)', () => {
let app;
before(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module.exports = class Boot {
constructor(app) {
this.app = app;
}

async beforeClose() {

const request = {
headers: {
host: '127.0.0.2',
'x-forwarded-for': '127.0.0.2',
},
querystring: 'Testing',
host: '127.0.0.2',
hostname: '127.0.0.2',
protocol: 'http',
secure: 'false',
method: 'GET',
url: '/',
path: '/',
socket: {
remoteAddress: '127.0.0.2',
remotePort: 7001,
},
};

await this.app.runInAnonymousContextScope(async ctx => {
ctx.logger.info('inside before close on ctx logger');
this.app.logger.info('inside before close on app logger');
}, request);
this.app.logger.info('outside before close on app logger');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.keys = 'foo';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.logger = {
consoleLevel: 'NONE',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "app-runInAnonymousContextScope-withRequest"
}
4 changes: 2 additions & 2 deletions test/lib/cluster/app_worker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ describe('test/lib/cluster/app_worker.test.js', () => {
version[0] > 9) {
html = new RegExp(
'GET /foo bar HTTP/1.1\r\nHost: 127.0.0.1:\\d+\r\nAccept-Encoding: gzip, ' +
'deflate\r\nConnection: close\r\n\r\n');
'deflate\r\nuser-agent: egg-mock/\\d+.\\d+.\\d+\r\nConnection: close\r\n\r\n');
}

// customized client error response
const test1 = app.httpRequest().get('/foo bar');
test1.request().path = '/foo bar';
await test1.expect(html)
.expect('foo', 'bar')
.expect('content-length', '99')
.expect('content-length', '128')
.expect(418);

// customized client error handle function throws
Expand Down

0 comments on commit 7d741cf

Please sign in to comment.