Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support xml #131

Merged
merged 2 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ app.use(async ctx => {

## Options

* **enableTypes**: parser will only parse when request type hits enableTypes, default is `['json', 'form']`.
* **enableTypes**: parser will only parse when request type hits enableTypes, support `json/form/text/xml`, default is `['json', 'form']`.
* **encoding**: requested encoding. Default is `utf-8` by `co-body`.
* **formLimit**: limit of the `urlencoded` body. If the body ends up being larger than this limit, a 413 error code is returned. Default is `56kb`.
* **jsonLimit**: limit of the `json` body. Default is `1mb`.
* **textLimit**: limit of the `text` body. Default is `1mb`.
* **xmlLimit**: limit of the `xml` body. Default is `1mb`.
* **strict**: when set to true, JSON parser will only accept arrays and objects. Default is `true`. See [strict mode](https://github.com/cojs/co-body#options) in `co-body`. In strict mode, `ctx.request.body` will always be an object(or array), this avoid lots of type judging. But text body will always return string type.
* **detectJSON**: custom json request detect function. Default is `null`.

Expand Down
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module.exports = function (opts) {
var enableForm = checkEnable(enableTypes, 'form');
var enableJson = checkEnable(enableTypes, 'json');
var enableText = checkEnable(enableTypes, 'text');
var enableXml = checkEnable(enableTypes, 'xml');

opts.detectJSON = undefined;
opts.onerror = undefined;
Expand All @@ -59,15 +60,23 @@ module.exports = function (opts) {
'text/plain',
];

// default xml types
var xmlTypes = [
'text/xml',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

默认先不开启吧,作为一个可选项打开。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enableTypes 里面没有 xml,所以不是默认打开的。

'application/xml',
];

var jsonOpts = formatOptions(opts, 'json');
var formOpts = formatOptions(opts, 'form');
var textOpts = formatOptions(opts, 'text');
var xmlOpts = formatOptions(opts, 'xml');

var extendTypes = opts.extendTypes || {};

extendType(jsonTypes, extendTypes.json);
extendType(formTypes, extendTypes.form);
extendType(textTypes, extendTypes.text);
extendType(xmlTypes, extendTypes.xml);

return async function bodyParser(ctx, next) {
if (ctx.request.body !== undefined) return await next();
Expand Down Expand Up @@ -96,6 +105,9 @@ module.exports = function (opts) {
if (enableText && ctx.request.is(textTypes)) {
return await parse.text(ctx, textOpts) || '';
}
if (enableXml && ctx.request.is(xmlTypes)) {
return await parse.text(ctx, xmlOpts) || '';
}
return {};
}
};
Expand Down
72 changes: 69 additions & 3 deletions test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,56 @@ describe('test/middleware.test.js', function () {
});
});

describe('extent type', function () {
it('should extent json ok', function (done) {
describe('xml body', function () {
it('should parse xml body ok', function (done) {
var app = App({
enableTypes: ['xml'],
});
app.use(async (ctx) => {
ctx.headers['content-type'].should.equal('application/xml');
ctx.request.body.should.equal('<xml>abc</xml>');
ctx.request.rawBody.should.equal('<xml>abc</xml>');
ctx.body = ctx.request.body;
});
request(app.listen())
.post('/')
.type('xml')
.send('<xml>abc</xml>')
.expect('<xml>abc</xml>', done);
});

it('should not parse text body when disable', function (done) {
var app = App();
app.use(async (ctx) => {
ctx.headers['content-type'].should.equal('application/xml');
ctx.body = ctx.request.body;
});
request(app.listen())
.post('/')
.type('xml')
.send('<xml>abc</xml>')
.expect({}, done);
});

it('should xml body reach the limit size', function (done) {
var app = App({
enableTypes: ['xml'],
xmlLimit: 10,
});
app.use(async (ctx) => {
ctx.headers['content-type'].should.equal('application/xml');
ctx.body = ctx.request.body;
});
request(app.listen())
.post('/')
.type('xml')
.send('<xml>abcdefghijklmn</xml>')
.expect(413, done);
});
});

describe('extend type', function () {
it('should extend json ok', function (done) {
var app = App({
extendTypes: {
json: 'application/x-javascript'
Expand All @@ -228,7 +276,7 @@ describe('test/middleware.test.js', function () {
.expect({ foo: 'bar' }, done);
});

it('should extent json with array ok', function (done) {
it('should extend json with array ok', function (done) {
var app = App({
extendTypes: {
json: ['application/x-javascript', 'application/y-javascript']
Expand All @@ -244,6 +292,24 @@ describe('test/middleware.test.js', function () {
.send(JSON.stringify({ foo: 'bar' }))
.expect({ foo: 'bar' }, done);
});

it('should extend xml ok', function (done) {
var app = App({
enableTypes: ['xml'],
extendTypes: {
xml: 'application/xml-custom'
}
});
app.use(async (ctx) => {
ctx.body = ctx.request.body;
});

request(app.listen())
.post('/')
.type('application/xml-custom')
.send('<xml>abc</xml>')
.expect('<xml>abc</xml>', done);
});
});

describe('enableTypes', function () {
Expand Down