Skip to content

Commit 7d4457d

Browse files
authored
Merge pull request #11 from pactflow/PACT-3690-fix-raise-request-content-type-error-only-when-request-body-is-not-undefined
fix: raise request content-type error only when req body is undefiend
2 parents 49a8184 + cc1f7d7 commit 7d4457d

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

src/__tests__/fixtures/request-body/baseline.json

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
[
2+
{
3+
"code": "request.body.incompatible",
4+
"message": "Request body is incompatible with the request body schema in the spec file: must be object",
5+
"mockDetails": {
6+
"interactionDescription": "should pass when json body matches spec",
7+
"interactionState": "[none]",
8+
"location": "[root].interactions[0].request.body"
9+
},
10+
"specDetails": {
11+
"location": "[root].paths./login.post.requestBody.content.application/json.schema.type",
12+
"pathMethod": "post",
13+
"pathName": "/login",
14+
"value": "object"
15+
},
16+
"type": "error"
17+
},
218
{
319
"code": "request.body.incompatible",
420
"message": "Request body is incompatible with the request body schema in the spec file: must be object",
521
"mockDetails": {
622
"interactionDescription": "should error on empty body",
723
"interactionState": "[none]",
8-
"location": "[root].interactions[4].request.body",
24+
"location": "[root].interactions[5].request.body",
925
"value": ""
1026
},
1127
"specDetails": {
@@ -22,7 +38,7 @@
2238
"mockDetails": {
2339
"interactionDescription": "should error on schema mismatch for json body",
2440
"interactionState": "[none]",
25-
"location": "[root].interactions[5].request.body",
41+
"location": "[root].interactions[6].request.body",
2642
"value": {
2743
"email": "[email protected]"
2844
}
@@ -44,7 +60,7 @@
4460
"mockDetails": {
4561
"interactionDescription": "should error on schema mismatch for form body",
4662
"interactionState": "[none]",
47-
"location": "[root].interactions[6].request.body",
63+
"location": "[root].interactions[7].request.body",
4864
"value": "[email protected]"
4965
},
5066
"specDetails": {

src/__tests__/fixtures/request-body/pact.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22
"consumer": { "name": "consumer" },
33
"provider": { "name": "provider" },
44
"interactions": [
5+
{
6+
"description": "should pass when json body matches spec",
7+
"providerState": "",
8+
"request": {
9+
"method": "POST",
10+
"path": "/login"
11+
},
12+
"response": {
13+
"status": 201,
14+
"body": {}
15+
}
16+
},
517
{
618
"description": "should pass when json body matches spec",
719
"providerState": "",

src/__tests__/fixtures/request-body/results.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"mockDetails": {
66
"interactionDescription": "should error on empty body",
77
"interactionState": "[none]",
8-
"location": "[root].interactions[4].request.body.",
8+
"location": "[root].interactions[5].request.body.",
99
"value": ""
1010
},
1111
"specDetails": {
@@ -22,7 +22,7 @@
2222
"mockDetails": {
2323
"interactionDescription": "should error on schema mismatch for json body",
2424
"interactionState": "[none]",
25-
"location": "[root].interactions[5].request.body.",
25+
"location": "[root].interactions[6].request.body.",
2626
"value": {
2727
"email": "[email protected]"
2828
}
@@ -44,7 +44,7 @@
4444
"mockDetails": {
4545
"interactionDescription": "should error on schema mismatch for form body",
4646
"interactionState": "[none]",
47-
"location": "[root].interactions[6].request.body.",
47+
"location": "[root].interactions[7].request.body.",
4848
"value": {
4949
"email": "[email protected]"
5050
}

src/compare/requestHeader.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function* compareReqHeader(
2626
index: number,
2727
): Iterable<Result> {
2828
const { method, oas, operation, path, securitySchemes } = route.store;
29-
29+
const { body } = interaction.request;
3030
const availableRequestContentType =
3131
operation.consumes ||
3232
Object.keys(
@@ -104,6 +104,9 @@ export function* compareReqHeader(
104104
};
105105
}
106106

107+
// req body
108+
const hasBody = !!(body || body === "");
109+
107110
// request content-type
108111
// --------------------
109112
const requestContentType: string =
@@ -157,7 +160,7 @@ export function* compareReqHeader(
157160
};
158161
}
159162

160-
if (!requestContentType && availableRequestContentType.length) {
163+
if (!requestContentType && availableRequestContentType.length && hasBody) {
161164
yield {
162165
code: "request.content-type.missing",
163166
message:

0 commit comments

Comments
 (0)