Skip to content

[dotnet] Fix network response data encoding #13576

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

Merged
merged 4 commits into from
Feb 16, 2024

Conversation

nvborisenko
Copy link
Member

@nvborisenko nvborisenko commented Feb 13, 2024

Description

NetworkManager exposes HTTP response body data as string type. Not all responses have strings. In case if the response is bytes, selenium is doing extra convertion from bytes to utf8 string. It leads to data loosing, we cannot convert string back to bytes.

Motivation and Context

Fix #11726

New approach to get data

network.NetworkResponseReceived += (sender, e) =>
{
    if (e.ResponseResourceType == "Image")
    {
        Console.WriteLine($"{e.ResponseUrl} : {e.ResponseResourceType}");
        if (!string.IsNullOrEmpty(e.ResponseBody))
        {
            Console.WriteLine(Encoding.UTF8.GetBytes(e.ResponseBody).Length);
        }

        if (e.ResponseContent != null)
        {
            Console.WriteLine(e.ResponseContent.ReadAsByteArray().Length);
        }
    }
};

await network.StartMonitoring();

driver.Url = "https://selenium.dev";

And its output is:

https://www.selenium.dev/images/sponsors/saucelabs.png : Image
11191
6255

where 6255 is correct bytes of the image.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@nvborisenko nvborisenko added this to the 4.18 milestone Feb 14, 2024
@diemol diemol force-pushed the dotnet-fix-network-response branch from fb45ff0 to 027280a Compare February 16, 2024 10:11
Copy link
Member

@diemol diemol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @nvborisenko!

@diemol diemol merged commit 0209bec into SeleniumHQ:trunk Feb 16, 2024
@nvborisenko nvborisenko deleted the dotnet-fix-network-response branch February 17, 2024 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[🚀 Feature]: Direct access to the raw data of the body via stream/byte array and not exclusively as string
2 participants