Skip to content

How to handle CORS issues with hard-coded domain #97

@soundstep

Description

@soundstep

Pasting from an original discussion with @bethesque

I got an issue, not caused by pact itself but more as a conceptual question caused by the way the pact mock service is used.

This page does not cover everything:
https://github.com/pact-foundation/pact-mock_service/wiki/Using-the-mock-service-with-CORS

In case you make a request from a browser, the Origin header will be sent, the pact mock service can just return Access-Control-Allow-Origin: * for it to work.

However, if you make request with withCredentials, current browsers will not take a wildcard for answer (https://stackoverflow.com/questions/42803394/cors-credentials-mode-is-include).

That's why most backend framework instead takes the Origin value and send it back as Access-Control-Allow-Origin, for example for it could look like:

withRequest: {
  headers: {
    'Origin': 'http://mydomain.com',
willRespondWith: {
  headers: {
    'Access-Control-Allow-Origin': 'http://mydomain.com',

Unfortunately, doing this will prevent you making request against the pact mock service from let's say, a localhost.

The solution could be to add a matcher and with a wildcard:

willRespondWith: {
    headers: {
        'Access-Control-Allow-Origin': Pact.Matchers.somethingLike('*'),

This will make you able to request from a localhost domain, to the pact mock service, but the only thing you want to make sure is that 'Access-Control-Allow-Origin contains http://mydomain.com so that your app can call your service, and this doesn't do the job.

Question:

How can we validate having a proper url in an Access-Control-Allow-Origin header as we can't make request against the pact-mock-service.

Update:

An executable demo can be found there:
https://github.com/soundstep/pact-ruby-standalone-e2e-example/blob/master/DEMO.md

More in-code explanation there:
https://github.com/soundstep/pact-ruby-standalone-e2e-example/blob/master/demo/tests/cors-error/test.js#L26

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions