|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +import mockFs from 'mock-fs'; |
| 10 | +import { createReadStream } from 'fs'; |
| 11 | + |
| 12 | +import { getResponsePayloadBytes } from './get_payload_size'; |
| 13 | + |
| 14 | +describe('getPayloadSize', () => { |
| 15 | + describe('handles Buffers', () => { |
| 16 | + test('with ascii characters', () => { |
| 17 | + const payload = 'heya'; |
| 18 | + const result = getResponsePayloadBytes(Buffer.from(payload)); |
| 19 | + expect(result).toBe(4); |
| 20 | + }); |
| 21 | + |
| 22 | + test('with special characters', () => { |
| 23 | + const payload = '¡hola!'; |
| 24 | + const result = getResponsePayloadBytes(Buffer.from(payload)); |
| 25 | + expect(result).toBe(7); |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + describe('handles fs streams', () => { |
| 30 | + afterEach(() => mockFs.restore()); |
| 31 | + |
| 32 | + test('with ascii characters', async () => { |
| 33 | + mockFs({ 'test.txt': 'heya' }); |
| 34 | + const readStream = createReadStream('test.txt'); |
| 35 | + |
| 36 | + let data = ''; |
| 37 | + for await (const chunk of readStream) { |
| 38 | + data += chunk; |
| 39 | + } |
| 40 | + |
| 41 | + const result = getResponsePayloadBytes(readStream); |
| 42 | + expect(result).toBe(Buffer.byteLength(data)); |
| 43 | + }); |
| 44 | + |
| 45 | + test('with special characters', async () => { |
| 46 | + mockFs({ 'test.txt': '¡hola!' }); |
| 47 | + const readStream = createReadStream('test.txt'); |
| 48 | + |
| 49 | + let data = ''; |
| 50 | + for await (const chunk of readStream) { |
| 51 | + data += chunk; |
| 52 | + } |
| 53 | + |
| 54 | + const result = getResponsePayloadBytes(readStream); |
| 55 | + expect(result).toBe(Buffer.byteLength(data)); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('handles plain responses', () => { |
| 60 | + test('when source is text', () => { |
| 61 | + const result = getResponsePayloadBytes('heya'); |
| 62 | + expect(result).toBe(4); |
| 63 | + }); |
| 64 | + |
| 65 | + test('when source contains special characters', () => { |
| 66 | + const result = getResponsePayloadBytes('¡hola!'); |
| 67 | + expect(result).toBe(7); |
| 68 | + }); |
| 69 | + |
| 70 | + test('when source is object', () => { |
| 71 | + const payload = { message: 'heya' }; |
| 72 | + const result = getResponsePayloadBytes(payload); |
| 73 | + expect(result).toBe(JSON.stringify(payload).length); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('handles content-length header', () => { |
| 78 | + test('always provides content-length header if available', () => { |
| 79 | + const headers = { 'content-length': '123' }; |
| 80 | + const result = getResponsePayloadBytes('heya', headers); |
| 81 | + expect(result).toBe(123); |
| 82 | + }); |
| 83 | + |
| 84 | + test('uses first value when hapi header is an array', () => { |
| 85 | + const headers = { 'content-length': ['123', '456'] }; |
| 86 | + const result = getResponsePayloadBytes(null, headers); |
| 87 | + expect(result).toBe(123); |
| 88 | + }); |
| 89 | + |
| 90 | + test('returns undefined if length is NaN', () => { |
| 91 | + const headers = { 'content-length': 'oops' }; |
| 92 | + const result = getResponsePayloadBytes(null, headers); |
| 93 | + expect(result).toBeUndefined(); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + test('defaults to undefined', () => { |
| 98 | + const result = getResponsePayloadBytes(null); |
| 99 | + expect(result).toBeUndefined(); |
| 100 | + }); |
| 101 | +}); |
0 commit comments