-
Notifications
You must be signed in to change notification settings - Fork 158
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
Comments
allthesignals
changed the title
Intercept XMLHttpRequest through a service worker
Intercept XMLHttpRequest through a Service Worker
Jan 20, 2020
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. |
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> |
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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!
The text was updated successfully, but these errors were encountered: