Skip to content

Commit f37ae0a

Browse files
authored
fix: incorrect response content-type behaviour (#22)
* fix: incorrect response content-type behaviour * chore: remove redundant parts of oas
1 parent 96f6885 commit f37ae0a

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed

src/__tests__/fixtures/request-parameters-header/baseline.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,5 +210,24 @@
210210
}
211211
},
212212
"type": "warning"
213+
},
214+
{
215+
"code": "request.content-type.incompatible",
216+
"message": "Request Content-Type header is incompatible with the mime-types the spec accepts to consume",
217+
"mockDetails": {
218+
"interactionDescription": "should error about incorrect request content type",
219+
"interactionState": "[none]",
220+
"location": "[root].interactions[9].request.headers.content-type",
221+
"value": "unknown"
222+
},
223+
"specDetails": {
224+
"location": "[root].paths./submit.post.requestBody.content",
225+
"pathMethod": "post",
226+
"pathName": "/submit",
227+
"value": [
228+
"application/json"
229+
]
230+
},
231+
"type": "error"
213232
}
214233
]

src/__tests__/fixtures/request-parameters-header/oas.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ paths:
4343
description: Bad request
4444
content:
4545
application/json: {}
46+
/submit:
47+
post:
48+
requestBody:
49+
required: true
50+
content:
51+
application/json:
52+
schema: {}
53+
responses:
54+
"400":
55+
description: Bad request
56+
content:
57+
"application/json":
58+
schema: {}
4659
/no-response:
4760
get:
4861
responses:

src/__tests__/fixtures/request-parameters-header/pact.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,24 @@
190190
"Content-Type": "application/json"
191191
}
192192
}
193+
},
194+
{
195+
"description": "should error about incorrect request content type",
196+
"providerState": "",
197+
"request": {
198+
"method": "POST",
199+
"path": "/submit",
200+
"headers": {
201+
"content-type": "unknown"
202+
},
203+
"body": "unknown"
204+
},
205+
"response": {
206+
"status": 400,
207+
"headers": {
208+
"Content-Type": "application/json"
209+
}
210+
}
193211
}
194212
]
195213
}

src/__tests__/fixtures/request-parameters-header/results.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,5 +199,24 @@
199199
}
200200
},
201201
"type": "warning"
202+
},
203+
{
204+
"code": "request.content-type.incompatible",
205+
"message": "Request Content-Type header is incompatible with the mime-types the spec accepts to consume",
206+
"mockDetails": {
207+
"interactionDescription": "should error about incorrect request content type",
208+
"interactionState": "[none]",
209+
"location": "[root].interactions[9].request.headers.content-type",
210+
"value": "unknown"
211+
},
212+
"specDetails": {
213+
"location": "[root].paths./submit.post.requestBody.content",
214+
"pathMethod": "post",
215+
"pathName": "/submit",
216+
"value": [
217+
"application/json"
218+
]
219+
},
220+
"type": "error"
202221
}
203222
]

src/compare/responseHeader.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ export function* compareResHeader(
2424
): Iterable<Result> {
2525
const { method, oas, operation, path } = route.store;
2626

27+
const allAvailableResponseContentTypes =
28+
operation.produces ||
29+
Object.entries(operation.responses).reduce(
30+
(acc: string[], [_status, response]) => {
31+
return [
32+
...acc,
33+
...Object.keys(dereferenceOas(response || {}, oas)?.content || {}),
34+
];
35+
},
36+
[],
37+
);
2738
const availableResponseContentType =
2839
operation.produces ||
2940
Object.keys(
@@ -41,7 +52,7 @@ export function* compareResHeader(
4152
const responseContentType =
4253
responseHeaders.get("content-type")?.split(";")[0] || "";
4354

44-
if (responseContentType && !availableResponseContentType.length) {
55+
if (responseContentType && !allAvailableResponseContentTypes.length) {
4556
yield {
4657
code: "response.content-type.unknown",
4758
message:

0 commit comments

Comments
 (0)