Skip to content

Commit

Permalink
Feature/allow json list as response (#127)
Browse files Browse the repository at this point in the history
* allow list of objects as response type in validation

* increment version for release
  • Loading branch information
dtobe committed Mar 11, 2024
1 parent 00a0f64 commit 7b7503a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
11 changes: 10 additions & 1 deletion lib/configValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ const responseSchema = {
}
},
data: {
type: 'object'
oneOf: [
{
type: 'object'
},
{
type: 'array',
items: {
type: 'object'
}
}]
},
meta: {
type: 'object',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aietes-js",
"version": "0.10.0",
"version": "0.11.0",
"description": "JSON mock server for NodeJs applications",
"main": "mock-server.js",
"types": "mock-server.d.ts",
Expand Down
7 changes: 7 additions & 0 deletions test/configValidation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ describe('response config validation', () => {
expect(() => validateResponseConfig(validResponseObject, '/foo', 'get')).not.toThrow()
})

it('successfully validates response config with a list of objects as response body', () => {
const validResponseObject = {
data: [{ field1: 1, field2: 'value', field3: false }, { field1: 1, field2: 'value', field3: false }]
}
expect(() => validateResponseConfig(validResponseObject, '/foo', 'get')).not.toThrow()
})

it('successfully validates complex response body config', () => {
const validResponseObject = {
status: 201,
Expand Down
11 changes: 11 additions & 0 deletions test/mock-server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ describe('AietesServer IT', () => {
responseObject2
]
},
'/endpoint-list': {
get: {
data: [{ field1: 'value' }, { field1: 'value2' }]
}
},
'/endpoint-caps': {
GET: {}
}
Expand Down Expand Up @@ -79,6 +84,12 @@ describe('AietesServer IT', () => {
expect(res.body).toMatchObject({ field1: 1, field2: 'value' })
})

it('response body may be a list', async() => {
const res = await request(mockServer.server).get('/endpoint-list')
expect(res.status).toBe(200)
expect(res.body).toMatchObject([{ field1: 'value' }, { field1: 'value2' }])
})

it('should return responses in a list in a round robin fashion', async() => {
let res = await request(mockServer.server).get('/endpoint3')
expect(res.status).toBe(201)
Expand Down

0 comments on commit 7b7503a

Please sign in to comment.