Skip to content
This repository has been archived by the owner on Jun 20, 2022. It is now read-only.

Commit

Permalink
fix: Return 200 with empty array for searches with no results.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shogun committed May 16, 2018
1 parent c1fb32c commit 2e96b0b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
10 changes: 2 additions & 8 deletions packages/trail-hapi-plugin/lib/routes/trails.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ module.exports = {
async handler (request, h) {
const { from, to, who, what, subject, page, pageSize, sort } = request.query

const results = await request.trailCore.search({ from, to, who, what, subject, page, pageSize, sort })

return results.length ? results : h.response().code(204)
return request.trailCore.search({ from, to, who, what, subject, page, pageSize, sort })
},
config: {
description: 'Search audit trails.',
Expand All @@ -44,7 +42,6 @@ module.exports = {
200: Joi.array()
.description('The search results.')
.items(trailSchema.response),
204: Joi.empty().description('No trails found.'),
422: errorsSchemas['422'],
500: errorsSchemas['500']
}
Expand All @@ -58,9 +55,7 @@ module.exports = {
async handler (request, h) {
const { from, to, type, page, pageSize, desc } = request.query

const results = await request.trailCore.enumerate({ from, to, type, page, pageSize, desc })

return results.length ? results : h.response().code(204)
return request.trailCore.enumerate({ from, to, type, page, pageSize, desc })
},
config: {
description: 'Enumerate audit trails ids.',
Expand All @@ -75,7 +70,6 @@ module.exports = {
200: Joi.array()
.description('The enumeration results.')
.items(Joi.string().description('A trail who, what or subject id')),
204: Joi.empty().description('No ids found.'),
422: errorsSchemas['422'],
500: errorsSchemas['500']
}
Expand Down
20 changes: 12 additions & 8 deletions packages/trail-hapi-plugin/test/routes/trails.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('Trails REST operations', () => {
await server.trailCore.delete(id)
})

test('it should search trails and return no body with 204 when no records are found', async () => {
test('it should search trails and return a empty array when no records are found', async () => {
await server.trailCore.performDatabaseOperations(client => client.query('TRUNCATE trails'))

const id = await server.trailCore.insert({
Expand All @@ -93,8 +93,10 @@ describe('Trails REST operations', () => {
url: `/trails?from=${encodeURIComponent('2014-01-02T18:04:05.123+03:00')}&to=${encodeURIComponent('2018-01-02T18:04:05.123+03:00')}&who=foo`
})

expect(response.statusCode).toEqual(204)
expect(response.payload).toEqual('')
expect(response.statusCode).toEqual(200)
const trails = JSON.parse(response.payload)

expect(trails).toEqual([])

await server.trailCore.delete(id)
})
Expand Down Expand Up @@ -136,14 +138,14 @@ describe('Trails REST operations', () => {
})

expect(response.statusCode).toEqual(200)
const trails = JSON.parse(response.payload)
const enumeration = JSON.parse(response.payload)

expect(trails).toEqual(['1'])
expect(enumeration).toEqual(['1'])

await server.trailCore.delete(id)
})

test('it should enumerate trails and return no body with 204 when no records are found', async () => {
test('it should enumerate trails and return a empty array when no records are found', async () => {
await server.trailCore.performDatabaseOperations(client => client.query('TRUNCATE trails'))

const id = await server.trailCore.insert({
Expand All @@ -158,8 +160,10 @@ describe('Trails REST operations', () => {
url: `/trails/enumerate?from=${encodeURIComponent('2014-01-02T18:04:05.123+03:00')}&to=${encodeURIComponent('2015-01-02T18:04:05.123+03:00')}&type=who`
})

expect(response.statusCode).toEqual(204)
expect(response.payload).toEqual('')
expect(response.statusCode).toEqual(200)
const enumeration = JSON.parse(response.payload)

expect(enumeration).toEqual([])

await server.trailCore.delete(id)
})
Expand Down

0 comments on commit 2e96b0b

Please sign in to comment.