diff --git a/src/__tests__/fixtures/response-body/baseline.json b/src/__tests__/fixtures/response-body/baseline.json index 1fe8d7f..8158902 100644 --- a/src/__tests__/fixtures/response-body/baseline.json +++ b/src/__tests__/fixtures/response-body/baseline.json @@ -5,7 +5,7 @@ "mockDetails": { "interactionDescription": "should error on unknown pet", "interactionState": "[none]", - "location": "[root].interactions[4].response.body", + "location": "[root].interactions[5].response.body", "value": { "petType": "Spider" } @@ -26,7 +26,7 @@ "mockDetails": { "interactionDescription": "should error with invalid response", "interactionState": "[none]", - "location": "[root].interactions[5].response.body.detail", + "location": "[root].interactions[6].response.body.detail", "value": null }, "specDetails": { @@ -42,7 +42,7 @@ "mockDetails": { "interactionDescription": "should error with unknown response body", "interactionState": "[none]", - "location": "[root].interactions[6].response.body", + "location": "[root].interactions[7].response.body", "value": { "unknown": "schema" } @@ -64,7 +64,7 @@ "mockDetails": { "interactionDescription": "should error with unknown response body", "interactionState": "[none]", - "location": "[root].interactions[6].response.headers.Content-Type", + "location": "[root].interactions[7].response.headers.Content-Type", "value": "application/json" }, "specDetails": { diff --git a/src/__tests__/fixtures/response-body/oas.yaml b/src/__tests__/fixtures/response-body/oas.yaml index f56395b..5c136c6 100644 --- a/src/__tests__/fixtures/response-body/oas.yaml +++ b/src/__tests__/fixtures/response-body/oas.yaml @@ -43,6 +43,15 @@ paths: "200": description: OK content: {} + /throws-error: + get: + responses: + "500": + description: OK + content: + "text/html": + schema: + type: string components: schemas: Animal: diff --git a/src/__tests__/fixtures/response-body/pact.json b/src/__tests__/fixtures/response-body/pact.json index 4de5983..8fd51e1 100644 --- a/src/__tests__/fixtures/response-body/pact.json +++ b/src/__tests__/fixtures/response-body/pact.json @@ -91,6 +91,17 @@ } } }, + { + "description": "should pass when unable to validate content-type", + "request": { + "method": "GET", + "path": "/throws-error" + }, + "response": { + "status": 500, + "body": "boom" + } + }, { "description": "should error on unknown pet", "request": { diff --git a/src/__tests__/fixtures/response-body/results.json b/src/__tests__/fixtures/response-body/results.json index 33eae19..b93fe69 100644 --- a/src/__tests__/fixtures/response-body/results.json +++ b/src/__tests__/fixtures/response-body/results.json @@ -5,7 +5,7 @@ "mockDetails": { "interactionDescription": "should error on unknown pet", "interactionState": "[none]", - "location": "[root].interactions[4].response.body", + "location": "[root].interactions[5].response.body", "value": { "petType": "Spider" } @@ -26,7 +26,7 @@ "mockDetails": { "interactionDescription": "should error with invalid response", "interactionState": "[none]", - "location": "[root].interactions[5].response.body.detail", + "location": "[root].interactions[6].response.body.detail", "value": null }, "specDetails": { @@ -42,7 +42,7 @@ "mockDetails": { "interactionDescription": "should error with unknown response body", "interactionState": "[none]", - "location": "[root].interactions[6].response.headers.content-type", + "location": "[root].interactions[7].response.headers.content-type", "value": "application/json" }, "specDetails": { @@ -66,7 +66,7 @@ "mockDetails": { "interactionDescription": "should error with unknown response body", "interactionState": "[none]", - "location": "[root].interactions[6].response.body", + "location": "[root].interactions[7].response.body", "value": { "unknown": "schema" } diff --git a/src/compare/responseBody.ts b/src/compare/responseBody.ts index 4368251..b1d7428 100644 --- a/src/compare/responseBody.ts +++ b/src/compare/responseBody.ts @@ -17,6 +17,10 @@ import { dereferenceOas, splitPath } from "../utils/schema"; import { getValidateFunction } from "../utils/validation"; import { findMatchingType } from "./utils/content"; +const canValidate = (contentType = ""): boolean => { + return !!findMatchingType(contentType, ["application/json"]); +}; + const DEFAULT_CONTENT_TYPE = "application/json"; export function* compareResBody( @@ -64,7 +68,9 @@ export function* compareResBody( findMatchingType( requestHeaders.get("accept") || DEFAULT_CONTENT_TYPE, availableResponseContentTypes, - ) || DEFAULT_CONTENT_TYPE; + ) || + availableResponseContentTypes[0] || + DEFAULT_CONTENT_TYPE; const dereferencedResponse = dereferenceOas(response, oas); const schema: SchemaObject | undefined = (dereferencedResponse as OpenAPIV2.ResponseObject)?.schema || @@ -90,7 +96,7 @@ export function* compareResBody( }; } - if (value && !schema) { + if (value && canValidate(contentType) && !schema) { yield { code: "response.body.unknown", message: "No matching schema found for response body", @@ -109,7 +115,7 @@ export function* compareResBody( }; } - if (value && schema) { + if (value && canValidate(contentType) && schema) { const schemaId = `[root].paths.${path}.${method}.responses.${status}.content.${contentType}`; const validate = getValidateFunction(ajv, schemaId, () => transformResponseSchema(minimumSchema(schema, oas)),