Skip to content

Commit

Permalink
test default response in oas2 too
Browse files Browse the repository at this point in the history
  • Loading branch information
XVincentX committed Apr 18, 2019
1 parent 6fd666b commit 27b2dc8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
18 changes: 18 additions & 0 deletions examples/petstore.oas2.json
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,24 @@
"schema": {
"$ref": "#/definitions/Pet"
}
},
"default": {
"description": "default response",
"schema": {
"required": [
"code",
"message"
],
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
}
}
}
}
},
"security": [
Expand Down
51 changes: 17 additions & 34 deletions packages/http-server/src/__tests__/server.oas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,39 +138,6 @@ describe.each([['petstore.oas2.json'], ['petstore.oas3.json']])('server %s', fil
});
expect(response.statusCode).toBe(400);
});
});

describe('oas2 specific tests', () => {
test('should return response even if there is no content defined in spec', async () => {
const server = createServer({}, { components: {}, config: { mock: true } });
await server.prism.load({
path: resolve(__dirname, 'fixtures', 'no-responses.oas2.yaml'),
});

const response = await server.fastify.inject({ method: 'GET', url: '/' });

expect(response.statusCode).toBe(200);
expect(response.headers['content-type']).toEqual('text/plain');
expect(response.payload).toEqual('');

await server.fastify.close();
});
});

describe('oas3 specific', () => {
let server: IPrismHttpServer<{}>;

beforeAll(async () => {
server = createServer({}, { components: {}, config: { mock: true } });
await server.prism.load({
path: relative(
process.cwd(),
resolve(__dirname, '..', '..', '..', '..', 'examples', 'petstore.oas3.json')
),
});
});

afterAll(() => server.fastify.close());

test('will return the default response when using the __code property with a non existing code', async () => {
const response = await server.fastify.inject({
Expand All @@ -179,7 +146,6 @@ describe('oas3 specific', () => {
});

expect(response.statusCode).toBe(499);

const payload = JSON.parse(response.payload);
expect(payload).toHaveProperty('code');
expect(payload).toHaveProperty('message');
Expand All @@ -194,3 +160,20 @@ describe('oas3 specific', () => {
expect(response.statusCode).toBe(500);
});
});

describe('oas2 specific tests', () => {
test('should return response even if there is no content defined in spec', async () => {
const server = createServer({}, { components: {}, config: { mock: true } });
await server.prism.load({
path: resolve(__dirname, 'fixtures', 'no-responses.oas2.yaml'),
});

const response = await server.fastify.inject({ method: 'GET', url: '/' });

expect(response.statusCode).toBe(200);
expect(response.headers['content-type']).toEqual('text/plain');
expect(response.payload).toEqual('');

await server.fastify.close();
});
});

0 comments on commit 27b2dc8

Please sign in to comment.