From 903d2128011517e9527110df97b709a41872e51e Mon Sep 17 00:00:00 2001 From: feitian124 Date: Sat, 2 May 2015 23:39:27 +0800 Subject: [PATCH 1/2] support content-type "text/xml" --- lib/wechat.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/wechat.js b/lib/wechat.js index 816772bd..0048197e 100644 --- a/lib/wechat.js +++ b/lib/wechat.js @@ -96,6 +96,13 @@ var load = function (stream, callback) { callback(null, Buffer.concat(buffers)); }); stream.once('error', callback); + + // support content-type 'text/xml' using 'express-xml-bodyparser', which set raw xml string + // to 'req.rawBody'(while latest body-parser no longer set req.rawBody), see + // https://github.com/macedigital/express-xml-bodyparser/blob/master/lib/types/xml.js#L79 + if(stream.rawBody) { + callback(null, stream.rawBody); + } }; /*! From 7e20aa267e140f3bcab5b643e57f401681d9835c Mon Sep 17 00:00:00 2001 From: feitian124 Date: Sun, 3 May 2015 14:59:39 +0800 Subject: [PATCH 2/2] enhance if logic --- lib/wechat.js | 14 ++++++++------ test/wechat.test.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/lib/wechat.js b/lib/wechat.js index 0048197e..77289c55 100644 --- a/lib/wechat.js +++ b/lib/wechat.js @@ -88,6 +88,14 @@ var wrapTpl = '' + var encryptWrap = ejs.compile(wrapTpl); var load = function (stream, callback) { + // support content-type 'text/xml' using 'express-xml-bodyparser', which set raw xml string + // to 'req.rawBody'(while latest body-parser no longer set req.rawBody), see + // https://github.com/macedigital/express-xml-bodyparser/blob/master/lib/types/xml.js#L79 + if(stream.rawBody) { + callback(null, stream.rawBody); + return; + } + var buffers = []; stream.on('data', function (trunk) { buffers.push(trunk); @@ -97,12 +105,6 @@ var load = function (stream, callback) { }); stream.once('error', callback); - // support content-type 'text/xml' using 'express-xml-bodyparser', which set raw xml string - // to 'req.rawBody'(while latest body-parser no longer set req.rawBody), see - // https://github.com/macedigital/express-xml-bodyparser/blob/master/lib/types/xml.js#L79 - if(stream.rawBody) { - callback(null, stream.rawBody); - } }; /*! diff --git a/test/wechat.test.js b/test/wechat.test.js index 43a4aa42..55827c36 100644 --- a/test/wechat.test.js +++ b/test/wechat.test.js @@ -153,6 +153,36 @@ describe('wechat.js', function () { }); }); + /* + it('should ok with req.rawBody', function (done) { + var rawBody = { + rawBody: { + ToUserName: 'nvshen', + FromUserName: 'diaosi', + CreateTime: '' + new Date().getTime(), + MsgType: 'text', + Content: '测试中' + } + }; + + request(app) + .post('/wechat'+tail()) + .send({}) + .expect(200, done); + //.expect(200) + //.end(function(err, res){ + // if (err) return done(err); + // var body = res.text.toString(); + // body.should.include(''); + // body.should.include(''); + // body.should.match(/\d{13}<\/CreateTime>/); + // body.should.include(''); + // body.should.include(''); + // done(); + //}); + }); + */ + it('should ok with text type object', function (done) { var info = { sp: 'nvshen',