|
| 1 | +/** |
| 2 | + * Tests for raw body buffer capture functionality |
| 3 | + * This ensures exact bytes are preserved for v4 HMAC authentication |
| 4 | + * @prettier |
| 5 | + */ |
| 6 | +import 'should'; |
| 7 | +import 'should-http'; |
| 8 | +import 'should-sinon'; |
| 9 | +import '../../lib/asserts'; |
| 10 | +import * as sinon from 'sinon'; |
| 11 | +import * as express from 'express'; |
| 12 | +import { agent as supertest, Response } from 'supertest'; |
| 13 | +import { app as expressApp } from '../../../src/expressApp'; |
| 14 | + |
| 15 | +describe('Raw Body Buffer Capture', () => { |
| 16 | + const sandbox = sinon.createSandbox(); |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + sandbox.verifyAndRestore(); |
| 20 | + }); |
| 21 | + |
| 22 | + describe('body-parser verify callback', () => { |
| 23 | + let agent: ReturnType<typeof supertest>; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + const app = expressApp({ |
| 27 | + env: 'test', |
| 28 | + disableProxy: true, |
| 29 | + } as any); |
| 30 | + |
| 31 | + // Add a test route that returns the rawBodyBuffer info |
| 32 | + app.post('/test/rawbody', (req: express.Request, res: express.Response) => { |
| 33 | + res.json({ |
| 34 | + hasRawBodyBuffer: !!req.rawBodyBuffer, |
| 35 | + rawBodyBufferLength: req.rawBodyBuffer?.length || 0, |
| 36 | + rawBodyBufferContent: req.rawBodyBuffer?.toString('utf-8') || null, |
| 37 | + parsedBody: req.body, |
| 38 | + bodyKeysCount: Object.keys(req.body || {}).length, |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + agent = supertest(app); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should capture raw body buffer on POST requests', async () => { |
| 46 | + const testBody = { address: 'tb1qtest', amount: 100000 }; |
| 47 | + |
| 48 | + const res: Response = await agent.post('/test/rawbody').set('Content-Type', 'application/json').send(testBody); |
| 49 | + |
| 50 | + res.status.should.equal(200); |
| 51 | + res.body.hasRawBodyBuffer.should.equal(true); |
| 52 | + res.body.rawBodyBufferLength.should.be.greaterThan(0); |
| 53 | + res.body.parsedBody.should.deepEqual(testBody); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should preserve exact bytes including whitespace', async () => { |
| 57 | + // JSON with extra whitespace that would be lost during parse/re-serialize |
| 58 | + const bodyWithWhitespace = '{"address": "tb1qtest", "amount":100000}'; |
| 59 | + |
| 60 | + const res: Response = await agent |
| 61 | + .post('/test/rawbody') |
| 62 | + .set('Content-Type', 'application/json') |
| 63 | + .send(bodyWithWhitespace); |
| 64 | + |
| 65 | + res.status.should.equal(200); |
| 66 | + // Raw buffer should preserve the exact whitespace |
| 67 | + res.body.rawBodyBufferContent.should.equal(bodyWithWhitespace); |
| 68 | + // Parsed body should have the correct values |
| 69 | + res.body.parsedBody.address.should.equal('tb1qtest'); |
| 70 | + res.body.parsedBody.amount.should.equal(100000); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should preserve exact key ordering', async () => { |
| 74 | + // JSON with specific key ordering |
| 75 | + const bodyWithOrdering = '{"z_last":"last","a_first":"first","m_middle":"middle"}'; |
| 76 | + |
| 77 | + const res: Response = await agent |
| 78 | + .post('/test/rawbody') |
| 79 | + .set('Content-Type', 'application/json') |
| 80 | + .send(bodyWithOrdering); |
| 81 | + |
| 82 | + res.status.should.equal(200); |
| 83 | + // Raw buffer should preserve exact key ordering |
| 84 | + res.body.rawBodyBufferContent.should.equal(bodyWithOrdering); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should handle empty body', async () => { |
| 88 | + const res: Response = await agent.post('/test/rawbody').set('Content-Type', 'application/json').send({}); |
| 89 | + |
| 90 | + res.status.should.equal(200); |
| 91 | + res.body.hasRawBodyBuffer.should.equal(true); |
| 92 | + res.body.rawBodyBufferLength.should.equal(2); // "{}" |
| 93 | + }); |
| 94 | + |
| 95 | + it('should handle nested JSON objects', async () => { |
| 96 | + const nestedBody = { |
| 97 | + level1: { |
| 98 | + level2: { |
| 99 | + level3: { |
| 100 | + value: 'deep', |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }; |
| 105 | + |
| 106 | + const res: Response = await agent.post('/test/rawbody').set('Content-Type', 'application/json').send(nestedBody); |
| 107 | + |
| 108 | + res.status.should.equal(200); |
| 109 | + res.body.hasRawBodyBuffer.should.equal(true); |
| 110 | + res.body.parsedBody.level1.level2.level3.value.should.equal('deep'); |
| 111 | + }); |
| 112 | + }); |
| 113 | +}); |
0 commit comments