fix: harden discogs mappers against malformed payloads, name error classes, assert auth header
This commit is contained in:
@@ -15,17 +15,19 @@ function jsonResponse(body: unknown, status = 200): Response {
|
||||
})
|
||||
}
|
||||
|
||||
function stubFetch(routes: (url: string) => Response): typeof fetch {
|
||||
return (async (input: any) => routes(String(input))) as typeof fetch
|
||||
function stubFetch(routes: (url: string, init?: any) => Response): typeof fetch {
|
||||
return (async (input: any, init?: any) => routes(String(input), init)) as typeof fetch
|
||||
}
|
||||
|
||||
describe('DiscogsClient', () => {
|
||||
it('searches by barcode with token and maps results', async () => {
|
||||
const seen: string[] = []
|
||||
const seenAuth: string[] = []
|
||||
const c = new DiscogsClient(
|
||||
'testtoken',
|
||||
stubFetch((url) => {
|
||||
seen.push(url)
|
||||
stubFetch((_url, init) => {
|
||||
seen.push(_url)
|
||||
seenAuth.push(String(new Headers(init?.headers).get('Authorization')))
|
||||
return jsonResponse(discogsSearchFixture)
|
||||
})
|
||||
)
|
||||
@@ -34,6 +36,7 @@ describe('DiscogsClient', () => {
|
||||
expect(seen[0]).toContain('barcode=5021592210629')
|
||||
expect(seen[0]).toContain('type=release')
|
||||
expect(seen[0]).toContain('token=testtoken')
|
||||
expect(seenAuth[0]).toBe('Discogs token=testtoken')
|
||||
expect(results).toHaveLength(2)
|
||||
expect(results[0]).toEqual({
|
||||
id: 1001,
|
||||
@@ -105,4 +108,13 @@ describe('mappers', () => {
|
||||
{ position: '2', title: 'Theme de Yoyo' },
|
||||
])
|
||||
})
|
||||
|
||||
it('does not throw on malformed payloads', () => {
|
||||
expect(() => mapSearchResult(null)).not.toThrow()
|
||||
expect(() => mapRelease(undefined)).not.toThrow()
|
||||
expect(mapRelease({ tracklist: [null, { title: 'X' }] }).tracklist).toEqual([{ position: '', title: 'X' }])
|
||||
expect(mapSearchResult({ format: [null, 'CD'] }).formats).toEqual(['CD'])
|
||||
expect(new DiscogsAuthError().name).toBe('DiscogsAuthError')
|
||||
expect(new DiscogsRateLimitError().name).toBe('DiscogsRateLimitError')
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user