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

Send only necessary commands to retrieve the request post data #228

Closed
derevnjuk opened this issue Mar 7, 2023 · 0 comments · Fixed by #229
Closed

Send only necessary commands to retrieve the request post data #228

derevnjuk opened this issue Mar 7, 2023 · 0 comments · Fixed by #229
Assignees
Labels
Type: bug Something isn't working

Comments

@derevnjuk
Copy link
Member

Stack trace
An output of execution with NODE_DEBUG=cypress-har-generator in the environment:

CYPRESS-HAR-GENERATOR 45676: The plugin sends to the CDP: {"id":7,"method":"Network.getRequestPostData","params":{"requestId":"FD2C692DA16C42898656D6280EA620A0"}}
CYPRESS-HAR-GENERATOR 50148: The plugin receives from the CDP: {"id":7,"error":{"code":-32000,"message":"No post data available for the request"},"sessionId":"C40321408052EA86C1D0A271345D56CD"}

Steps to reproduce

  1. Create a simple mock server as shown below:
import express from 'express';
import bodyParser from 'body-parser';
import exphbs from 'express-handlebars';

const app = express();

// configure middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// configure view engine
app.engine('.hbs', exphbs({ extname: '.hbs' }));
app.set('view engine', '.hbs');

// serve the index page
app.get('/', (_: Request, res: Response) => res.render('./index.hbs'));

// define a route for handling POST requests
app.get('/api/echo', (req: Request, res: Response) => res.json(req.body));

// start the server
const port = 3000;
app.listen(port, () => {
  console.log(`Server started on http://localhost:${port}`);
});

The index.hbs file should look like this:

<html>
  <body>
    <h1>Post Data</h1>
    <pre></pre>
  </body>
  <script>
    const postData = {
      document: document.body.innerHTML,
    };
    const pre = document.querySelector('pre')

    fetch('/api/echo', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(postData)
    })
      .then(response => response.json())
      .then(data => pre.innerText = JSON.stringify(data, null, 2))
      .catch(error => console.error(error));
  </script>
</html>
  1. Open a page and start recording using the following code:
cy.recordHar({ content: false });

cy.visit('/');

cy.saveHar({ waitForIdle: true });

Actual behavior
Although the HAR is recorded and postData is present in the corresponding request, the plugin tries to retrieve the request post data for multiple other requests, as indicated by the logs. This results in redundant commands being sent.

@derevnjuk derevnjuk added the Type: bug Something isn't working label Mar 7, 2023
@derevnjuk derevnjuk self-assigned this Mar 7, 2023
derevnjuk added a commit that referenced this issue Mar 7, 2023
derevnjuk added a commit that referenced this issue Mar 7, 2023
derevnjuk added a commit that referenced this issue Mar 7, 2023
derevnjuk added a commit that referenced this issue Mar 7, 2023
derevnjuk added a commit that referenced this issue Mar 7, 2023
derevnjuk added a commit that referenced this issue Mar 7, 2023
derevnjuk added a commit that referenced this issue Mar 7, 2023
derevnjuk added a commit that referenced this issue Mar 7, 2023
derevnjuk added a commit that referenced this issue Mar 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant