question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: Cross origin http://localhost forbidden

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11

github_iconTop GitHub Comments

139reactions
morphaticcommented, 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…

11reactions
wangdejuncommented, Jan 9, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cross origin http://localhost forbidden - in ReactJS/Jest test ...
The error happens because the external API you call during the tests has a CORS restriction different than Jest's default jsdom one has ......
Read more >
Jest + Node + SharePoint + MSAL = Error: Cross origin http ...
If you are getting the following errors when you try and use MSAL with JEST to connect to SharePoint: Error: Cross origin http://localhost...
Read more >
Cross origin http://localhost forbidden - in ReactJS/Jest test ...
The error happens because the external API you call during the tests has a CORS restriction different than Jest's default jsdom one has...
Read more >
error cross origin http //localhost forbidden jest ...
Answers related to “error cross origin http //localhost forbidden jest”. How to by pass CORS error locally · access to xmlhttprequest at from...
Read more >
CORS errors - HTTP - MDN Web Docs - Mozilla
Cross -Origin Resource Sharing (CORS) is a standard that allows a server to relax the same-origin policy. This is used to explicitly allow ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found