it("should decompress with 'deflateRaw'", () => { const mw = Middleware({ method: "deflateRaw", threshold: 0 }); mw.created(broker); const meta = {}; const next = jest.fn(); const receive = mw.transporterReceive.call(broker, next); const compressedData = Buffer.concat([COMPRESSED_FLAG, zlib.deflateRawSync(Buffer.from("compressed data"))]); return receive("topic", compressedData, meta).catch(protectReject).then(() => { expect(next).toHaveBeenCalledTimes(1); expect(next).toHaveBeenCalledWith("topic", Buffer.from("compressed data"), meta); }); });
beforeEach(() => { msgHandler = jest.fn(); transporter = new Amqp10Transporter({ url: "amqp10://localhost" }); transporter.init(new Transit(new ServiceBroker({ logger: false, namespace: "TEST" })), msgHandler); transporter.serialize = jest.fn(() => Buffer.from("json data")); return transporter.connect(); });
describe('Protocol > Requests > EndTxn > v0', () => { test('request', async () => { const { buffer } = await RequestV0Protocol({ transactionalId: 'test-transactional-id', producerId: '1001', producerEpoch: 0, transactionResult: true, }).encode() expect(buffer).toEqual(Buffer.from(require('../fixtures/v0_request.json'))) }) })
it("should compress with 'gzip'", () => { const mw = Middleware({ method: "gzip", threshold: 0 }); mw.created(broker); const meta = {}; const next = jest.fn(() => Promise.resolve()); const send = mw.transporterSend.call(broker, next); return send("topic", Buffer.from("uncompressed data"), meta).catch(protectReject).then(() => { expect(next).toHaveBeenCalledTimes(1); expect(next).toHaveBeenCalledWith("topic", expect.any(Buffer), meta); expect(next.mock.calls[0][1]).toEqual(Buffer.concat([COMPRESSED_FLAG, zlib.gzipSync(Buffer.from("uncompressed data"))])); }); });
describe('response with mixed formats (0.10 MessageSet + 0.11 RecordBatch)', () => { test('decode only the 0.10 messages, 0.11 should be decoded on the next request', async () => { const data = await decode(Buffer.from(require('../fixtures/v4_response_mixed_formats.json'))) const messagesMagicBytes = data.responses[0].partitions[0].messages.map(m => m.magicByte) // the fixture is too big, jest deepCheck matcher takes too long to compare the object, // and the purpose of the test is to check if the decoder can skip the 0.11 on this request expect(new Set(messagesMagicBytes)).toEqual(new Set([1])) }) })
describe('Protocol > Requests > Heartbeat > v0', () => { test('response', async () => { const data = await decode(Buffer.from(require('../fixtures/v0_response.json'))) expect(data).toEqual({ errorCode: 0 }) await expect(parse(data)).resolves.toBeTruthy() }) test('throws KafkaJSProtocolError if the api is not supported', async () => { await expect(decode(unsupportedVersionResponse())).rejects.toThrow( /The version of API is not supported/ ) }) })
it("should decompress with 'deflate'", () => { const mw = Middleware({ threshold: 0 }); mw.created(broker); const meta = {}; const next = jest.fn(); const receive = mw.transporterReceive.call(broker, next); const compressedData = Buffer.concat([COMPRESSED_FLAG, zlib.deflateSync(Buffer.from("compressed data"))]); return receive("topic", compressedData, meta).catch(protectReject).then(() => { expect(next).toHaveBeenCalledTimes(1); expect(next).toHaveBeenCalledWith("topic", Buffer.from("compressed data"), meta); }); });
it("should decompress with 'gzip'", () => { const mw = Middleware({ method: "gzip", threshold: 0 }); mw.created(broker); const meta = {}; const next = jest.fn(); const receive = mw.transporterReceive.call(broker, next); const compressedData = Buffer.concat([COMPRESSED_FLAG, zlib.gzipSync(Buffer.from("compressed data"))]); return receive("topic", compressedData, meta).catch(protectReject).then(() => { expect(next).toHaveBeenCalledTimes(1); expect(next).toHaveBeenCalledWith("topic", Buffer.from("compressed data"), meta); }); });
describe('Protocol > Requests > Heartbeat > v0', () => { test('request', async () => { const groupId = 'consumer-group-id-ba8da1f6117562ed5615' const memberId = 'test-169232b069c4a377bc4b-040f5f1a-a469-4062-9d36-bd803d8d6767' const groupGenerationId = 1 const { buffer } = await RequestV0Protocol({ groupId, groupGenerationId, memberId }).encode() expect(buffer).toEqual(Buffer.from(require('../fixtures/v0_request.json'))) }) })
describe('Protocol > Requests > LeaveGroup > v0', () => { test('request', async () => { const groupId = 'consumer-group-id-64fbf5dce5065868aa8f' const memberId = 'test-45eb7a4239f548578e8b-b2b08fa3-b887-4719-b9e1-391ec944b53f' const { buffer } = await RequestV0Protocol({ groupId, memberId }).encode() expect(buffer).toEqual(Buffer.from(require('../fixtures/v0_request.json'))) }) })
describe('Protocol > Requests > LeaveGroup > v1', () => { test('request', async () => { const { buffer } = await RequestV0Protocol({ groupId: 'consumer-group-id-82d77df5d0974e21502d-30919-0ec5e55e-e3e1-433a-bbed-96fe228408b4', memberId: 'test-c598169a5d8dbedcb806-30919-ff1f3c53-1855-4c04-aadf-12d298160f5c-b41b37f8-6482-47c5-811e-e658ab656a75', }).encode() expect(buffer).toEqual(Buffer.from(require('../fixtures/v1_request.json'))) }) })