Skip to content

Commit 705673d

Browse files
authoredMar 24, 2020
feat: support xml (#131)
1 parent 6fd7e9c commit 705673d

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed
 

‎README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ app.use(async ctx => {
4848

4949
## Options
5050

51-
* **enableTypes**: parser will only parse when request type hits enableTypes, default is `['json', 'form']`.
51+
* **enableTypes**: parser will only parse when request type hits enableTypes, support `json/form/text/xml`, default is `['json', 'form']`.
5252
* **encoding**: requested encoding. Default is `utf-8` by `co-body`.
5353
* **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`.
5454
* **jsonLimit**: limit of the `json` body. Default is `1mb`.
5555
* **textLimit**: limit of the `text` body. Default is `1mb`.
56+
* **xmlLimit**: limit of the `xml` body. Default is `1mb`.
5657
* **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.
5758
* **detectJSON**: custom json request detect function. Default is `null`.
5859

‎index.js

+12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module.exports = function (opts) {
3434
var enableForm = checkEnable(enableTypes, 'form');
3535
var enableJson = checkEnable(enableTypes, 'json');
3636
var enableText = checkEnable(enableTypes, 'text');
37+
var enableXml = checkEnable(enableTypes, 'xml');
3738

3839
opts.detectJSON = undefined;
3940
opts.onerror = undefined;
@@ -59,15 +60,23 @@ module.exports = function (opts) {
5960
'text/plain',
6061
];
6162

63+
// default xml types
64+
var xmlTypes = [
65+
'text/xml',
66+
'application/xml',
67+
];
68+
6269
var jsonOpts = formatOptions(opts, 'json');
6370
var formOpts = formatOptions(opts, 'form');
6471
var textOpts = formatOptions(opts, 'text');
72+
var xmlOpts = formatOptions(opts, 'xml');
6573

6674
var extendTypes = opts.extendTypes || {};
6775

6876
extendType(jsonTypes, extendTypes.json);
6977
extendType(formTypes, extendTypes.form);
7078
extendType(textTypes, extendTypes.text);
79+
extendType(xmlTypes, extendTypes.xml);
7180

7281
return async function bodyParser(ctx, next) {
7382
if (ctx.request.body !== undefined) return await next();
@@ -96,6 +105,9 @@ module.exports = function (opts) {
96105
if (enableText && ctx.request.is(textTypes)) {
97106
return await parse.text(ctx, textOpts) || '';
98107
}
108+
if (enableXml && ctx.request.is(xmlTypes)) {
109+
return await parse.text(ctx, xmlOpts) || '';
110+
}
99111
return {};
100112
}
101113
};

‎test/middleware.test.js

+69-3
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,56 @@ describe('test/middleware.test.js', function () {
210210
});
211211
});
212212

213-
describe('extent type', function () {
214-
it('should extent json ok', function (done) {
213+
describe('xml body', function () {
214+
it('should parse xml body ok', function (done) {
215+
var app = App({
216+
enableTypes: ['xml'],
217+
});
218+
app.use(async (ctx) => {
219+
ctx.headers['content-type'].should.equal('application/xml');
220+
ctx.request.body.should.equal('<xml>abc</xml>');
221+
ctx.request.rawBody.should.equal('<xml>abc</xml>');
222+
ctx.body = ctx.request.body;
223+
});
224+
request(app.listen())
225+
.post('/')
226+
.type('xml')
227+
.send('<xml>abc</xml>')
228+
.expect('<xml>abc</xml>', done);
229+
});
230+
231+
it('should not parse text body when disable', function (done) {
232+
var app = App();
233+
app.use(async (ctx) => {
234+
ctx.headers['content-type'].should.equal('application/xml');
235+
ctx.body = ctx.request.body;
236+
});
237+
request(app.listen())
238+
.post('/')
239+
.type('xml')
240+
.send('<xml>abc</xml>')
241+
.expect({}, done);
242+
});
243+
244+
it('should xml body reach the limit size', function (done) {
245+
var app = App({
246+
enableTypes: ['xml'],
247+
xmlLimit: 10,
248+
});
249+
app.use(async (ctx) => {
250+
ctx.headers['content-type'].should.equal('application/xml');
251+
ctx.body = ctx.request.body;
252+
});
253+
request(app.listen())
254+
.post('/')
255+
.type('xml')
256+
.send('<xml>abcdefghijklmn</xml>')
257+
.expect(413, done);
258+
});
259+
});
260+
261+
describe('extend type', function () {
262+
it('should extend json ok', function (done) {
215263
var app = App({
216264
extendTypes: {
217265
json: 'application/x-javascript'
@@ -228,7 +276,7 @@ describe('test/middleware.test.js', function () {
228276
.expect({ foo: 'bar' }, done);
229277
});
230278

231-
it('should extent json with array ok', function (done) {
279+
it('should extend json with array ok', function (done) {
232280
var app = App({
233281
extendTypes: {
234282
json: ['application/x-javascript', 'application/y-javascript']
@@ -244,6 +292,24 @@ describe('test/middleware.test.js', function () {
244292
.send(JSON.stringify({ foo: 'bar' }))
245293
.expect({ foo: 'bar' }, done);
246294
});
295+
296+
it('should extend xml ok', function (done) {
297+
var app = App({
298+
enableTypes: ['xml'],
299+
extendTypes: {
300+
xml: 'application/xml-custom'
301+
}
302+
});
303+
app.use(async (ctx) => {
304+
ctx.body = ctx.request.body;
305+
});
306+
307+
request(app.listen())
308+
.post('/')
309+
.type('application/xml-custom')
310+
.send('<xml>abc</xml>')
311+
.expect('<xml>abc</xml>', done);
312+
});
247313
});
248314

249315
describe('enableTypes', function () {

0 commit comments

Comments
 (0)
Please sign in to comment.