Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/PuppeteerSharp.Tests/NetworkTests/NetworkEventTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -58,6 +59,23 @@ public async Task PageEventsResponse()
Assert.NotNull(responses[0].Request);
}

[Fact]
public async Task ResponseStatusText()
{
Server.SetRoute("/cool", (context) =>
{
context.Response.StatusCode = 200;
//There are some debates about this on these issues
//https://github.com/aspnet/HttpAbstractions/issues/395
//https://github.com/aspnet/HttpAbstractions/issues/486
//https://github.com/aspnet/HttpAbstractions/issues/794
context.Features.Get<IHttpResponseFeature>().ReasonPhrase = "cool!";
Copy link
Member

Choose a reason for hiding this comment

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

cool 😉

return Task.CompletedTask;
});
var response = await Page.GoToAsync(TestConstants.ServerUrl + "/cool");
Assert.Equal("cool!", response.StatusText);
}

[Fact]
public async Task ResponseFromCache()
{
Expand Down
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp/Messaging/RequestWillBeSentPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class RequestWillBeSentPayload
public Payload Request { get; set; }

[JsonProperty("redirectResponse", NullValueHandling = NullValueHandling.Ignore)]
public ResponseData RedirectResponse { get; set; }
public ResponsePayload RedirectResponse { get; set; }

[JsonProperty("type")]
public ResourceType Type { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@

namespace PuppeteerSharp.Messaging
{
internal class ResponseData
internal class ResponsePayload
{
[JsonProperty("url")]
public string Url { get; internal set; }
public string Url { get; set; }
[JsonProperty("headers")]
public Dictionary<string, object> Headers { get; internal set; }
public Dictionary<string, object> Headers { get; set; }
[JsonProperty("status")]
public HttpStatusCode Status { get; internal set; }
public HttpStatusCode Status { get; set; }
[JsonProperty("securityDetails")]
public SecurityDetails SecurityDetails { get; set; }
[JsonProperty("fromDiskCache")]
public bool FromDiskCache { get; set; }
[JsonProperty("fromServiceWorker")]
public bool FromServiceWorker { get; set; }
[JsonProperty("statusText")]
public string StatusText { get; set; }
}
}
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp/Messaging/ResponseReceivedResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ internal class ResponseReceivedResponse
public string RequestId { get; set; }

[JsonProperty("response")]
public ResponseData Response { get; set; }
public ResponsePayload Response { get; set; }
}
}
28 changes: 4 additions & 24 deletions lib/PuppeteerSharp/NetworkManager.cs
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,7 @@ private void OnResponseReceived(ResponseReceivedResponse e)
var response = new Response(
_client,
request,
e.Response.Status,
e.Response.Headers,
e.Response.FromDiskCache,
e.Response.FromServiceWorker,
e.Response.SecurityDetails);
e.Response);

request.Response = response;

Expand Down Expand Up @@ -268,13 +264,7 @@ private void OnRequest(RequestWillBeSentPayload e, string interceptionId)
// If we connect late to the target, we could have missed the requestWillBeSent event.
if (request != null)
{
HandleRequestRedirect(
request,
e.RedirectResponse.Status,
e.RedirectResponse.Headers,
e.RedirectResponse.FromDiskCache,
e.RedirectResponse.FromServiceWorker,
e.RedirectResponse.SecurityDetails);
HandleRequestRedirect(request, e.RedirectResponse);
redirectChain = request.RedirectChainList;
}
}
Expand Down Expand Up @@ -334,22 +324,12 @@ private void HandleRequestStart(
});
}

private void HandleRequestRedirect(
Request request,
HttpStatusCode redirectStatus,
Dictionary<string, object> redirectHeaders,
bool fromDiskCache,
bool fromServiceWorker,
SecurityDetails securityDetails = null)
private void HandleRequestRedirect(Request request, Messaging.ResponsePayload responseMessage)
{
var response = new Response(
_client,
request,
redirectStatus,
redirectHeaders,
fromDiskCache,
fromServiceWorker,
securityDetails);
responseMessage);

request.Response = response;
request.RedirectChainList.Add(request);
Expand Down
26 changes: 14 additions & 12 deletions lib/PuppeteerSharp/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using PuppeteerSharp.Messaging;

namespace PuppeteerSharp
{
Expand All @@ -18,28 +19,25 @@ public class Response
internal Response(
CDPSession client,
Request request,
HttpStatusCode status,
Dictionary<string, object> headers,
bool fromDiskCache,
bool fromServiceWorker,
SecurityDetails securityDetails)
ResponsePayload responseMessage)
{
_client = client;
Request = request;
Status = status;
Status = responseMessage.Status;
StatusText = responseMessage.StatusText;
Url = request.Url;
_fromDiskCache = fromDiskCache;
FromServiceWorker = fromServiceWorker;
_fromDiskCache = responseMessage.FromDiskCache;
FromServiceWorker = responseMessage.FromServiceWorker;

Headers = new Dictionary<string, object>();
if (headers != null)
if (responseMessage.Headers != null)
{
foreach (var keyValue in headers)
foreach (var keyValue in responseMessage.Headers)
{
Headers[keyValue.Key] = keyValue.Value;
}
}
SecurityDetails = securityDetails;
SecurityDetails = responseMessage.SecurityDetails;
BodyLoadedTaskWrapper = new TaskCompletionSource<bool>();
}

Expand Down Expand Up @@ -83,7 +81,11 @@ internal Response(
/// </summary>
/// <value><c>true</c> if the <see cref="Response"/> was served by a service worker; otherwise, <c>false</c>.</value>
public bool FromServiceWorker { get; }

/// <summary>
/// Contains the status text of the response (e.g. usually an "OK" for a success).
/// </summary>
/// <value>The status text.</value>
public string StatusText { get; }
internal TaskCompletionSource<bool> BodyLoadedTaskWrapper { get; }
#endregion

Expand Down