UNPKG

koa2-wechat2

Version:

koa2 middleware for wechat

64 lines (52 loc) 1.7 kB
'use strict'; var Koa = require('koa'); var assert = require('assert'); var request = require('supertest'); var koa2wechat = require('../lib'); var weReply = koa2wechat.weReply; var getWechat = koa2wechat.getWechat; var wechatOpts = { appID: 'wxbbbeba8b89bf14cf', appSecret: '14f874b87dbf7711a7d2410ac0da6b4c', token: 'eid' }; var app = createApp(wechatOpts); describe('Array', function () { describe('#indexOf()', function () { it('should return -1 when the value is not present', function () { assert.equal(-1, [1, 2, 3].indexOf(4)); }); }); }); describe('weReply()', function () { it('should be a function', function () { should(weReply).be.Function(); }); it('should body get wrong', function (done) { request(app).get('/').set('Accept', 'application/json').expect('wrong', done); }); it('should 200', function (done) { var q = { timestamp: new Date().getTime(), nonce: parseInt(Math.random() * 10e10, 10) }; var s = ['some token', q.timestamp, q.nonce].sort().join(''); q.signature = require('crypto').createHash('sha1').update(s).digest('hex'); q.echostr = 'hehe'; request(app).get('/wechat?' + querystring.stringify(q)).expect(200).expect('hehe', done); }); it('should 401 invalid signature', function (done) { var q = { timestamp: new Date().getTime(), nonce: parseInt(Math.random() * 10e10, 10) }; q.signature = 'invalid_signature'; q.echostr = 'hehe'; request(app).get('/wechat?' + querystring.stringify(q)).expect(401).expect('Invalid signature', done); }); }); function createApp(opts) { var app = new Koa(); app.use(weReply(opts)); return app.callback(); }