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

Error: Cross origin http://localhost forbidden #1754

Closed
serverlesspolska opened this issue Aug 25, 2018 · 11 comments
Closed

Error: Cross origin http://localhost forbidden #1754

serverlesspolska opened this issue Aug 25, 2018 · 11 comments

Comments

@serverlesspolska
Copy link

Hi All,

I've got lambda function which I access via API Gateway.
This function works ok when I test it via Postman or via this test which uses request library:

it('shoudl work via request', async () => {
        
        var options = { method: 'POST',
        url: 'https://-----------.execute-api.eu-central-1.amazonaws.com/dev/generate',
        headers: 
         { 
           'Cache-Control': 'no-cache',
           'Content-Type': 'application/json' },
        body: 
         { html: 'PGgxPkhlbGxvIHt7bmFtZX19PC9oMT48aDI+SGVsbG8gd29ybGQhISE8L2gyPg==',
           variables: 'eyJuYW1lIjoiV29ybGQiLCJwYXRoIjoiL2hvbWUvcGF3ZWwvLm52bS92ZXJzaW9ucy9ub2RlL3Y4LjExLjMvYmluOi91c3IvbG9jYWwvc2JpbjovdXNyL2xvY2FsL2JpbjovdXNyL3NiaW46L3Vzci9iaW46L3NiaW46L2JpbjovdXNyL2dhbWVzOi91c3IvbG9jYWwvZ2FtZXM6L3NuYXAvYmluOi91c3IvbGliL2p2bS9qYXZhLTgtb3JhY2xlL2JpbjovdXNyL2xpYi9qdm0vamF2YS04LW9yYWNsZS9kYi9iaW46L3Vzci9saWIvanZtL2phdmEtOC1vcmFjbGUvanJlL2JpbjovdmFyL3Rhc2sifQ==',
           css: 'aDEge2NvbG9yOiByZWR9' },
        json: true };
      
      request(options, function (error, response, body) {
        if (error) throw new Error(error);
        expect(body.generated).toBeTruthy()
        expect(body.pdf).toBeTruthy()
      });
    })

Problem

Unfortunately, when I'm using Axis I get following message Error: Cross origin http://localhost forbidden.

Could you please tell me what am I doing wrong?

My code looks like that:

   it('should work via axios', async () => {
        const event = {
            html: "PGgxPkhlbGxvIHt7bmFtZX19PC9oMT48aDI+SGVsbG8gd29ybGQhISE8L2gyPg==",
            variables: "eyJuYW1lIjoiV29ybGQiLCJwYXRoIjoiL2hvbWUvcGF3ZWwvLm52bS92ZXJzaW9ucy9ub2RlL3Y4LjExLjMvYmluOi91c3IvbG9jYWwvc2JpbjovdXNyL2xvY2FsL2JpbjovdXNyL3NiaW46L3Vzci9iaW46L3NiaW46L2JpbjovdXNyL2dhbWVzOi91c3IvbG9jYWwvZ2FtZXM6L3NuYXAvYmluOi91c3IvbGliL2p2bS9qYXZhLTgtb3JhY2xlL2JpbjovdXNyL2xpYi9qdm0vamF2YS04LW9yYWNsZS9kYi9iaW46L3Vzci9saWIvanZtL2phdmEtOC1vcmFjbGUvanJlL2JpbjovdmFyL3Rhc2sifQ==",
            css: "aDEge2NvbG9yOiByZWR9"
        }

        await axios.post(url, event, {
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json',
                },
                responseType: 'json',
                withCredentials: true,
            })
            .then(function (response) {
                console.log(response);
                // console.log('ok')
                expect(response.generated).toBeTruthy()
                expect(response.pdf).toBeTruthy()
            })
            .catch(function (error) {
                console.log(error);
            });
    })

Context

  • axios version: v0.18.0
  • Environment: node v8.11.3
@OpenGG
Copy link

OpenGG commented Aug 25, 2018

Possible duplicated: #1418

@serverlesspolska
Copy link
Author

Thanks @OpenGG . adding const adapter = require('axios/lib/adapters/http') to the config solved the problem for me

@morphatic
Copy link

morphatic commented Nov 5, 2018

I also had this problem. This thread led me to this part of the Jest docs. I was able to fix the issue by pasting:

/**
 * @jest-environment node
 */

At the top of my test file. FWIW, adding:

{
  // ...
  "testEnvironment": "node"
  //...
}

to either jest.config.js or to my package.json file did not work for some reason. Not sure why...

@wangdejun
Copy link

I also had this problem. This thread led me to this part of the Jest docs. I was able to fix the issue by pasting:

/**
 * @jest-environment node
 */

At the top of my test file. FWIW, adding:

{
  // ...
  "testEnvironment": "node"
  //...
}

to either jest.config.js or to my package.json file did not work for some reason. Not sure why...

really thanks, this solves my problem

@zlq4863947
Copy link

@dongyuanxin
Copy link

I also had this problem. This thread led me to this part of the Jest docs. I was able to fix the issue by pasting:

/**
 * @jest-environment node
 */

At the top of my test file. FWIW, adding:

{
  // ...
  "testEnvironment": "node"
  //...
}

to either jest.config.js or to my package.json file did not work for some reason. Not sure why...

thanks very much! I use it in package.json:

{
  "scripts": {
    "test": "jest  --env=node"
  }
}

document: https://jestjs.io/docs/en/cli#env-environment

@odusseys
Copy link

I don't think the solution above is complete, often you will actually want to run your tests in the jsdom environment, if testing UI

@chinesedfan
Copy link
Collaborator

If using jest, users can set testURL to avoid CORS problem.

@squalsoft
Copy link

My jest.config.js resolves this issue (note testEnvironment without quotes):
module.exports = { "transform": { "^.+\\.(ts|tsx)$": "ts-jest" }, testEnvironment: 'node' }

@techieanant
Copy link

Adding anything in my jest config was making other tests fail so adding the line below to my beforeAll function in my tests and it fixed the CORS error.

axios.defaults.adapter = require('axios/lib/adapters/http');

@vidyamore
Copy link

vidyamore commented Feb 25, 2020

Added testEnvironment variable in the Jest.config file within module.exports
module.exports = {
rootDir: "../../",
transform: {"^.+\.tsx?$": "ts-jest"},
setupFiles: ["./config/pact/pactSetup.ts"],
moduleFileExtensions: ["ts", "js"],
moduleNameMapper: {
"^@src/(.)$": "/src/$1",
"^@test/(.
)$": "/test/$1"
},
testRegex: "pact.ts$",
testEnvironment:"node",
setupFilesAfterEnv: ["./config/pact/pactTestWrapper.js"]
};

It resolved the issue.

iwatakeshi added a commit to iwatakeshi/gitly that referenced this issue Mar 16, 2020
@axios axios locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests