|
| 1 | +import { assertEquals, assertThrows } from '@std/assert' |
| 2 | +import { parseChunkedBody } from './parse_chunked_body.ts'; |
| 3 | +import { InvalidChunkSizeError, MalformedChunkedEncodingError } from '../../error/index.ts'; |
| 4 | + |
| 5 | +Deno.test('Tests "parseChunkedBody"', async (test) => { |
| 6 | + await test.step({ |
| 7 | + name: 'Parse valid chunked body.', |
| 8 | + fn() : void { |
| 9 | + const responseContent = '6\r\nHello \r\n6\r\nWorld!\r\n0\r\n\r\n' |
| 10 | + assertEquals( |
| 11 | + parseChunkedBody(responseContent), |
| 12 | + 'Hello World!' |
| 13 | + ) |
| 14 | + } |
| 15 | + }) |
| 16 | + |
| 17 | + await test.step({ |
| 18 | + name: 'Parse invalid chunked body. Wrong chunk size.', |
| 19 | + fn() : void { |
| 20 | + const responseContent = '6\r\nHello \r\n6\r\nWor\r\n0\r\n\r\n' |
| 21 | + assertThrows( |
| 22 | + () => parseChunkedBody(responseContent), |
| 23 | + InvalidChunkSizeError, |
| 24 | + 'Invalid chunk size' |
| 25 | + ) |
| 26 | + } |
| 27 | + }) |
| 28 | + |
| 29 | + await test.step({ |
| 30 | + name: 'Parse invalid chunked body. Missing chunk size delimiter.', |
| 31 | + fn() : void { |
| 32 | + const responseContent = '6' |
| 33 | + assertThrows( |
| 34 | + () => parseChunkedBody(responseContent), |
| 35 | + MalformedChunkedEncodingError, |
| 36 | + 'Malformed chunked encoding: missing chunk size delimiter' |
| 37 | + ) |
| 38 | + } |
| 39 | + }) |
| 40 | + |
| 41 | + await test.step({ |
| 42 | + name: 'Parse invalid chunked body. Chunk size to small for chunk.', |
| 43 | + fn() : void { |
| 44 | + const responseContent = '6\r\nHello \r\n2\r\nWorld!\r\n0\r\n\r\n' |
| 45 | + assertThrows( |
| 46 | + () => parseChunkedBody(responseContent), |
| 47 | + MalformedChunkedEncodingError, |
| 48 | + 'Malformed chunked encoding: missing chunk data delimiter' |
| 49 | + ) |
| 50 | + } |
| 51 | + }) |
| 52 | + |
| 53 | + await test.step({ |
| 54 | + name: 'Parse invalid chunked body. Chunk size to big for chunk (exceeds end).', |
| 55 | + fn() : void { |
| 56 | + const responseContent = '6\r\nHello \r\n20\r\nWorld!\r\n0\r\n\r\n' |
| 57 | + assertThrows( |
| 58 | + () => parseChunkedBody(responseContent), |
| 59 | + MalformedChunkedEncodingError, |
| 60 | + 'Malformed chunked encoding: incomplete chunk data' |
| 61 | + ) |
| 62 | + } |
| 63 | + }) |
| 64 | + |
| 65 | + await test.step({ |
| 66 | + name: 'Parse invalid chunked body. Missing 0 at end.', |
| 67 | + fn() : void { |
| 68 | + const responseContent = '6\r\nHello \r\n2\r\nWor\r\n' |
| 69 | + assertThrows( |
| 70 | + () => parseChunkedBody(responseContent), |
| 71 | + MalformedChunkedEncodingError, |
| 72 | + 'Malformed chunked encoding: missing chunk data delimiter' |
| 73 | + ) |
| 74 | + } |
| 75 | + }) |
| 76 | + |
| 77 | + await test.step({ |
| 78 | + name: 'Parse invalid chunked body. Missing \\r\\n\\r\\n at end.', |
| 79 | + fn() : void { |
| 80 | + const responseContent = '6\r\nHello \r\n6\r\nWorld!\r\n0' |
| 81 | + assertThrows( |
| 82 | + () => parseChunkedBody(responseContent), |
| 83 | + MalformedChunkedEncodingError, |
| 84 | + 'Malformed chunked encoding: missing chunk size delimiter' |
| 85 | + ) |
| 86 | + } |
| 87 | + }) |
| 88 | +}) |
0 commit comments