Skip to content

Commit

Permalink
feat: add tests and modify error response message
Browse files Browse the repository at this point in the history
SL-732
  • Loading branch information
Chris Miaskowski committed Dec 17, 2018
1 parent f4d8b1e commit 73db545
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 106 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Http Prism Instance function tests given correct route should return correct response 1`] = `
Object {
"input": Object {
"method": "get",
"url": Object {
"path": "/pet/findByStatus",
"query": Object {
"status": Array [
"available",
"pending",
],
},
},
},
"output": Object {
"headers": Object {
"Content-type": "application/json",
},
"statusCode": 200,
},
"validations": Object {
"input": Array [],
"output": Array [],
},
}
`;

exports[`Http Prism Instance function tests given route with invalid param should return a validation error 1`] = `
Object {
"input": Object {
"method": "get",
"url": Object {
"path": "/pet/findByStatus",
},
},
"output": Object {
"body": "ERROR: Your request is not valid.
We cannot generate a sensible response because your '400'
response has neither example nor schema or is not defined.
Here is the original validation result instead: [{\\"path\\":[\\"query\\",\\"status\\"],\\"name\\":\\"required\\",\\"summary\\":\\"\\",\\"message\\":\\"Missing status query param\\",\\"severity\\":\\"error\\"}]",
"headers": Object {
"Content-type": "text/plain",
},
"statusCode": 400,
},
"validations": Object {
"input": Array [
Object {
"message": "Missing status query param",
"name": "required",
"path": Array [
"query",
"status",
],
"severity": "error",
"summary": "",
},
],
"output": Array [],
},
}
`;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"swagger": "2.0",
"info": {
"title": "Swagger Petstore"
},
"host": "petstore.swagger.io",
"basePath": "/v2",
"tags": [],
"schemes": ["https", "http"],
"paths": {
"/pet/findByStatus": {
"get": {
"tags": ["pet"],
"summary": "Finds Pets by status",
"operationId": "findPetsByStatus",
"produces": ["application/xml", "application/json"],
"parameters": [
{
"name": "status",
"in": "query",
"description": "Status values that need to be considered for filter",
"required": true,
"type": "array",
"items": {
"type": "string",
"enum": ["available", "pending", "sold"],
"default": "available"
},
"collectionFormat": "multi"
}
],
"responses": {
"200": {
"description": "successful operation",
"schema": {
"type": "array",
"items": {
"type": "object",
"required": ["name", "photoUrls"],
"properties": {
"name": {
"type": "string",
"example": "doggie"
},
"photoUrls": {
"type": "array",
"xml": {
"name": "photoUrl",
"wrapped": true
},
"items": {
"type": "string"
}
}
}
}
}
},
"400": {
"description": "Invalid status value"
}
},
"security": [
{
"petstore_auth": ["write:pets", "read:pets"]
}
]
}
}
},
"securityDefinitions": {
"petstore_auth": {
"type": "oauth2",
"authorizationUrl": "https://petstore.swagger.io/oauth/dialog",
"flow": "implicit",
"scopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
}
},
"api_key": {
"type": "apiKey",
"name": "api_key",
"in": "header"
}
}
}
75 changes: 0 additions & 75 deletions packages/http/src/__tests__/fixtures/no-refs-petstore.oas2.json
Original file line number Diff line number Diff line change
Expand Up @@ -367,81 +367,6 @@
}
],
"responses": {
"200": {
"description": "successful operation",
"schema": {
"type": "array",
"items": {
"type": "object",
"required": ["name", "photoUrls"],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"category": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
}
},
"xml": {
"name": "Category"
}
},
"name": {
"type": "string",
"example": "doggie"
},
"photoUrls": {
"type": "array",
"xml": {
"name": "photoUrl",
"wrapped": true
},
"items": {
"type": "string"
}
},
"tags": {
"type": "array",
"xml": {
"name": "tag",
"wrapped": true
},
"items": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
}
},
"xml": {
"name": "Tag"
}
}
},
"status": {
"type": "string",
"description": "pet status in the store",
"enum": ["available", "pending", "sold"]
}
},
"xml": {
"name": "Pet"
}
}
}
},
"400": {
"description": "Invalid tag value"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ describe('Http Prism Instance function tests', () => {

beforeAll(async () => {
prism = createInstance();
await prism.load({ path: resolve(__dirname, 'fixtures', 'no-refs-petstore.oas2.json') });
await prism.load({
path: resolve(__dirname, 'fixtures', 'no-refs-petstore-minimal.oas2.json'),
});
});

test('given incorrect route should throw error', () => {
Expand Down Expand Up @@ -43,4 +45,14 @@ describe('Http Prism Instance function tests', () => {
// because body is generated randomly
expect(omit(response, 'output.body')).toMatchSnapshot();
});

test('given route with invalid param should return a validation error', async () => {
const response = await prism.process({
method: 'get',
url: {
path: '/pet/findByStatus',
},
});
expect(response).toMatchSnapshot();
});
});
15 changes: 14 additions & 1 deletion packages/http/src/mocker/HttpMocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,20 @@ export class HttpMocker
// looking up proper example
let negotiationResult;
if (input.validations.input.length > 0) {
negotiationResult = helpers.negotiateOptionsForInvalidRequest(resource.responses);
try {
negotiationResult = helpers.negotiateOptionsForInvalidRequest(resource.responses);
} catch (error) {
return {
statusCode: 400,
headers: {
'Content-type': 'text/plain',
},
body: `ERROR: Your request is not valid.
We cannot generate a sensible response because your '400'
response has neither example nor schema or is not defined.
Here is the original validation result instead: ${JSON.stringify(input.validations.input)}`,
};
}
} else {
negotiationResult = helpers.negotiateOptionsForValidRequest(resource, mockConfig);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/http/src/mocker/negotiator/NegotiatorHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ const helpers = {
schema: responseWithSchema.schema,
};
} else {
throw new Error('Data corrupted');
throw new Error(
'Request invalid but mock data corrupted. Neither schema nor example defined for 400 response.'
);
}
},
};
Expand Down

0 comments on commit 73db545

Please sign in to comment.