Pact mock server not returning proper CORS headers
alagesann opened this issue · 7 comments
My Pact mock server is listening in different port from my tests and my pact mock server is running with following configuration:
const provider = pact({
consumer: 'myconsumer',
provider: 'myprovider',
port: 8989,
cors: true,
log: path.resolve(process.cwd(), 'logs', 'pact.log'),
dir: path.resolve(process.cwd(), 'pacts'),
logLevel: 'DEBUG',
spec: 1
});
I request the above mock server using 'axios' and the request is matched with one of the interaction i have configured on the provider (as per pact.log). But response always throws error with CORS.
My pact log has following information:
Received OPTIONS request for mock service administration endpoint POST /interactions. Returning CORS headers: {"Access-Control-Allow-Origin":"null","Access-Control-Allow-Headers":"x-pact-mock-service","Access-Control-Allow-Methods":"DELETE, POST, GET, HEAD, PUT, TRACE, CONNECT, PATCH"}.
why is Access-Control-Allow-Origin is null instead of '*'. ?
How can i disable CORS security completely with tests as the final resorts when i use webpack, npm and mocha based tests.?
Your Http-Origin header appears to be empty:
Here's some documentation on using CORS with the mock service. https://github.com/pact-foundation/pact-mock_service/wiki/Using-the-mock-service-with-CORS
Why is http-origin header empty when pact mock service is created with cors enabled ( Please note pact mock service creation config above with cors: true). cors_origin_header_middleware adds the 'Access-Control-Allow-Origin with value of '*' when i create pact mock service with cors enabled. but thats not seems to work for me as i have cors enabled and Access-Control-Allow-Origin is still null.
def add_cors_header env, response
[response[0], response[1].merge('Access-Control-Allow-Origin' => env.fetch('HTTP_ORIGIN','*')), response[2]]
end
env.fetch will return the second argument if the given key is not found. As env.fetch('HTTP_ORIGIN','*')
is returning nil, it suggests that you have an Http-Origin header set with an empty value.
Thanks for clarifying it.
i am using mocha to fire my tests and for a react-action in testing, it logs the following request in pact.log:
I, [2017-07-17T11:34:55.421448 #15431] INFO -- : Received request GET /mydata?address=75001
D, [2017-07-17T11:34:55.421542 #15431] DEBUG -- : {
"path": "/mydata",
"query": "address=75001",
"method": "get",
"headers": {
"Accept": "application/json",
"Referer": "about:blank",
"User-Agent": "Node.js (darwin; U; rv:v7.10.0) AppleWebKit/537.36 (KHTML, like Gecko)",
"Accept-Language": "en",
"Origin": "null",
"Host": "localhost:8989",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
"Version": "HTTP/1.1"
}
}
Here "Origin": "null" is populated automatically and even though request matches with the interaction and generates the pact file but the response is not returned from mock server. How do i resolve the issue now?
My local nodejs was automatically populating the request header with origin null. Pact started working fine when i fixed it. Thanks for your time. @bethesque
No worries, glad you've fixed it.