Skip to content

Commit

Permalink
enable meta and body_parser
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 19, 2024
1 parent 9200306 commit e5d11f1
Show file tree
Hide file tree
Showing 51 changed files with 158 additions and 172 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ jobs:
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest, macos-latest, windows-latest'
version: '18.19.0, 18, 20, 22'
version: '18.19.0, 18, 20, 22, 23'
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
const assert = require('assert');
const querystring = require('querystring');
const utils = require('../../utils');

describe('test/app/middleware/body_parser.test.js', () => {
let app;
let app1;
let csrf;
let cookies;
before(done => {
app = utils.app('apps/body_parser_testapp');
app.ready(() => {
app.httpRequest()
.get('/test/body_parser/user')
.expect(200, (err, res) => {
assert(!err);
csrf = res.body.csrf || '';
cookies = res.headers['set-cookie'].join(';');
assert(csrf);
done();
});
});
import { strict as assert } from 'node:assert';
import querystring from 'node:querystring';
import { createApp, MockApplication } from '../../utils.js';

describe('test/app/middleware/body_parser.test.ts', () => {
let app: MockApplication;
let app1: MockApplication;
let csrf: string;
let cookies: string;
before(async () => {
app = createApp('apps/body_parser_testapp');
await app.ready();
const res = await app.httpRequest()
.get('/test/body_parser/user')
.expect(200);
csrf = res.body.csrf || '';
cookies = (res.headers['set-cookie'] as any).join(';');
assert(csrf);
});

after(() => app.close());
Expand Down Expand Up @@ -109,7 +105,7 @@ describe('test/app/middleware/body_parser.test.js', () => {
});

it('should disable body parser', async () => {
app1 = utils.app('apps/body_parser_testapp_disable');
app1 = createApp('apps/body_parser_testapp_disable');
await app1.ready();

await app1.httpRequest()
Expand All @@ -119,7 +115,7 @@ describe('test/app/middleware/body_parser.test.js', () => {
});

it('should body parser support ignore', async () => {
app1 = utils.app('apps/body_parser_testapp_ignore');
app1 = createApp('apps/body_parser_testapp_ignore');
await app1.ready();

await app1.httpRequest()
Expand All @@ -135,7 +131,7 @@ describe('test/app/middleware/body_parser.test.js', () => {
});

it('should body parser support match', async () => {
app1 = utils.app('apps/body_parser_testapp_match');
app1 = createApp('apps/body_parser_testapp_match');
await app1.ready();

await app1.httpRequest()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
const assert = require('assert');
const mm = require('egg-mock');
const fs = require('fs/promises');
const utils = require('../../utils');
import { strict as assert } from 'node:assert';
import fs from 'node:fs/promises';
import { scheduler } from 'node:timers/promises';
import { createApp, MockApplication, restore, cluster } from '../../utils.js';

describe('test/app/middleware/meta.test.js', () => {
afterEach(mm.restore);
describe('test/app/middleware/meta.test.ts', () => {
afterEach(restore);

let app: MockApplication;

describe('default config', () => {
let app;
before(() => {
app = utils.app('apps/middlewares');
app = createApp('apps/middlewares');
return app.ready();
});
after(() => app.close());
Expand All @@ -23,9 +24,8 @@ describe('test/app/middleware/meta.test.js', () => {
});

describe('config.logger.enablePerformanceTimer = true', () => {
let app;
before(() => {
app = utils.app('apps/middlewares-meta-enablePerformanceTimer');
app = createApp('apps/middlewares-meta-enablePerformanceTimer');
return app.ready();
});
after(() => app.close());
Expand All @@ -41,9 +41,8 @@ describe('test/app/middleware/meta.test.js', () => {
});

describe('meta.logging = true', () => {
let app;
before(() => {
app = utils.app('apps/meta-logging-app');
app = createApp('apps/meta-logging-app');
return app.ready();
});
after(() => app.close());
Expand All @@ -54,16 +53,18 @@ describe('test/app/middleware/meta.test.js', () => {
.expect('X-Readtime', /\d+/)
.expect('hello world')
.expect(200);
if (process.platform === 'win32') await utils.sleep(2000);
if (process.platform === 'win32') {
await scheduler.wait(2000);
}
const content = (await fs.readFile(app.coreLogger.options.file, 'utf8')).split('\n').slice(-2, -1)[0];
assert(content.includes('[meta] request started, host: '));
assert.match(content, /\[meta] request started, host: /);
});
});

describe('cluster start', () => {
let app;
let app: MockApplication;
before(() => {
app = utils.cluster('apps/middlewares');
app = cluster('apps/middlewares');
return app.ready();
});
after(() => app.close());
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/apps/app-locals-getter/app/router.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

module.exports = app => {
app.get('/test', function* () {
app.get('/test', function () {
this.app.locals.foo = 'bar';
this.locals.abc = '123';
this.body = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

module.exports = app => {
app.get('/', function* () {
app.get('/', function () {
this.body = this.app.serverEmit;
});
};
2 changes: 1 addition & 1 deletion test/fixtures/apps/app-server-with-hostname/app/router.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

module.exports = app => {
app.get('/', function* () {
app.get('/', function () {
this.body = 'done';
});
};
2 changes: 1 addition & 1 deletion test/fixtures/apps/app-server/app/router.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = app => {
app.get('/', function* () {
app.get('/', function () {
this.body = this.app.serverEmit;
});
};
10 changes: 5 additions & 5 deletions test/fixtures/apps/app-throw/app/router.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';

module.exports = app => {
app.get('/throw', function* () {
app.get('/throw', function () {
this.body = 'foo';
setTimeout(() => {
a.b = c;
}, 1);
});

app.get('/throw-error-setter', function* () {
app.get('/throw-error-setter', function () {
this.body = 'foo';
setTimeout(() => {
const err = new Error('abc');
Expand All @@ -20,21 +20,21 @@ module.exports = app => {
}, 1);
});

app.get('/throw-unhandledRejection', function* () {
app.get('/throw-unhandledRejection', function () {
this.body = 'foo';
new Promise((resolve, reject) => {
reject(new Error('foo reject error'));
});
});

app.get('/throw-unhandledRejection-string', function* () {
app.get('/throw-unhandledRejection-string', function () {
this.body = 'foo';
new Promise((resolve, reject) => {
reject('foo reject string error');
});
});

app.get('/throw-unhandledRejection-obj', function* () {
app.get('/throw-unhandledRejection-obj', function () {
this.body = 'foo';
new Promise((resolve, reject) => {
const err = {
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/apps/body_parser_testapp/app/router.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
module.exports = app => {
app.get('/test/body_parser/user', function* () {
app.get('/test/body_parser/user', function () {
this.body = {
url: this.url,
csrf: this.csrf
};
});

app.post('/test/body_parser/user', function* () {
app.post('/test/body_parser/user', function () {
this.logger.info('request body %s', this.request.body);
this.body = this.request.body;
});

app.post('/test/body_parser/foo.json', function* () {
app.post('/test/body_parser/foo.json', function () {
this.body = this.request.body;
});
app.post('/test/body_parser/form.json', function* () {
app.post('/test/body_parser/form.json', function () {
this.body = this.request.body;
});
};
8 changes: 4 additions & 4 deletions test/fixtures/apps/body_parser_testapp_disable/app/router.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
module.exports = app => {
app.get('/test/body_parser/user', function* () {
app.get('/test/body_parser/user', function () {
this.body = {
url: this.url,
csrf: this.csrf
};
});

app.post('/test/body_parser/user', function* () {
app.post('/test/body_parser/user', function () {
this.body = this.request.body;
});

app.post('/test/body_parser/foo.json', function* () {
app.post('/test/body_parser/foo.json', function () {
this.body = this.request.body;
});
app.post('/test/body_parser/form.json', function* () {
app.post('/test/body_parser/form.json', function () {
this.body = this.request.body;
});
};
8 changes: 4 additions & 4 deletions test/fixtures/apps/body_parser_testapp_ignore/app/router.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
module.exports = app => {
app.get('/test/body_parser/user', function* () {
app.get('/test/body_parser/user', function () {
this.body = {
url: this.url,
csrf: this.csrf
};
});

app.post('/test/body_parser/user', function* () {
app.post('/test/body_parser/user', function () {
this.body = this.request.body;
});

app.post('/test/body_parser/foo.json', function* () {
app.post('/test/body_parser/foo.json', function () {
this.body = this.request.body;
});
app.post('/test/body_parser/form.json', function* () {
app.post('/test/body_parser/form.json', function () {
this.body = this.request.body;
});
};
8 changes: 4 additions & 4 deletions test/fixtures/apps/body_parser_testapp_match/app/router.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
module.exports = app => {
app.get('/test/body_parser/user', function* () {
app.get('/test/body_parser/user', function () {
this.body = {
url: this.url,
csrf: this.csrf
};
});

app.post('/test/body_parser/user', function* () {
app.post('/test/body_parser/user', function () {
this.body = this.request.body;
});

app.post('/test/body_parser/foo.json', function* () {
app.post('/test/body_parser/foo.json', function () {
this.body = this.request.body;
});
app.post('/test/body_parser/form.json', function* () {
app.post('/test/body_parser/form.json', function () {
this.body = this.request.body;
});
};
2 changes: 1 addition & 1 deletion test/fixtures/apps/csrf-disable/app/controller/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

exports.user = function* () {
exports.user = function () {
this.body = {
url: this.url,
name: this.request.body.name,
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/apps/csrf-enable/app/controller/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

exports.user = function* () {
exports.user = function () {
this.body = {
url: this.url,
name: this.query.name,
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/apps/csrf-ignore/app/controller/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

exports.user = function* () {
exports.user = function () {
this.body = {
url: this.url,
name: this.request.body.name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

module.exports = function* () {
module.exports = function () {
const logger = this.getLogger('foo');
logger.info('hello');
this.body = 'work, logger: ' + (logger ? 'exists' : 'not exists');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = function* () {
module.exports = function () {
this.body = {
workerTitle: process.title
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

module.exports = function* () {
module.exports = function () {
if (this.query.set_ip) {
this.ip = this.query.set_ip;
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/apps/demo/app/controller/home.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = function* () {
module.exports = function () {
this.body = {
workerTitle: process.title
};
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/apps/demo/app/controller/ip.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

module.exports = function* () {
module.exports = function () {
if (this.query.set_ip) {
this.ip = this.query.set_ip;
}
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/apps/development/app/router.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

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

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

0 comments on commit e5d11f1

Please sign in to comment.