diff --git a/lib/wechat.js b/lib/wechat.js index 816772bd..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); @@ -96,6 +104,7 @@ var load = function (stream, callback) { callback(null, Buffer.concat(buffers)); }); stream.once('error', callback); + }; /*! 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',