pretenderjs / pretender

A mock server library with a nice routing DSL
MIT License
1.26k stars 158 forks source link

feat: adds support to utilize service workers to see network response for mocked requests #351

Open gabrielcsapo opened 2 years ago

gabrielcsapo commented 2 years ago

fixes #287

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

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