Skip to content

Commit

Permalink
style: check:fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Lordfirespeed committed Aug 20, 2024
1 parent 4f020f1 commit 5224f92
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 92 deletions.
4 changes: 2 additions & 2 deletions src/content-types/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from './custom'
export * from './json'
export * from "./multipart-form-data"
export * from './multipart-form-data'
export * from './raw'
export * from "./raw-multipart"
export * from './raw-multipart'
export * from './text'
export * from './urlencoded'
12 changes: 6 additions & 6 deletions src/utils/split-multipart.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { Buffer } from 'node:buffer'
import { ClientError } from "@otterhttp/errors";
import { ClientError } from '@otterhttp/errors'

const doubleHyphen = Buffer.from("--")
const CRLF = Buffer.from("\r\n")
const linearWhitespace = Buffer.from(" \t")
const doubleHyphen = Buffer.from('--')
const CRLF = Buffer.from('\r\n')
const linearWhitespace = Buffer.from(' \t')

function isWhitespaceCharCode(charCode: number | undefined): boolean {
return charCode === linearWhitespace.at(0) || charCode === linearWhitespace.at(1)
}

function fail(): never {
throw new ClientError("Invalid multipart data", {
throw new ClientError('Invalid multipart data', {
statusCode: 400,
code: "ERR_INVALID_MULTIPART_DATA",
code: 'ERR_INVALID_MULTIPART_DATA',
})
}

Expand Down
101 changes: 17 additions & 84 deletions tests/parsers/multipart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,157 +59,90 @@ 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')
const multipart = ['preamble', '--boundary', '', '', '--boundary--', 'epilogue'].join('\r\n')

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

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

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

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

expect(parseMultipart(Buffer.from(multipart), 'boundary')).toEqual([
{
headers: {},
content: Buffer.from(""),
}
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')
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')
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')
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')
const multipart = ['preamble', '--boundary', '', '', '--boundary', 'epilogue'].join('\r\n')

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

it('should reject multipart form data with non-linear-whitespace characters on a boundary line', () => {
const multipart = [
'preamble',
'--boundary eeee',
'',
'',
'--boundary--',
'epilogue',
].join('\r\n')
const multipart = ['preamble', '--boundary eeee', '', '', '--boundary--', 'epilogue'].join('\r\n')

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

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

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

it('should reject multipart form data with non-linear-whitespace characters on a boundary line with no preamble', () => {
const multipart = [
'--boundary eeee',
'',
'',
'--boundary--',
'epilogue',
].join('\r\n')
const multipart = ['--boundary eeee', '', '', '--boundary--', 'epilogue'].join('\r\n')

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

0 comments on commit 5224f92

Please sign in to comment.