Skip to content

Commit 4305553

Browse files
authored
fix: request accept header should be checked against all response status codes (#38)
1 parent bf6f164 commit 4305553

File tree

4 files changed

+29
-27
lines changed

4 files changed

+29
-27
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"mockDetails": {
66
"interactionDescription": "should warn when header is not defined in spec",
77
"interactionState": "[none]",
8-
"location": "[root].interactions[3].request.headers.x-surprise",
8+
"location": "[root].interactions[4].request.headers.x-surprise",
99
"value": "unexpected header"
1010
},
1111
"specDetails": {
@@ -83,10 +83,9 @@
8383
"mockDetails": {
8484
"interactionDescription": "should error when required header is missing",
8585
"interactionState": "[none]",
86-
"location": "[root].interactions[4]",
86+
"location": "[root].interactions[5]",
8787
"value": {
8888
"description": "should error when required header is missing",
89-
"providerState": "",
9089
"request": {
9190
"method": "GET",
9291
"path": "/path",
@@ -121,7 +120,7 @@
121120
"mockDetails": {
122121
"interactionDescription": "should error when required header does not match the spec",
123122
"interactionState": "[none]",
124-
"location": "[root].interactions[5].request.headers.x-required-number",
123+
"location": "[root].interactions[6].request.headers.x-required-number",
125124
"value": "abc"
126125
},
127126
"specDetails": {
@@ -145,7 +144,7 @@
145144
"mockDetails": {
146145
"interactionDescription": "should error when optional header does not match the spec",
147146
"interactionState": "[none]",
148-
"location": "[root].interactions[6].request.headers.x-optional-number",
147+
"location": "[root].interactions[7].request.headers.x-optional-number",
149148
"value": "abc"
150149
},
151150
"specDetails": {
@@ -169,7 +168,7 @@
169168
"mockDetails": {
170169
"interactionDescription": "should warn when spec is missing accept and content-type",
171170
"interactionState": "[none]",
172-
"location": "[root].interactions[8].request.headers.accept",
171+
"location": "[root].interactions[9].request.headers.accept",
173172
"value": "application/json"
174173
},
175174
"specDetails": {
@@ -193,7 +192,7 @@
193192
"mockDetails": {
194193
"interactionDescription": "should warn when spec is missing accept and content-type",
195194
"interactionState": "[none]",
196-
"location": "[root].interactions[8].response.headers.Content-Type",
195+
"location": "[root].interactions[9].response.headers.Content-Type",
197196
"value": "application/json"
198197
},
199198
"specDetails": {
@@ -217,7 +216,7 @@
217216
"mockDetails": {
218217
"interactionDescription": "should error about incorrect request content type",
219218
"interactionState": "[none]",
220-
"location": "[root].interactions[9].request.headers.content-type",
219+
"location": "[root].interactions[10].request.headers.content-type",
221220
"value": "unknown"
222221
},
223222
"specDetails": {

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"interactions": [
55
{
66
"description": "should pass when header matches the spec",
7-
"providerState": "",
87
"request": {
98
"method": "GET",
109
"path": "/path",
@@ -24,7 +23,6 @@
2423
},
2524
{
2625
"description": "should pass for 4xx responses",
27-
"providerState": "",
2826
"request": {
2927
"method": "GET",
3028
"path": "/path",
@@ -39,9 +37,21 @@
3937
}
4038
}
4139
},
40+
{
41+
"description": "should pass when requested content-type is supported, even if not for current response status",
42+
"request": {
43+
"method": "GET",
44+
"path": "/path",
45+
"headers": {
46+
"accept": "application/json"
47+
}
48+
},
49+
"response": {
50+
"status": 400
51+
}
52+
},
4253
{
4354
"description": "should pass for standard headers not defined in spec",
44-
"providerState": "",
4555
"request": {
4656
"method": "GET",
4757
"path": "/path",
@@ -90,7 +100,6 @@
90100
},
91101
{
92102
"description": "should warn when header is not defined in spec",
93-
"providerState": "",
94103
"request": {
95104
"method": "GET",
96105
"path": "/path",
@@ -108,7 +117,6 @@
108117
},
109118
{
110119
"description": "should error when required header is missing",
111-
"providerState": "",
112120
"request": {
113121
"method": "GET",
114122
"path": "/path",
@@ -123,7 +131,6 @@
123131
},
124132
{
125133
"description": "should error when required header does not match the spec",
126-
"providerState": "",
127134
"request": {
128135
"method": "GET",
129136
"path": "/path",
@@ -140,7 +147,6 @@
140147
},
141148
{
142149
"description": "should error when optional header does not match the spec",
143-
"providerState": "",
144150
"request": {
145151
"method": "GET",
146152
"path": "/path",
@@ -158,7 +164,6 @@
158164
},
159165
{
160166
"description": "should error when header does not match the array spec",
161-
"providerState": "",
162167
"request": {
163168
"method": "GET",
164169
"path": "/path",
@@ -176,7 +181,6 @@
176181
},
177182
{
178183
"description": "should warn when spec is missing accept and content-type",
179-
"providerState": "",
180184
"request": {
181185
"method": "GET",
182186
"path": "/no-response",
@@ -193,7 +197,6 @@
193197
},
194198
{
195199
"description": "should error about incorrect request content type",
196-
"providerState": "",
197200
"request": {
198201
"method": "POST",
199202
"path": "/submit",

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"mockDetails": {
66
"interactionDescription": "should warn when header is not defined in spec",
77
"interactionState": "[none]",
8-
"location": "[root].interactions[3].request.headers.x-surprise",
8+
"location": "[root].interactions[4].request.headers.x-surprise",
99
"value": "unexpected header"
1010
},
1111
"specDetails": {
@@ -83,7 +83,7 @@
8383
"mockDetails": {
8484
"interactionDescription": "should error when required header is missing",
8585
"interactionState": "[none]",
86-
"location": "[root].interactions[4].request.headers.x-required-number",
86+
"location": "[root].interactions[5].request.headers.x-required-number",
8787
"value": null
8888
},
8989
"specDetails": {
@@ -107,7 +107,7 @@
107107
"mockDetails": {
108108
"interactionDescription": "should error when required header does not match the spec",
109109
"interactionState": "[none]",
110-
"location": "[root].interactions[5].request.headers.x-required-number",
110+
"location": "[root].interactions[6].request.headers.x-required-number",
111111
"value": "abc"
112112
},
113113
"specDetails": {
@@ -124,7 +124,7 @@
124124
"mockDetails": {
125125
"interactionDescription": "should error when optional header does not match the spec",
126126
"interactionState": "[none]",
127-
"location": "[root].interactions[6].request.headers.x-optional-number",
127+
"location": "[root].interactions[7].request.headers.x-optional-number",
128128
"value": "abc"
129129
},
130130
"specDetails": {
@@ -141,7 +141,7 @@
141141
"mockDetails": {
142142
"interactionDescription": "should error when header does not match the array spec",
143143
"interactionState": "[none]",
144-
"location": "[root].interactions[7].request.headers.x-optional-array.2",
144+
"location": "[root].interactions[8].request.headers.x-optional-array.2",
145145
"value": "abc"
146146
},
147147
"specDetails": {
@@ -158,7 +158,7 @@
158158
"mockDetails": {
159159
"interactionDescription": "should warn when spec is missing accept and content-type",
160160
"interactionState": "[none]",
161-
"location": "[root].interactions[8].request.headers.accept",
161+
"location": "[root].interactions[9].request.headers.accept",
162162
"value": "application/json"
163163
},
164164
"specDetails": {
@@ -182,7 +182,7 @@
182182
"mockDetails": {
183183
"interactionDescription": "should warn when spec is missing accept and content-type",
184184
"interactionState": "[none]",
185-
"location": "[root].interactions[8].response.headers.content-type",
185+
"location": "[root].interactions[9].response.headers.content-type",
186186
"value": "application/json"
187187
},
188188
"specDetails": {
@@ -206,7 +206,7 @@
206206
"mockDetails": {
207207
"interactionDescription": "should error about incorrect request content type",
208208
"interactionState": "[none]",
209-
"location": "[root].interactions[9].request.headers.content-type",
209+
"location": "[root].interactions[10].request.headers.content-type",
210210
"value": "unknown"
211211
},
212212
"specDetails": {

src/compare/requestHeader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export function* compareReqHeader(
8383
if (
8484
requestAccept &&
8585
availableResponseContentType.length &&
86-
!findMatchingType(requestAccept, availableResponseContentType)
86+
!findMatchingType(requestAccept, allAvailableResponseContentTypes)
8787
) {
8888
yield {
8989
code: "request.accept.incompatible",

0 commit comments

Comments
 (0)