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

Intercept XMLHttpRequest through a Service Worker #287

Open
allthesignals opened this issue Jan 20, 2020 · 3 comments · May be fixed by #351
Open

Intercept XMLHttpRequest through a Service Worker #287

allthesignals opened this issue Jan 20, 2020 · 3 comments · May be fixed by #351

Comments

@allthesignals
Copy link

allthesignals commented Jan 20, 2020

I use PretenderJS (via Ember Mirage) to help test dozens of apps. In most cases, it intercepts network requests perfectly.

However, I also use another 3rd party library (MapboxGL) which issues XMLHttpRequests from within Web Workers. This is a problem because I do not have direct control of their global objects.

Because most of my functionality is built on top of this 3rd party library, being able to test it would greatly improve my test coverage.

(For additional context, this SO question explains my situation exactly. The answer to it suggests a way to intercept these requests using Service Workers.)

I couldn't find another issue related to this, but I'm curious what others think. It seems like Service Workers would help us intercept fetch requests at another level. I'd love to dig into the code if there's an appetite for it, but I could also just be completely off base!

@allthesignals allthesignals changed the title Intercept XMLHttpRequest through a service worker Intercept XMLHttpRequest through a Service Worker Jan 20, 2020
@gabrielcsapo
Copy link

I am currently trying to experiment with https://mswjs.io/docs/getting-started/integrate/browser as a way to show network requests in the network tab while continuing to use pretenderjs. I will update this issue with any findings.

@gabrielcsapo
Copy link

This is the demo code I ran to get some output from my experiment

<!DOCTYPE html>
<head>
  <meta charset="UTF-8">
  <title>Pretenderjs</title>
</head>
<body>
  <script src="./dist/pretender.bundle.js"></script>
  <script>
    (async function() {
      const server = new Pretender(function() {
        this.get('/hello-world', request => [200, {}, "hello world!"]);
      });

      server.unhandledRequest = function(verb, path, request) {
        console.log(verb, path, request);
      }
      
      await fetch('/hello-world');
    }())
  </script>
</body>
</html>

Screen Shot 2022-06-09 at 11 38 29 PM

Screen Shot 2022-06-09 at 11 38 30 PM

@gabrielcsapo
Copy link

gabrielcsapo commented Jun 10, 2022

unhandled requests work as well!

<!DOCTYPE html>
<head>
  <meta charset="UTF-8">
  <title>Pretenderjs</title>
</head>
<body>
  <script src="./dist/pretender.bundle.js"></script>
  <script>
    (async function() {
      const server = new Pretender(function() {
        this.get('/hello-world', request => [200, {}, "hello world!"]);
      });

      server.unhandledRequest = function(verb, path, request) {
        console.log('unhandledRequest', verb, path, request);
      }
      
      await fetch('/hello-world');
      await fetch('/hello');
    }())
  </script>
</body>
</html>

Screen Shot 2022-06-09 at 11 45 40 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants