angrykoala/wendigo

CORS mock not resolving

victor141516 opened this issue · 2 comments

I tried to mock a CORS request and it didn't work.
The mock appears as called but the fetch in the web code is not resolving.

Here is a piece of code that reproduces the issue:

const Wendigo = require('wendigo');


const MAIN_ORIGIN = 'http://example.com'
const MAIN_URL = `${MAIN_ORIGIN}/hello`;
const FETCH_URL = 'http://otherexample.com/eehehhehe';

let browser;

async function test() {
    browser = await Wendigo.createBrowser();
    browser.requests.mock(MAIN_URL, {
        body: `<html>
            <head>
                <script>
                    (function(){
                        fetch("${FETCH_URL}")
                            .then(() => (document.getElementById('the-id').innerHTML = 'HOORAYYYY'))
                    })();
                </script>
            </head>
            <body>
                <h1>HEYYYY</h1>
                <h2 id="the-id">ITZ A SAZ DAY :(</h2>
            </body>
        </html>`,
    });

    const mock = browser.requests.mock(FETCH_URL, {
        body: 'hihihihihiihhi',
        headers: {
            'Access-Control-Allow-Headers ': 'authorization,content-type',
            'Access-Control-Allow-Methods': 'PATCH,GET,OPTIONS,POST,PUT',
            'Access-Control-Allow-Origin': MAIN_ORIGIN,
            'Access-Contro-Allow-Credentials': 'true',
        },
    });

    await browser.wait();
    await browser.open(MAIN_URL);
    await browser.waitForText('HEYYYY', 5000); // This works
    await mock.assert.called(); // This works
    await browser.waitForText('HOORAYYYY', 5000); // This doesn't
}


test().finally(() => browser.close());

For now I do the following:

let browser = await Wendigo.createBrowser({
    args: ['--disable-web-security'],
})

Doing this skips OPTIONS preflight request.

I haven't come back to this yet, the workaround found by @guillenotfound seems good enough. When properly fixing this, we may need to consider setting that flag as a default.

Adding this workaround to troubleshooting guide may be useful