Skip to content

Commit

Permalink
Fix invalid char before root tag
Browse files Browse the repository at this point in the history
  • Loading branch information
fiznool committed Aug 28, 2015
1 parent 84a4e3c commit 9042140
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/types/xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports.regexp = regexp;
function xmlparser(options) {

var parserOptions = util._extend({
async: true,
async: false,
explicitArray: true,
normalize: true,
normalizeTags: true,
Expand Down Expand Up @@ -82,7 +82,7 @@ function xmlparser(options) {
});

// since xml2js v0.4.5 parse errors propagate without being able to catch them otherwise
parser.saxParser.onerror = responseHandler;
// parser.saxParser.onerror = responseHandler;

// in case `parseString` callback never was called, ensure response is sent
parser.saxParser.onend = function() {
Expand All @@ -92,7 +92,7 @@ function xmlparser(options) {
};

req.on('end', function () {

// invalid xml, length required
if (data.trim().length === 0) {
return next(error(411));
Expand Down
16 changes: 13 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ describe('XmlParserMiddleware', function () {
.expect(400, done);
});

it('should throw 400 on invalid char before root tag', function (done) {
request(app)
.post('/')
.set('Content-Type', 'application/vendor-spec+xml')
.send('"<xml>ok</xml>')
.expect(400, done);
});

it('should throw 400 on unexpected end', function (done) {
request(app)
.post('/')
Expand All @@ -118,7 +126,9 @@ describe('XmlParserMiddleware', function () {
.set('Content-Type', 'application/xml')
.send('<xml></xml>')
.expect(200, function (err, res) {
assert.deepEqual(res.body, {});
assert.deepEqual(res.body, {
xml: ''
});
done();
});
});
Expand All @@ -144,7 +154,7 @@ describe('XmlParserMiddleware', function () {
.send('<xml>this is invalid')
.expect(400, done);
});

it('should throw 400 on invalid xml body', function (done) {
request(app)
.post('/')
Expand Down Expand Up @@ -237,7 +247,7 @@ describe('XmlParserMiddleware', function () {
app.post('/', function (req, res) {
res.json(req.body);
});

app.post('/xml', xmlparser(), function (req, res) {
res.json(req.body);
});
Expand Down

0 comments on commit 9042140

Please sign in to comment.