Skip to content

Commit 61bd9d1

Browse files
authored
feat: add 'Access-Control-Allow-Headers' = true to cors response headers (pact-foundation#121)
1 parent 96e2604 commit 61bd9d1

5 files changed

+145
-131
lines changed

lib/pact/consumer/mock_service/cors_origin_header_middleware.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ def shutdown
2323
private
2424

2525
def add_cors_header env, response
26-
[response[0], response[1].merge('Access-Control-Allow-Origin' => env.fetch('HTTP_ORIGIN','*')), response[2]]
26+
cors_headers = { 'Access-Control-Allow-Origin' => env.fetch('HTTP_ORIGIN','*'), 'Access-Control-Allow-Credentials' => 'true'}
27+
[response[0], response[1].merge(cors_headers), response[2]]
2728
end
2829
end
2930
end

spec/features/administration_endpoints_cors_spec.rb

+4
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,25 @@
8585
it "includes the CORS headers in the response to DELETE /interactions" do | example |
8686
delete "/interactions", nil, admin_headers
8787
expect(last_response.headers['Access-Control-Allow-Origin']).to eq '*'
88+
expect(last_response.headers['Access-Control-Allow-Credentials']).to eq 'true'
8889
end
8990

9091
it "includes the CORS headers in the response to POST /interactions" do | example |
9192
post "/interactions", expected_interaction, admin_headers
9293
expect(last_response.headers['Access-Control-Allow-Origin']).to eq '*'
94+
expect(last_response.headers['Access-Control-Allow-Credentials']).to eq 'true'
9395
end
9496

9597
it "includes the CORS headers in the response to POST /pact" do | example |
9698
post "/pact", pact_details, admin_headers
9799
expect(last_response.headers['Access-Control-Allow-Origin']).to eq '*'
100+
expect(last_response.headers['Access-Control-Allow-Credentials']).to eq 'true'
98101
end
99102

100103
it "includes the CORS headers in the response to GET /interactions/verification" do | example |
101104
get "/interactions/verification", nil, admin_headers
102105
expect(last_response.headers['Access-Control-Allow-Origin']).to eq '*'
106+
expect(last_response.headers['Access-Control-Allow-Credentials']).to eq 'true'
103107
end
104108

105109
context "when the Origin header is set" do
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
INFO -- : Cleared interactions for example "Pact::Consumer::MockService when more than one response has been mocked when the actual request matches more than one expected request returns an error response"
1+
INFO -- : Cleared interactions for example "Pact::Consumer::MockService when more than one response has been mocked when the actual request matches one expected request returns the expected response"
22
INFO -- : Registered expected interaction GET /alligators
33
DEBUG -- : {
44
"description": "a request for alligators",
@@ -20,15 +20,16 @@ DEBUG -- : {
2020
"name": "Mary"
2121
}
2222
]
23-
}
23+
},
24+
"metadata": null
2425
}
25-
INFO -- : Registered expected interaction GET /alligators
26+
INFO -- : Registered expected interaction GET /zebras
2627
DEBUG -- : {
27-
"description": "a request for alligators",
28-
"providerState": "there are no alligators",
28+
"description": "a request for zebras",
29+
"providerState": "there are zebras",
2930
"request": {
3031
"method": "get",
31-
"path": "/alligators",
32+
"path": "/zebras",
3233
"headers": {
3334
"Accept": "application/json"
3435
}
@@ -39,9 +40,12 @@ DEBUG -- : {
3940
"Content-Type": "application/json"
4041
},
4142
"body": [
42-
43+
{
44+
"name": "Xena Zebra"
45+
}
4346
]
44-
}
47+
},
48+
"metadata": null
4549
}
4650
INFO -- : Received request GET /alligators
4751
DEBUG -- : {
@@ -56,7 +60,46 @@ DEBUG -- : {
5660
"Cookie": ""
5761
}
5862
}
59-
ERROR -- : Multiple interactions found for GET /alligators:
63+
INFO -- : Found matching response for GET /alligators
64+
DEBUG -- : {
65+
"status": 200,
66+
"headers": {
67+
"Content-Type": "application/json"
68+
},
69+
"body": [
70+
{
71+
"name": "Mary"
72+
}
73+
]
74+
}
75+
INFO -- : Received request GET /zebras
76+
DEBUG -- : {
77+
"method": "get",
78+
"query": "",
79+
"path": "/zebras",
80+
"headers": {
81+
"Https": "off",
82+
"Content-Length": "0",
83+
"Accept": "application/json",
84+
"Host": "example.org",
85+
"Cookie": ""
86+
}
87+
}
88+
INFO -- : Found matching response for GET /zebras
89+
DEBUG -- : {
90+
"status": 200,
91+
"headers": {
92+
"Content-Type": "application/json"
93+
},
94+
"body": [
95+
{
96+
"name": "Xena Zebra"
97+
}
98+
]
99+
}
100+
INFO -- : Verifying - interactions matched for example "Pact::Consumer::MockService when more than one response has been mocked when the actual request matches one expected request returns the expected response"
101+
INFO -- : Cleared interactions for example "Pact::Consumer::MockService when more than one response has been mocked when the actual request matches more than one expected request returns an error response"
102+
INFO -- : Registered expected interaction GET /alligators
60103
DEBUG -- : {
61104
"description": "a request for alligators",
62105
"providerState": "alligators exist",
@@ -77,8 +120,10 @@ DEBUG -- : {
77120
"name": "Mary"
78121
}
79122
]
80-
}
123+
},
124+
"metadata": null
81125
}
126+
INFO -- : Registered expected interaction GET /alligators
82127
DEBUG -- : {
83128
"description": "a request for alligators",
84129
"providerState": "there are no alligators",
@@ -97,21 +142,23 @@ DEBUG -- : {
97142
"body": [
98143

99144
]
145+
},
146+
"metadata": null
147+
}
148+
INFO -- : Received request GET /alligators
149+
DEBUG -- : {
150+
"method": "get",
151+
"query": "",
152+
"path": "/alligators",
153+
"headers": {
154+
"Https": "off",
155+
"Content-Length": "0",
156+
"Accept": "application/json",
157+
"Host": "example.org",
158+
"Cookie": ""
100159
}
101160
}
102-
WARN -- : Verifying - actual interactions do not match expected interactions for example "Pact::Consumer::MockService when more than one response has been mocked when the actual request matches more than one expected request returns an error response".
103-
Missing requests:
104-
GET /alligators
105-
GET /alligators
106-
107-
108-
WARN -- : Missing requests:
109-
GET /alligators
110-
GET /alligators
111-
112-
113-
INFO -- : Cleared interactions for example "Pact::Consumer::MockService when more than one response has been mocked when the actual request matches one expected request returns the expected response"
114-
INFO -- : Registered expected interaction GET /alligators
161+
ERROR -- : Multiple interactions found for GET /alligators:
115162
DEBUG -- : {
116163
"description": "a request for alligators",
117164
"providerState": "alligators exist",
@@ -132,15 +179,15 @@ DEBUG -- : {
132179
"name": "Mary"
133180
}
134181
]
135-
}
182+
},
183+
"metadata": null
136184
}
137-
INFO -- : Registered expected interaction GET /zebras
138185
DEBUG -- : {
139-
"description": "a request for zebras",
140-
"providerState": "there are zebras",
186+
"description": "a request for alligators",
187+
"providerState": "there are no alligators",
141188
"request": {
142189
"method": "get",
143-
"path": "/zebras",
190+
"path": "/alligators",
144191
"headers": {
145192
"Accept": "application/json"
146193
}
@@ -151,60 +198,19 @@ DEBUG -- : {
151198
"Content-Type": "application/json"
152199
},
153200
"body": [
154-
{
155-
"name": "Xena Zebra"
156-
}
201+
157202
]
158-
}
159-
}
160-
INFO -- : Received request GET /alligators
161-
DEBUG -- : {
162-
"method": "get",
163-
"query": "",
164-
"path": "/alligators",
165-
"headers": {
166-
"Https": "off",
167-
"Content-Length": "0",
168-
"Accept": "application/json",
169-
"Host": "example.org",
170-
"Cookie": ""
171-
}
172-
}
173-
INFO -- : Found matching response for GET /alligators
174-
DEBUG -- : {
175-
"status": 200,
176-
"headers": {
177-
"Content-Type": "application/json"
178-
},
179-
"body": [
180-
{
181-
"name": "Mary"
182-
}
183-
]
184-
}
185-
INFO -- : Received request GET /zebras
186-
DEBUG -- : {
187-
"method": "get",
188-
"query": "",
189-
"path": "/zebras",
190-
"headers": {
191-
"Https": "off",
192-
"Content-Length": "0",
193-
"Accept": "application/json",
194-
"Host": "example.org",
195-
"Cookie": ""
196-
}
197-
}
198-
INFO -- : Found matching response for GET /zebras
199-
DEBUG -- : {
200-
"status": 200,
201-
"headers": {
202-
"Content-Type": "application/json"
203203
},
204-
"body": [
205-
{
206-
"name": "Xena Zebra"
207-
}
208-
]
204+
"metadata": null
209205
}
210-
INFO -- : Verifying - interactions matched for example "Pact::Consumer::MockService when more than one response has been mocked when the actual request matches one expected request returns the expected response"
206+
WARN -- : Verifying - actual interactions do not match expected interactions for example "Pact::Consumer::MockService when more than one response has been mocked when the actual request matches more than one expected request returns an error response".
207+
Missing requests:
208+
GET /alligators
209+
GET /alligators
210+
211+
212+
WARN -- : Missing requests:
213+
GET /alligators
214+
GET /alligators
215+
216+

0 commit comments

Comments
 (0)