Skip to content

Commit 45880a9

Browse files
authored
BufferAsync should work with binaries (#766)
1 parent 6375671 commit 45880a9

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Threading.Tasks;
2+
using Xunit;
3+
using Xunit.Abstractions;
4+
5+
namespace PuppeteerSharp.Tests.Issues
6+
{
7+
[Collection("PuppeteerLoaderFixture collection")]
8+
public class Issue0764 : PuppeteerPageBaseTest
9+
{
10+
public Issue0764(ITestOutputHelper output) : base(output)
11+
{
12+
}
13+
14+
[Fact]
15+
public async Task BufferAsyncShouldWorkWithBinaries()
16+
{
17+
var tcs = new TaskCompletionSource<byte[]>();
18+
Page.Response += async (sender, e) =>
19+
{
20+
if (e.Response.Url.Contains("digits/0.png"))
21+
{
22+
tcs.TrySetResult(await e.Response.BufferAsync());
23+
}
24+
};
25+
26+
await Task.WhenAll(
27+
Page.GoToAsync(TestConstants.ServerUrl + "/grid.html"),
28+
tcs.Task);
29+
Assert.True(ScreenshotHelper.PixelMatch("0.png", await tcs.Task));
30+
}
31+
}
32+
}
434 Bytes
Loading

lib/PuppeteerSharp/Response.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class Response
1515
{
1616
private readonly CDPSession _client;
1717
private readonly bool _fromDiskCache;
18-
private string _buffer;
18+
private byte[] _buffer;
1919

2020
internal Response(
2121
CDPSession client,
@@ -112,7 +112,7 @@ internal Response(
112112
/// Returns a Task which resolves to a buffer with response body
113113
/// </summary>
114114
/// <returns>A Task which resolves to a buffer with response body</returns>
115-
public async ValueTask<string> BufferAsync()
115+
public async ValueTask<byte[]> BufferAsync()
116116
{
117117
if (_buffer == null)
118118
{
@@ -125,9 +125,9 @@ public async ValueTask<string> BufferAsync()
125125
{ MessageKeys.RequestId, Request.RequestId}
126126
}).ConfigureAwait(false);
127127

128-
_buffer = response.Base64Encoded
129-
? Encoding.UTF8.GetString(Convert.FromBase64String(response.Body))
130-
: response.Body;
128+
_buffer = response.Base64Encoded
129+
? Convert.FromBase64String(response.Body)
130+
: Encoding.UTF8.GetBytes(response.Body);
131131
}
132132
catch (Exception ex)
133133
{
@@ -142,7 +142,7 @@ public async ValueTask<string> BufferAsync()
142142
/// Returns a Task which resolves to a text representation of response body
143143
/// </summary>
144144
/// <returns>A Task which resolves to a text representation of response body</returns>
145-
public ValueTask<string> TextAsync() => BufferAsync();
145+
public async ValueTask<string> TextAsync() => Encoding.UTF8.GetString(await BufferAsync().ConfigureAwait(false));
146146

147147
/// <summary>
148148
/// Returns a Task which resolves to a <see cref="JObject"/> representation of response body

0 commit comments

Comments
 (0)