Skip to content

Commit

Permalink
test(parsers/multipart): add more tests to ensure RFC 2046 is adhered to
Browse files Browse the repository at this point in the history
  • Loading branch information
Lordfirespeed committed Aug 20, 2024
1 parent 49d21ad commit 9975310
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions tests/parsers/multipart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,112 @@ it('should parse valid multipart with multiple parts', () => {
},
])
})

it('should parse valid multipart form data with blank part', () => {
const multipart = [
'preamble',
'--boundary',
'',
'',
'--boundary--',
'epilogue',
].join('\r\n')

expect(parseMultipart(Buffer.from(multipart), 'boundary')).toEqual([
{
headers: {},
content: Buffer.from(""),
}
])
})

it('should parse valid multipart form data with no preamble', () => {
const multipart = [
'--boundary',
'',
'',
'--boundary--',
'epilogue',
].join('\r\n')

expect(parseMultipart(Buffer.from(multipart), 'boundary')).toEqual([
{
headers: {},
content: Buffer.from(""),
}
])
})

it('should parse valid multipart form data with no epilogue', () => {
const multipart = [
'preamble',
'--boundary',
'',
'',
'--boundary--',
].join('\r\n')

expect(parseMultipart(Buffer.from(multipart), 'boundary')).toEqual([
{
headers: {},
content: Buffer.from(""),
}
])
})

it('should reject multipart form data with missing CRLF to separate headerlines from content', () => {
const multipart = [
'preamble',
'--boundary',
'',
'--boundary--',
'epilogue',
].join('\r\n')

expect(() => {
parseMultipart(Buffer.from(multipart), 'boundary')
}).toThrow()
})

it('should reject multipart form data with invalid part header-lines', () => {
const multipart = [
'preamble',
'--boundary',
'--boundary',
'',
'foobar',
'--boundary--',
'epilogue',
].join('\r\n')

expect(() => {
parseMultipart(Buffer.from(multipart), 'boundary')
}).toThrow()
})

it('should reject multipart form data with no parts', () => {
const multipart = [
'preamble',
'--boundary--',
'epilogue'
].join('\r\n')

expect(() => {
parseMultipart(Buffer.from(multipart), 'boundary')
}).toThrow()
})

it('should reject multipart form data with no close delimiter', () => {
const multipart = [
'preamble',
'--boundary',
'',
'',
'--boundary',
'epilogue',
].join('\r\n')

expect(() => {
parseMultipart(Buffer.from(multipart), 'boundary')
}).toThrow()
})

0 comments on commit 9975310

Please sign in to comment.