Skip to content
Closed
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
9 changes: 9 additions & 0 deletions lib/wechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ var wrapTpl = '<xml>' +
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);
Expand All @@ -96,6 +104,7 @@ var load = function (stream, callback) {
callback(null, Buffer.concat(buffers));
});
stream.once('error', callback);

};

/*!
Expand Down
30 changes: 30 additions & 0 deletions test/wechat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<ToUserName><![CDATA[diaosi]]></ToUserName>');
// body.should.include('<FromUserName><![CDATA[nvshen]]></FromUserName>');
// body.should.match(/<CreateTime>\d{13}<\/CreateTime>/);
// body.should.include('<MsgType><![CDATA[text]]></MsgType>');
// body.should.include('<Content><![CDATA[hehe]]></Content>');
// done();
//});
});
*/

it('should ok with text type object', function (done) {
var info = {
sp: 'nvshen',
Expand Down