fix: bodyless posts no longer send json content-type (fastify 400 on empty body)
This commit is contained in:
@@ -48,8 +48,9 @@ async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
function post<T>(path: string, payload?: unknown): Promise<T> {
|
||||
return request<T>(path, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: payload === undefined ? undefined : JSON.stringify(payload),
|
||||
...(payload !== undefined
|
||||
? { headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }
|
||||
: {}),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -40,4 +40,19 @@ describe('api additions', () => {
|
||||
expect(err).toBeInstanceOf(ApiError)
|
||||
expect((err as ApiError).code).toBe('no_subsonic_config')
|
||||
})
|
||||
|
||||
it('bodyless posts do not send a json content-type (Fastify 400s on empty json bodies)', async () => {
|
||||
fetchMock.mockClear()
|
||||
fetchMock.mockImplementation(() => jsonOk({ ok: true }))
|
||||
await api.returnLoan(7)
|
||||
await api.markPlayed('a1')
|
||||
await api.logout()
|
||||
await api.triggerBackup()
|
||||
for (const call of fetchMock.mock.calls) {
|
||||
const init = call[1] as RequestInit | undefined
|
||||
const headers = (init?.headers ?? {}) as Record<string, string>
|
||||
expect(headers['Content-Type']).toBeUndefined()
|
||||
expect(init?.body).toBeUndefined()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user