Skip to content

[🐛 Bug]: Mock callback receives invalid body of response for binary files #11709

@Antonius-S

Description

@Antonius-S

Have you read the Contributing Guidelines on issues?

WebdriverIO Version

latest

Node.js Version

latest

Mode

Standalone Mode

Which capabilities are you using?

{
          browserName: 'chrome',
}

What happened?

  • Setup mocking with callback to receive responses
  • Visit a site
  • See all binary responses have damaged body (wrongly encoded to UTF-8 string)

What is your expected behavior?

All binary responses have body of type Buffer not encoded as UTF-8 string

The source is this line

request.body = base64Encoded ? Buffer.from(body, 'base64').toString('utf8') : body

It doesn't only damages binary resources but also probably will damage any text resources encoded with other encodings.

As quick fix, I changed it to:

                const { body, base64Encoded = undefined } = isRequest ? { body: '' } : await client.send('Fetch.getResponseBody', { requestId }).catch(/* istanbul ignore next */ () => ({}));
                if (base64Encoded)
                {
                    request.bodyBuf = Buffer.from(body, 'base64');
                    request.body = request.bodyBuf.toString('utf8');
                }
                else
                    request.body = body;

so that request always has unencoded buffer and body could be damaged or not but in fact there must be more advanced logic to properly encode body according to content-encoding and content-type

How to reproduce the bug.

'use strict';

const wdio = require('webdriverio');

/** @typedef {import ('webdriverio/build/types')} wdio */ // linter happy
/** @typedef {import ('webdriverio/build/utils/interception/types').MockOverwriteFunction} wdio.MockOverwriteFunction */
/** @typedef {import ('webdriverio/build/types').Mock} wdio.Mock */

/** @type {wdio.MockOverwriteFunction} */
function cb(resp)
{
  console.log(resp.url);
  require('fs').writeFileSync(String(new Date().getTime()) + '.png', Buffer.from(resp.body, 'utf-8'));
}

/** Console main */
async function main()
{
  // Init WebDriver
  const browser = await wdio.remote({
    capabilities:
      {
        browserName: 'chrome'
      }});
  /** @type {wdio.Mock} */
  const mock = await browser.mock('**/*.png');
  mock.respond(cb);
  await browser.url('https://www.google.com/');
  await browser.deleteSession();
}

main();

And see all the PNG's damaged

Relevant log output

2023-11-22T12:33:53.868Z INFO webdriver: Initiate new session using the WebDriver protocol
2023-11-22T12:33:53.922Z INFO webdriver: [POST] http://localhost:4444/session
2023-11-22T12:33:53.923Z INFO webdriver: DATA {
  capabilities: { alwaysMatch: { browserName: 'chrome' }, firstMatch: [ {} ] },
  desiredCapabilities: { browserName: 'chrome' }
}
2023-11-22T12:33:55.759Z INFO webdriver: COMMAND getWindowHandle()
2023-11-22T12:33:55.761Z INFO webdriver: [GET] http://localhost:4444/session/cf98f0e99b8c2412270d4285f3f29390/window
2023-11-22T12:33:55.764Z INFO webdriver: RESULT CDwindow-0AAD6EBFC585506394CE7AD4BF9F78F8
2023-11-22T12:33:55.782Z INFO webdriver: COMMAND navigateTo("https://www.google.com/favicon.ico")
2023-11-22T12:33:55.782Z INFO webdriver: [POST] http://localhost:4444/session/cf98f0e99b8c2412270d4285f3f29390/url
2023-11-22T12:33:55.783Z INFO webdriver: DATA { url: 'https://www.google.com/favicon.ico' }
https://www.google.com/favicon.ico
2023-11-22T12:33:55.944Z INFO webdriver: COMMAND deleteSession()
2023-11-22T12:33:55.945Z INFO webdriver: [DELETE] http://localhost:4444/session/cf98f0e99b8c2412270d4285f3f29390

Code of Conduct

  • I agree to follow this project's Code of Conduct

Is there an existing issue for this?

  • I have searched the existing issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug 🐛good first picka reasonable task to start getting familiar with the code basehelp wantedIssues that are free to take by anyone interested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions