| 1 | import { Readable |
| 2 | import { buffer as readStream } from 'node:stream/consumers'; |
| 3 | import { gzipSync } from 'node:zlib'; |
| 4 | import { describe, expect, it, vi } from 'vitest'; |
| 5 |
| 6 | import { GitRawBodyMiddleware } from './git-raw-body.middleware.js'; |
| 7 | import type { GitAuthenticatedBufferedRequest } from '../types.js'; |
| 8 |
| 9 | function request(body: Buffer, headers: Record<string, string> = {}) { |
| 10 | return Object.assign(Readable.from(body), { |
| 11 | headers, |
| 12 | }) as unknown as GitAuthenticatedBufferedRequest; |
| 13 | } |
| 14 |
| 15 | describe('GitRawBodyMiddleware', () => { |
| 16 | const middleware = new GitRawBodyMiddleware(); |
| 17 | const response = { on: () => {} } as never; |
| 18 |
| 19 | it('spools the request before the handler runs', async () => { |
| 20 | const req = request(Buffer.from('0032command\n0000PACK')); |
| 21 | const next = vi.fn(); |
| 22 |
| 23 | await middleware.use(req, response, next); |
| 24 |
| 25 | expect((await readStream(req.gitBody!.open())).toString()).toBe( |
| 26 | '0032command\n0000PACK', |
| 27 | ); |
| 28 | expect(next).toHaveBeenCalledWith(); |
| 29 | }); |
| 30 |
| 31 | it('inflates a gzipped body', async () => { |
| 32 | const raw = Buffer.from('0032command\n0000PACK'); |
| 33 | const req = request(gzipSync(raw), { 'content-encoding': 'gzip' }); |
| 34 |
| 35 | await middleware.use(req, response, vi.fn()); |
| 36 |
| 37 | expect(await readStream(req.gitBody!.open())).toEqual(raw); |
| 38 | }); |
| 39 |
| 40 | it('passes a decode failure to the error handler', async () => { |
| 41 | const req = request(Buffer.from('not gzip'), { |
| 42 | 'content-encoding': 'gzip', |
| 43 | }); |
| 44 | const next = vi.fn(); |
| 45 |
| 46 | await middleware.use(req, response, next); |
| 47 |
| 48 | expect(next).toHaveBeenCalledWith(expect.any(Error)); |
| 49 | }); |
| 50 | }); |