GithubHelp home page GithubHelp logo

Comments (1)

mxschmitt avatar mxschmitt commented on June 9, 2024 1

I was able to reproduce on Chromium, but not on WebKit/Firefox. Looks like a bug.

// @ts-check
import http from 'http';
import playwright from 'playwright';
const root = await startLocalServer();

const browser = await playwright.chromium.launch();

const context = await browser.newContext();
const page = await context.newPage();

async function handler(response) {
  console.log('on response start', response.url());
  console.log('on response status text:', response.statusText());
  console.log('on response headers:', await response.headersArray());
  console.log('on response text:', await response.body()); // <<-- this triggers a remote fetch, regardless. Does not happen when commented out.
  console.log('on response headers2:', await response.headersArray()); // Same as before
  console.log('on response finish');
}
page.on('response', response => {
  handler(response).then(() => console.log('handler completed...'), e => console.log('handler crashed:', e));
});

await page.route('**', async route => {
  const request = route.request();
  const url = request.url();

  if (url === `${root}/main`) {
    console.log('Root (remote) response:');
    const response = await route.fetch();
    return await route.fulfill({ response });
  }

  console.log('Fail (mock) response:', [url]);
  return route.fulfill({
    status: 404,
    contentType: 'text/plain',
    body: 'Not Found! (mocked)',
  });
});

await page.goto(`${root}/main`);

await page.waitForLoadState('networkidle');

console.log('Closing page');
await page.close();

async function startLocalServer() {
  const PORT = 3022;
  const HOST = 'localhost';

  // Serve local website
  const server = http.createServer((req, res) => {
    console.log('server hit:', req.method, req.url);

    if (req.url === '/main') {
      res.statusCode = 200;
      res.setHeader('x-custom', 'stuff');
      res.setHeader('Content-Type', 'text/html');
      res.end('<body>main response <script src="/fail"></script>');
      return;
    }

    res.statusCode = 404; // Can return any status code here, doesn't matter really
    res.setHeader('Content-Type', 'text/html'); // Content type doesn't appear to matter either
    res.setHeader('x-custom', 'stuff');
    res.end('This response text should not be fetched and/or displayed'); // At least this doesn't show up?
  });

  await new Promise(resolve => {
    server.listen(PORT, HOST, () => {
      resolve(undefined);
    });
  });

  return `http://${HOST}:${PORT}`;
}

Expected: No server hit: GET /fail
Actual: server hit: GET /fail

getting printed.

from playwright.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.