Skip to content

Commit fb4cf11

Browse files
kblokMeir017
authored andcommitted
Improve Request overrides on ContinueAsync (#617)
* Improve Request overrides on ContinueAsync * cr and better test
1 parent a3f4175 commit fb4cf11

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using Microsoft.AspNetCore.Http;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Net;
7+
using System.Net.Http;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
using Xunit;
11+
using Xunit.Abstractions;
12+
13+
namespace PuppeteerSharp.Tests.Issues
14+
{
15+
[Collection("PuppeteerLoaderFixture collection")]
16+
public class Issue0616 : PuppeteerPageBaseTest
17+
{
18+
public Issue0616(ITestOutputHelper output) : base(output)
19+
{
20+
}
21+
22+
[Fact]
23+
public async Task ShouldBeAbleToChangeToPost()
24+
{
25+
await Page.SetRequestInterceptionAsync(true);
26+
Page.Request += async (sender, e) =>
27+
{
28+
var payload = new Payload()
29+
{
30+
Method = HttpMethod.Post,
31+
PostData = "foo"
32+
};
33+
await e.Request.ContinueAsync(payload);
34+
};
35+
36+
Server.SetRoute("/grid.html", async (context) =>
37+
{
38+
using (var reader = new StreamReader(context.Request.Body, Encoding.UTF8))
39+
{
40+
var request = await reader.ReadToEndAsync();
41+
await context.Response.WriteAsync(request);
42+
}
43+
});
44+
var response = await Page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
45+
46+
Assert.Equal(TestConstants.ServerUrl + "/grid.html", response.Url);
47+
Assert.Equal("foo", await response.TextAsync());
48+
}
49+
}
50+
}

lib/PuppeteerSharp/Payload.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ namespace PuppeteerSharp
1313
/// </summary>
1414
public class Payload
1515
{
16-
internal Payload()
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="PuppeteerSharp.Payload"/> class.
18+
/// </summary>
19+
public Payload()
1720
{
1821
Headers = new Dictionary<string, object>();
1922
}

lib/PuppeteerSharp/Request.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,15 @@ public async Task ContinueAsync(Payload overrides = null)
180180

181181
if (overrides?.Method != null)
182182
{
183-
requestData["method"] = overrides.Method;
183+
requestData["method"] = overrides.Method.ToString();
184184
}
185185

186186
if (overrides?.PostData != null)
187187
{
188188
requestData["postData"] = overrides.PostData;
189189
}
190190

191-
if (overrides?.Headers != null)
191+
if (overrides?.Headers?.Count > 0)
192192
{
193193
requestData["headers"] = overrides.Headers;
194194
}

0 commit comments

Comments
 (0)