Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pact mock server not returning proper CORS headers #72

Closed
alagesann opened this issue Jul 16, 2017 · 7 comments
Closed

Pact mock server not returning proper CORS headers #72

alagesann opened this issue Jul 16, 2017 · 7 comments

Comments

@alagesann
Copy link

alagesann commented Jul 16, 2017

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.?

@bethesque
Copy link
Member

Your Http-Origin header appears to be empty:

[response[0], response[1].merge('Access-Control-Allow-Origin' => env.fetch('HTTP_ORIGIN','*')), response[2]]

@bethesque
Copy link
Member

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

@alagesann
Copy link
Author

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.

@bethesque
Copy link
Member

bethesque commented Jul 17, 2017

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.

@alagesann
Copy link
Author

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?

@alagesann
Copy link
Author

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

@bethesque
Copy link
Member

No worries, glad you've fixed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants