UNPKG

1.27 kBJavaScriptView Raw
1const bodyParser = require('body-parser')
2const { xHubSignatureMiddleware: middleware, extractRawBody } = require('./index')
3
4const Readable = require('stream').Readable
5
6function mockRequest (signature, body, type) {
7 const req = new Readable()
8
9 req.headers = {
10 'content-length': body.length,
11 'content-type': type
12 }
13
14 if (signature) {
15 req.headers['X-Hub-Signature'] = signature
16 }
17
18 req.header = function (name) {
19 return this.headers[name]
20 }
21
22 req._read = function () {
23 this.push(body)
24 this.push(null)
25 }
26
27 return req
28}
29
30describe('body-parser compatibility', function () {
31 it('should work with bodyParser.json() and the "extractRawBody" verifier', function (done) {
32 const body = '{ "id": "realtime_update" }'
33 const signature = 'sha1=c1a072c0aca15c6bd2f5bfae288ff8420e74aa5e'
34 const req = mockRequest(signature, body, 'application/json')
35
36 const parser = bodyParser.json({
37 verify: extractRawBody
38 })
39
40 const middle = middleware({
41 algorithm: 'sha1',
42 secret: 'my_little_secret'
43 })
44
45 parser(req, null, function (err) {
46 expect(typeof err).not.toBe('object')
47 middle(req, null, function (err) {
48 expect(typeof err).not.toBe('object')
49 done()
50 })
51 })
52 })
53})