Skip to content

Commit 187b0e2

Browse files
feitian124JacksonTian
authored andcommitted
Support use other xml parser
1 parent 199cdfa commit 187b0e2

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

lib/wechat.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ var wrapTpl = '<xml>' +
8888
var encryptWrap = ejs.compile(wrapTpl);
8989

9090
var load = function (stream, callback) {
91+
// support content-type 'text/xml' using 'express-xml-bodyparser', which set raw xml string
92+
// to 'req.rawBody'(while latest body-parser no longer set req.rawBody), see
93+
// https://github.com/macedigital/express-xml-bodyparser/blob/master/lib/types/xml.js#L79
94+
if (stream.rawBody) {
95+
callback(null, stream.rawBody);
96+
return;
97+
}
98+
9199
var buffers = [];
92100
stream.on('data', function (trunk) {
93101
buffers.push(trunk);

test/wechat.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ var wechat = require('../');
1010

1111
var app = connect();
1212
app.use(connect.query());
13+
app.use(function (req, res, next) {
14+
if (req.query.rawBody) {
15+
req.rawBody = "<xml><ToUserName><![CDATA[nvshen]]></ToUserName>\
16+
<FromUserName><![CDATA[diaosi]]></FromUserName>\
17+
<CreateTime>1362161914</CreateTime>\
18+
<MsgType><![CDATA[location]]></MsgType>\
19+
<Location_X>30.283878</Location_X>\
20+
<Location_Y>120.063370</Location_Y>\
21+
<Scale>15</Scale>\
22+
<Label><![CDATA[]]></Label>\
23+
<MsgId>5850440872586764820</MsgId>\
24+
</xml>";
25+
}
26+
next();
27+
});
1328
app.use('/wechat', wechat('some token', function (req, res, next) {
1429
// 微信输入信息都在req.weixin上
1530
var info = req.weixin;
@@ -153,6 +168,23 @@ describe('wechat.js', function () {
153168
});
154169
});
155170

171+
it('should ok with req.rawBody', function (done) {
172+
request(app)
173+
.post('/wechat' + tail() + "&rawBody=true")
174+
.send('')
175+
.expect(200)
176+
.end(function (err, res){
177+
if (err) return done(err);
178+
var body = res.text.toString();
179+
body.should.include('<ToUserName><![CDATA[diaosi]]></ToUserName>');
180+
body.should.include('<FromUserName><![CDATA[nvshen]]></FromUserName>');
181+
body.should.match(/<CreateTime>\d{13}<\/CreateTime>/);
182+
body.should.include('<MsgType><![CDATA[text]]></MsgType>');
183+
body.should.include('<Content><![CDATA[hehe]]></Content>');
184+
done();
185+
});
186+
});
187+
156188
it('should ok with text type object', function (done) {
157189
var info = {
158190
sp: 'nvshen',

0 commit comments

Comments
 (0)