Skip to content

Commit

Permalink
fix(kitsu-core): Allow empty POST body
Browse files Browse the repository at this point in the history
  • Loading branch information
pedep authored and wopian committed Feb 28, 2023
1 parent 3668721 commit 16cd20d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/kitsu-core/src/serialise/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function isValid (isArray, type, payload, method) {
}
}
} else {
if (typeof payload !== 'object' || Object.keys(payload).length === 0) {
if (typeof payload !== 'object' || (method !== 'POST' && Object.keys(payload).length === 0)) {
throw new Error(`${method} requires an object or array body`)
}
// A POST request is the only request to not require an ID in spec
Expand Down
16 changes: 13 additions & 3 deletions packages/kitsu-core/src/serialise/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,20 @@ describe('kitsu-core', () => {
})
})

it('throws an error if obj is missing', () => {
it('does not throws an error if obj is missing when method is POST', () => {
expect.assertions(1)
expect(() => serialise('post'))
.toThrowError('POST requires an object or array body')
const input = serialise('anime', { }, 'POST')
expect(input).toEqual({
data: {
type: 'anime'
}
})
})

it('throws an error if obj is missing when method is not POST', () => {
expect.assertions(1)
expect(() => serialise('post', {}, 'PATCH'))
.toThrowError('PATCH requires an object or array body')
})

it('throws an error if obj is not an Object', () => {
Expand Down
37 changes: 34 additions & 3 deletions packages/kitsu/src/post.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,45 @@ describe('kitsu', () => {
await expect(await api.post('anime', { id: 123456789, type: 'anime', name: 'Name' })).toBeUndefined()
})

it('throws an error if missing a JSON object body', async () => {
expect.assertions(1)
it('throws an error if missing a valid JSON object body', async () => {
expect.assertions(2)
const api = new Kitsu()
try {
await api.post('posts')
await api.post('posts', 123)
} catch (err) {
expect(err.message).toEqual('POST requires an object or array body')
}

try {
await api.post('posts', 'invalid body')
} catch (err) {
expect(err.message).toEqual('POST requires an object or array body')
}
})

it('sends data in request if missing a JSON object body', async () => {
expect.assertions(4)
const api = new Kitsu()
mock.onPost('/anime').reply(config => {
expect(JSON.parse(config.data)).toEqual({
data: { type: 'anime' }
})
return [ 200 ]
})
await expect(await api.post('anime')).toBeUndefined()
await expect(await api.post('anime', {})).toBeUndefined()
})

it('sends data in request if given empty JSON object in array body', async () => {
expect.assertions(2)
const api = new Kitsu()
mock.onPost('/anime').reply(config => {
expect(JSON.parse(config.data)).toEqual({
data: [ { type: 'anime' } ]
})
return [ 200 ]
})
await expect(await api.post('anime', [ {} ])).toBeUndefined()
})
})
})

0 comments on commit 16cd20d

Please sign in to comment.