Skip to content

Commit

Permalink
test(content-disposition): prefer expect().toThrow to try ... catch
Browse files Browse the repository at this point in the history
  • Loading branch information
Lordfirespeed committed Aug 10, 2024
1 parent 61b5c50 commit eb09bf4
Showing 1 changed file with 47 additions and 97 deletions.
144 changes: 47 additions & 97 deletions tests/modules/content-disposition.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it, vi } from 'vitest'
import { describe, expect, it, onTestFinished, vi } from 'vitest'

import { ContentDisposition, contentDisposition, parse } from '@/packages/content-disposition/src'

Expand All @@ -17,12 +17,9 @@ describe('contentDisposition(filename)', () => {
expect(contentDisposition('/path/to/plans.pdf')).toBe('attachment; filename="plans.pdf"')
})
it('should throw an error when non latin fallback is used', () => {
expect.assertions(1)
try {
expect(() => {
contentDisposition('index.ht', { type: 'html', fallback: 'ÇŞ' })
} catch (e) {
expect((e as Error).message).toBe('fallback must be ISO-8859-1 string')
}
}).toThrow('fallback must be ISO-8859-1 string')
})
it('should use hasfallback', () => {
expect(contentDisposition('index.ht', { type: 'html', fallback: 'html' })).toEqual(
Expand All @@ -40,12 +37,9 @@ describe('contentDisposition(filename)', () => {
)
})
it('should throw an error when non string options.type is used', () => {
expect.assertions(1)
try {
expect(() => {
contentDisposition('index.ht', { type: { test: 'test' } as unknown as string })
} catch (e) {
expect((e as Error).message).toBe('invalid type')
}
}).toThrow('invalid type')
})
describe('when "filename" is US-ASCII', () => {
it('should only include filename parameter', () => {
Expand All @@ -61,18 +55,14 @@ describe('contentDisposition(filename)', () => {
describe('parse(string)', () => {
describe('with type', () => {
it('should throw on quoted value', () => {
try {
expect(() => {
parse('"attachment"')
} catch (e) {
expect(e.message).toBe('invalid type format')
}
}).toThrow('invalid type format')
})
it('should throw on trailing semi', () => {
try {
expect(() => {
parse('attachment;')
} catch (e) {
expect(e.message).toBe('invalid parameter format')
}
}).toThrow('invalid parameter format')
})
it('should parse "attachment"', () => {
expect(parse('attachment')).toStrictEqual(new ContentDisposition('attachment', {}))
Expand All @@ -92,63 +82,43 @@ describe('parse(string)', () => {
})
describe('with parameters', () => {
it('should throw on trailing semi', () => {
try {
expect(() => {
parse('attachment; filename="rates.pdf";')
} catch (e) {
expect(e.message).toBe('invalid parameter format')
}
}).toThrow('invalid parameter format')
})
it('should throw on invalid param name', () => {
try {
expect(() => {
parse('attachment; filename@="rates.pdf"')
} catch (e) {
expect(e.message).toBe('invalid parameter format')
}
}).toThrow('invalid parameter format')
})
it('should throw on missing param value', () => {
try {
expect(() => {
parse('attachment; filename=')
} catch (e) {
expect(e.message).toBe('invalid parameter format')
}
}).toThrow('invalid parameter format')
})

it('should reject invalid parameter value', () => {
try {
expect(() => {
parse('attachment; filename=trolly,trains')
} catch (e) {
expect(e.message).toMatch(/invalid parameter format/)
}
}).toThrow(/invalid parameter format/)
})

it('should reject invalid parameters', () => {
try {
expect(() => {
parse('attachment; filename=total/; foo=bar')
} catch (e) {
expect(e.message).toMatch(/invalid parameter format/)
}
}).toThrow(/invalid parameter format/)
})

it('should reject duplicate parameters', () => {
try {
expect(() => {
parse('attachment; filename=foo; filename=bar')
} catch (e) {
expect(e.message).toMatch(/invalid duplicate parameter/)
}
}).toThrow(/invalid duplicate parameter/)
})

it('should reject missing type', () => {
try {
parse('filename="plans.pdf"')
} catch (e) {
expect(e.message).toMatch(/invalid type format/)
}

try {
parse('; filename="plans.pdf"')
} catch (e) {
expect(e.message).toMatch(/invalid type format/)
}
it.each(['filename="plans.pdf"', '; filename="plans.pdf"'])('should reject missing type', (value: string) => {
expect(() => {
parse(value)
}).toThrow(/invalid type format/)
})

it('should lower-case parameter name', () => {
Expand Down Expand Up @@ -198,27 +168,21 @@ describe('parse(string)', () => {
parameters: { filename: '£ rates.pdf' }
})
})
it('should throw error if decodedURI throws an error', () => {
const cutomdecodeURIComponent = (encodedURIComponent: string) => {
it('should throw error if decodeURI throws an error', () => {
onTestFinished(() => vi.unstubAllGlobals())
vi.stubGlobal('decodeURIComponent', (encodedURIComponent: string) => {
throw encodedURIComponent
}
vi.stubGlobal('decodeURIComponent', cutomdecodeURIComponent)
expect.assertions(1)
try {
})
expect(() => {
parse("attachment; filename*=UTF-8'en'%E2%82%AC%20rates.pdf")
} catch (error) {
expect(error).toBeDefined()
}
vi.unstubAllGlobals()
}).toThrow()
})
})
describe('with extended parameters', () => {
it('should reject quoted extended parameter value', () => {
try {
expect(() => {
parse('attachment; filename*="UTF-8\'\'%E2%82%AC%20rates.pdf"')
} catch (e) {
expect(e.message).toMatch(/invalid extended.*value/)
}
}).toThrow(/invalid extended.*value/)
})

it('should parse UTF-8 extended parameter value', () => {
Expand Down Expand Up @@ -247,11 +211,9 @@ describe('parse(string)', () => {
})

it('should reject unsupported charset', () => {
try {
expect(() => {
parse("attachment; filename*=ISO-8859-2''%A4%20rates.pdf")
} catch (e) {
expect(e.message).toMatch(/unsupported charset/)
}
}).toThrow(/unsupported charset/)
})

it('should parse with embedded language', () => {
Expand Down Expand Up @@ -282,11 +244,9 @@ describe('parse(string)', () => {
})

it('should reject ""inline""', () => {
try {
expect(() => {
parse('"inline"')
} catch (e) {
expect(e.message).toMatch(/invalid type format/)
}
}).toThrow(/invalid type format/)
})

it('should parse "inline; filename="foo.html""', () => {
Expand Down Expand Up @@ -319,11 +279,9 @@ describe('parse(string)', () => {
})

it('should reject ""attachment""', () => {
try {
expect(() => {
parse('"attachment')
} catch (e) {
expect(e.message).toMatch(/invalid type format/)
}
}).toThrow(/invalid type format/)
})

it('should parse "ATTACHMENT"', () => {
Expand Down Expand Up @@ -404,35 +362,27 @@ describe('parse(string)', () => {
})

it('should reject "attachment; filename=foo,bar.html"', () => {
try {
expect(() => {
parse('attachment; filename=foo,bar.html')
} catch (e) {
expect(e.message).toMatch(/invalid parameter format/)
}
}).toThrow(/invalid parameter format/)
})

it('should reject "attachment; filename=foo.html ;"', () => {
try {
expect(() => {
parse('attachment; filename=foo.html ;')
} catch (e) {
expect(e.message).toMatch(/invalid parameter format/)
}
}).toThrow(/invalid parameter format/)
})

it('should reject "attachment; ;filename=foo"', () => {
try {
expect(() => {
parse('attachment; ;filename=foo')
} catch (e) {
expect(e.message).toMatch(/invalid parameter format/)
}
}).toThrow(/invalid parameter format/)
})

it('should reject "attachment; filename=foo bar.html"', () => {
try {
expect(() => {
parse('attachment; filename=foo bar.html')
} catch (e) {
expect(e.message).toMatch(/invalid parameter format/)
}
}).toThrow(/invalid parameter format/)
})

it("should parse \"attachment; filename='foo.bar'", () => {
Expand Down

0 comments on commit eb09bf4

Please sign in to comment.