diff --git a/lib/pact/consumer/mock_service/cors_origin_header_middleware.rb b/lib/pact/consumer/mock_service/cors_origin_header_middleware.rb index 1697dd8..7bd185d 100644 --- a/lib/pact/consumer/mock_service/cors_origin_header_middleware.rb +++ b/lib/pact/consumer/mock_service/cors_origin_header_middleware.rb @@ -23,7 +23,8 @@ def shutdown private def add_cors_header env, response - [response[0], response[1].merge('Access-Control-Allow-Origin' => env.fetch('HTTP_ORIGIN','*')), response[2]] + cors_headers = { 'Access-Control-Allow-Origin' => env.fetch('HTTP_ORIGIN','*'), 'Access-Control-Allow-Credentials' => 'true'} + [response[0], response[1].merge(cors_headers), response[2]] end end end diff --git a/spec/features/administration_endpoints_cors_spec.rb b/spec/features/administration_endpoints_cors_spec.rb index 1625ced..da23c4a 100644 --- a/spec/features/administration_endpoints_cors_spec.rb +++ b/spec/features/administration_endpoints_cors_spec.rb @@ -85,21 +85,25 @@ it "includes the CORS headers in the response to DELETE /interactions" do | example | delete "/interactions", nil, admin_headers expect(last_response.headers['Access-Control-Allow-Origin']).to eq '*' + expect(last_response.headers['Access-Control-Allow-Credentials']).to eq 'true' end it "includes the CORS headers in the response to POST /interactions" do | example | post "/interactions", expected_interaction, admin_headers expect(last_response.headers['Access-Control-Allow-Origin']).to eq '*' + expect(last_response.headers['Access-Control-Allow-Credentials']).to eq 'true' end it "includes the CORS headers in the response to POST /pact" do | example | post "/pact", pact_details, admin_headers expect(last_response.headers['Access-Control-Allow-Origin']).to eq '*' + expect(last_response.headers['Access-Control-Allow-Credentials']).to eq 'true' end it "includes the CORS headers in the response to GET /interactions/verification" do | example | get "/interactions/verification", nil, admin_headers expect(last_response.headers['Access-Control-Allow-Origin']).to eq '*' + expect(last_response.headers['Access-Control-Allow-Credentials']).to eq 'true' end context "when the Origin header is set" do diff --git a/spec/features/log/mock_multiple_responses_spec.log b/spec/features/log/mock_multiple_responses_spec.log index d11b5dc..a930e5c 100644 --- a/spec/features/log/mock_multiple_responses_spec.log +++ b/spec/features/log/mock_multiple_responses_spec.log @@ -1,4 +1,4 @@ -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" +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" INFO -- : Registered expected interaction GET /alligators DEBUG -- : { "description": "a request for alligators", @@ -20,15 +20,16 @@ DEBUG -- : { "name": "Mary" } ] - } + }, + "metadata": null } -INFO -- : Registered expected interaction GET /alligators +INFO -- : Registered expected interaction GET /zebras DEBUG -- : { - "description": "a request for alligators", - "providerState": "there are no alligators", + "description": "a request for zebras", + "providerState": "there are zebras", "request": { "method": "get", - "path": "/alligators", + "path": "/zebras", "headers": { "Accept": "application/json" } @@ -39,9 +40,12 @@ DEBUG -- : { "Content-Type": "application/json" }, "body": [ - + { + "name": "Xena Zebra" + } ] - } + }, + "metadata": null } INFO -- : Received request GET /alligators DEBUG -- : { @@ -56,7 +60,46 @@ DEBUG -- : { "Cookie": "" } } -ERROR -- : Multiple interactions found for GET /alligators: +INFO -- : Found matching response for GET /alligators +DEBUG -- : { + "status": 200, + "headers": { + "Content-Type": "application/json" + }, + "body": [ + { + "name": "Mary" + } + ] +} +INFO -- : Received request GET /zebras +DEBUG -- : { + "method": "get", + "query": "", + "path": "/zebras", + "headers": { + "Https": "off", + "Content-Length": "0", + "Accept": "application/json", + "Host": "example.org", + "Cookie": "" + } +} +INFO -- : Found matching response for GET /zebras +DEBUG -- : { + "status": 200, + "headers": { + "Content-Type": "application/json" + }, + "body": [ + { + "name": "Xena Zebra" + } + ] +} +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" +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" +INFO -- : Registered expected interaction GET /alligators DEBUG -- : { "description": "a request for alligators", "providerState": "alligators exist", @@ -77,8 +120,10 @@ DEBUG -- : { "name": "Mary" } ] - } + }, + "metadata": null } +INFO -- : Registered expected interaction GET /alligators DEBUG -- : { "description": "a request for alligators", "providerState": "there are no alligators", @@ -97,21 +142,23 @@ DEBUG -- : { "body": [ ] + }, + "metadata": null +} +INFO -- : Received request GET /alligators +DEBUG -- : { + "method": "get", + "query": "", + "path": "/alligators", + "headers": { + "Https": "off", + "Content-Length": "0", + "Accept": "application/json", + "Host": "example.org", + "Cookie": "" } } -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". -Missing requests: - GET /alligators - GET /alligators - - -WARN -- : Missing requests: - GET /alligators - GET /alligators - - -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" -INFO -- : Registered expected interaction GET /alligators +ERROR -- : Multiple interactions found for GET /alligators: DEBUG -- : { "description": "a request for alligators", "providerState": "alligators exist", @@ -132,15 +179,15 @@ DEBUG -- : { "name": "Mary" } ] - } + }, + "metadata": null } -INFO -- : Registered expected interaction GET /zebras DEBUG -- : { - "description": "a request for zebras", - "providerState": "there are zebras", + "description": "a request for alligators", + "providerState": "there are no alligators", "request": { "method": "get", - "path": "/zebras", + "path": "/alligators", "headers": { "Accept": "application/json" } @@ -151,60 +198,19 @@ DEBUG -- : { "Content-Type": "application/json" }, "body": [ - { - "name": "Xena Zebra" - } + ] - } -} -INFO -- : Received request GET /alligators -DEBUG -- : { - "method": "get", - "query": "", - "path": "/alligators", - "headers": { - "Https": "off", - "Content-Length": "0", - "Accept": "application/json", - "Host": "example.org", - "Cookie": "" - } -} -INFO -- : Found matching response for GET /alligators -DEBUG -- : { - "status": 200, - "headers": { - "Content-Type": "application/json" - }, - "body": [ - { - "name": "Mary" - } - ] -} -INFO -- : Received request GET /zebras -DEBUG -- : { - "method": "get", - "query": "", - "path": "/zebras", - "headers": { - "Https": "off", - "Content-Length": "0", - "Accept": "application/json", - "Host": "example.org", - "Cookie": "" - } -} -INFO -- : Found matching response for GET /zebras -DEBUG -- : { - "status": 200, - "headers": { - "Content-Type": "application/json" }, - "body": [ - { - "name": "Xena Zebra" - } - ] + "metadata": null } -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" +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". +Missing requests: + GET /alligators + GET /alligators + + +WARN -- : Missing requests: + GET /alligators + GET /alligators + + diff --git a/spec/features/log/mock_one_response_spec.log b/spec/features/log/mock_one_response_spec.log index 9b6f24c..31bcfa7 100644 --- a/spec/features/log/mock_one_response_spec.log +++ b/spec/features/log/mock_one_response_spec.log @@ -1,4 +1,4 @@ -INFO -- : Cleared interactions for example "Pact::Consumer::MockService when a response has been mocked when the actual request does not match the expected request returns an error response" +INFO -- : Cleared interactions for example "Pact::Consumer::MockService when a response has been mocked when the actual request matches the expected request returns the expected response" INFO -- : Registered expected interaction GET /alligators DEBUG -- : { "description": "a request for alligators", @@ -20,7 +20,8 @@ DEBUG -- : { "name": "Mary" } ] - } + }, + "metadata": null } INFO -- : Received request GET /alligators DEBUG -- : { @@ -30,41 +31,25 @@ DEBUG -- : { "headers": { "Https": "off", "Content-Length": "0", - "Accept": "application/xml", + "Accept": "application/json", "Host": "example.org", "Cookie": "" } } -ERROR -- : No matching interaction found for GET /alligators -ERROR -- : Interaction diffs for that route: -ERROR -- : Diff with interaction: "a request for alligators" given "alligators exist" -Diff --------------------------------------- -Key: - is expected - + is actual -Matching keys and values are not shown - - { - "headers": { -- "Accept": "application/json" -+ "Accept": "application/xml" - } - } - -Description of differences --------------------------------------- -* Expected "application/json" but got "application/xml" at $.headers.Accept - -WARN -- : Verifying - actual interactions do not match expected interactions for example "Pact::Consumer::MockService when a response has been mocked when the actual request does not match the expected request returns an error response". -Incorrect requests: - GET /alligators (request headers did not match) - - -WARN -- : Incorrect requests: - GET /alligators (request headers did not match) - - -INFO -- : Cleared interactions for example "Pact::Consumer::MockService when a response has been mocked when the actual request matches the expected request returns the expected response" +INFO -- : Found matching response for GET /alligators +DEBUG -- : { + "status": 200, + "headers": { + "Content-Type": "application/json" + }, + "body": [ + { + "name": "Mary" + } + ] +} +INFO -- : Verifying - interactions matched for example "Pact::Consumer::MockService when a response has been mocked when the actual request matches the expected request returns the expected response" +INFO -- : Cleared interactions for example "Pact::Consumer::MockService when a response has been mocked when the actual request does not match the expected request returns an error response" INFO -- : Registered expected interaction GET /alligators DEBUG -- : { "description": "a request for alligators", @@ -86,7 +71,8 @@ DEBUG -- : { "name": "Mary" } ] - } + }, + "metadata": null } INFO -- : Received request GET /alligators DEBUG -- : { @@ -96,21 +82,37 @@ DEBUG -- : { "headers": { "Https": "off", "Content-Length": "0", - "Accept": "application/json", + "Accept": "application/xml", "Host": "example.org", "Cookie": "" } } -INFO -- : Found matching response for GET /alligators -DEBUG -- : { - "status": 200, - "headers": { - "Content-Type": "application/json" - }, - "body": [ - { - "name": "Mary" - } - ] -} -INFO -- : Verifying - interactions matched for example "Pact::Consumer::MockService when a response has been mocked when the actual request matches the expected request returns the expected response" +ERROR -- : No matching interaction found for GET /alligators +ERROR -- : Interaction diffs for that route: +ERROR -- : Diff with interaction: "a request for alligators" given "alligators exist" +Diff +-------------------------------------- +Key: - is expected + + is actual +Matching keys and values are not shown + + { + "headers": { +- "Accept": "application/json" ++ "Accept": "application/xml" + } + } + +Description of differences +-------------------------------------- +* Expected "application/json" but got "application/xml" at $.headers.Accept + +WARN -- : Verifying - actual interactions do not match expected interactions for example "Pact::Consumer::MockService when a response has been mocked when the actual request does not match the expected request returns an error response". +Incorrect requests: + GET /alligators (request headers did not match) + + +WARN -- : Incorrect requests: + GET /alligators (request headers did not match) + + diff --git a/spec/features/mock_interactions_with_cors_spec.rb b/spec/features/mock_interactions_with_cors_spec.rb index 9dac90f..3bbdd81 100644 --- a/spec/features/mock_interactions_with_cors_spec.rb +++ b/spec/features/mock_interactions_with_cors_spec.rb @@ -61,6 +61,7 @@ # Ensure it allows the browser to actually make the request expect(last_response.status).to eq 200 expect(last_response.headers['Access-Control-Allow-Origin']).to eq 'http://localhost:1234' + expect(last_response.headers['Access-Control-Allow-Credentials']).to eq 'true' expect(last_response.headers['Access-Control-Allow-Headers']).to include 'accept' expect(last_response.headers['Access-Control-Allow-Methods']).to include "DELETE, POST, GET, HEAD, PUT, TRACE, CONNECT"