Netflix / pollyjs

Record, Replay, and Stub HTTP Interactions.
https://netflix.github.io/pollyjs
Apache License 2.0
10.22k stars 352 forks source link

Puppeteer adapter: "Evaluation failed: TypeError: Failed to fetch" #149

Closed 2xAA closed 5 years ago

2xAA commented 5 years ago

I'm attempting to use Polly in an existing project to test a webform, however upon adding Polly with the Puppeteer adapter I'm running into this error from Puppeteer.

Evaluation failed: TypeError: Failed to fetch

This error isn't present without Polly and the Puppeteer adapter.

I can provide more details upon request, but I'm unsure on what I need to share to diagnose this issue right now.

Thanks.

offirgolan commented 5 years ago

@2xAA if you can post the following:

2xAA commented 5 years ago

We decided not to use Polly in our project in the end. Using Puppeteer by itself provides enough functionality for us.

I doubt this issue would have been too helpful anyway as I'm unable to share the website we were testing or much of the test code.

Using the same test code with other websites didn't produce this error.

Setting the requestResourceTypes array to use only xhr squished this error, but then Puppeteer was throwing a different eval error for XHR requests instead.

Setting requestResourceTypes to a blank Array allowed this to run without errors, but no network requests were being captured by Puppeteer either.

These are my versions, just in case:

jasonmit commented 5 years ago

Closing for now since there isn't anything actionable. We'll keep an eye out for this in the future.

scotthovestadt commented 5 years ago

FYI this happens because of CSP errors and can be resolved with:

await page.setBypassCSP(true);
jasonmit commented 5 years ago

@scotthovestadt do you know in what scenario this happens?

Any chance you can PR a fail test for adapter-puppeteer/tests/integration/adapter-test.js

scotthovestadt commented 5 years ago

An easy way to reproduce it is to just open the console on github.com (this page works fine) and enter:

fetch('https://github.githubassets.com/favicon.ico')
Screen Shot 2019-04-09 at 12 41 02 AM

This shows in the console as:

Evaluation failed: TypeError: Failed to fetch

      at anonymous (__puppeteer_evaluation_script__:3:8)
      at ExecutionContext.evaluateHandle (node_modules/puppeteer/lib/ExecutionContext.js:122:13)
      ...

It's triggered by the following header:

Content-Security-Policy: default-src 'none'; base-uri 'self'; block-all-mixed-content; connect-src 'self' uploads.github.com www.githubstatus.com collector.githubapp.com api.github.com www.google-analytics.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com wss://live.github.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com; frame-ancestors 'none'; frame-src render.githubusercontent.com; img-src 'self' data: github.githubassets.com identicons.github.com collector.githubapp.com github-cloud.s3.amazonaws.com *.githubusercontent.com; manifest-src 'self'; media-src 'none'; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com

I'll look into setting up a fail test, but regardless is probably a good idea to just recommend that the user turn this on in the documentation or even in the error message for fetch failing. I'm not seeing an easier way to work around it (unless you want to turn it on for them).

Thoughts?

jasonmit commented 5 years ago

@scotthovestadt I agree with adding a warning the documentation.

However, I'm a bit confused. If the CSP rules do not account for a hostname you're trying to reach then I'd except the CSP violation with or without Polly being present. Is that not the case?

scotthovestadt commented 5 years ago

It's not trying to fetch the resources by itself, that's done by Polly: https://github.com/Netflix/pollyjs/blob/master/packages/%40pollyjs/adapter-puppeteer/src/index.js#L179

Ordinarily the favico would be loaded "normally" but it's proxied through fetch and then fails because of CSP.

jasonmit commented 5 years ago

This sounds like a bug where non-xhr requests are being picked up via Polly. The expectation would be Polly wouldn't be proxying these sorts of resource requests. We'll need to investigate why that's happening.

scotthovestadt commented 5 years ago

Likely related to: https://github.com/Netflix/pollyjs/blob/master/packages/%40pollyjs/adapter-puppeteer/src/index.js#L17

jasonmit commented 5 years ago

@scotthovestadt yeah, exactly! Will be resolved by https://github.com/Netflix/pollyjs/pull/197

Thanks for bringing all this context to the discussion, super helpful.

scotthovestadt commented 5 years ago

Glad I could help!