Skip to content

Commit ad30cf5

Browse files
authored
Introduce Response.StatusText (#651)
1 parent 460d905 commit ad30cf5

File tree

6 files changed

+43
-42
lines changed

6 files changed

+43
-42
lines changed

lib/PuppeteerSharp.Tests/NetworkTests/NetworkEventTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Http.Features;
23
using Newtonsoft.Json.Linq;
34
using System.Collections.Generic;
45
using System.Linq;
@@ -58,6 +59,23 @@ public async Task PageEventsResponse()
5859
Assert.NotNull(responses[0].Request);
5960
}
6061

62+
[Fact]
63+
public async Task ResponseStatusText()
64+
{
65+
Server.SetRoute("/cool", (context) =>
66+
{
67+
context.Response.StatusCode = 200;
68+
//There are some debates about this on these issues
69+
//https://github.com/aspnet/HttpAbstractions/issues/395
70+
//https://github.com/aspnet/HttpAbstractions/issues/486
71+
//https://github.com/aspnet/HttpAbstractions/issues/794
72+
context.Features.Get<IHttpResponseFeature>().ReasonPhrase = "cool!";
73+
return Task.CompletedTask;
74+
});
75+
var response = await Page.GoToAsync(TestConstants.ServerUrl + "/cool");
76+
Assert.Equal("cool!", response.StatusText);
77+
}
78+
6179
[Fact]
6280
public async Task ResponseFromCache()
6381
{

lib/PuppeteerSharp/Messaging/RequestWillBeSentPayload.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal class RequestWillBeSentPayload
1414
public Payload Request { get; set; }
1515

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

1919
[JsonProperty("type")]
2020
public ResourceType Type { get; set; }

lib/PuppeteerSharp/Messaging/ResponseData.cs renamed to lib/PuppeteerSharp/Messaging/ResponsePayload.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44

55
namespace PuppeteerSharp.Messaging
66
{
7-
internal class ResponseData
7+
internal class ResponsePayload
88
{
99
[JsonProperty("url")]
10-
public string Url { get; internal set; }
10+
public string Url { get; set; }
1111
[JsonProperty("headers")]
12-
public Dictionary<string, object> Headers { get; internal set; }
12+
public Dictionary<string, object> Headers { get; set; }
1313
[JsonProperty("status")]
14-
public HttpStatusCode Status { get; internal set; }
14+
public HttpStatusCode Status { get; set; }
1515
[JsonProperty("securityDetails")]
1616
public SecurityDetails SecurityDetails { get; set; }
1717
[JsonProperty("fromDiskCache")]
1818
public bool FromDiskCache { get; set; }
1919
[JsonProperty("fromServiceWorker")]
2020
public bool FromServiceWorker { get; set; }
21+
[JsonProperty("statusText")]
22+
public string StatusText { get; set; }
2123
}
2224
}

lib/PuppeteerSharp/Messaging/ResponseReceivedResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ internal class ResponseReceivedResponse
88
public string RequestId { get; set; }
99

1010
[JsonProperty("response")]
11-
public ResponseData Response { get; set; }
11+
public ResponsePayload Response { get; set; }
1212
}
1313
}

lib/PuppeteerSharp/NetworkManager.cs

100755100644
Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,7 @@ private void OnResponseReceived(ResponseReceivedResponse e)
175175
var response = new Response(
176176
_client,
177177
request,
178-
e.Response.Status,
179-
e.Response.Headers,
180-
e.Response.FromDiskCache,
181-
e.Response.FromServiceWorker,
182-
e.Response.SecurityDetails);
178+
e.Response);
183179

184180
request.Response = response;
185181

@@ -268,13 +264,7 @@ private void OnRequest(RequestWillBeSentPayload e, string interceptionId)
268264
// If we connect late to the target, we could have missed the requestWillBeSent event.
269265
if (request != null)
270266
{
271-
HandleRequestRedirect(
272-
request,
273-
e.RedirectResponse.Status,
274-
e.RedirectResponse.Headers,
275-
e.RedirectResponse.FromDiskCache,
276-
e.RedirectResponse.FromServiceWorker,
277-
e.RedirectResponse.SecurityDetails);
267+
HandleRequestRedirect(request, e.RedirectResponse);
278268
redirectChain = request.RedirectChainList;
279269
}
280270
}
@@ -334,22 +324,12 @@ private void HandleRequestStart(
334324
});
335325
}
336326

337-
private void HandleRequestRedirect(
338-
Request request,
339-
HttpStatusCode redirectStatus,
340-
Dictionary<string, object> redirectHeaders,
341-
bool fromDiskCache,
342-
bool fromServiceWorker,
343-
SecurityDetails securityDetails = null)
327+
private void HandleRequestRedirect(Request request, Messaging.ResponsePayload responseMessage)
344328
{
345329
var response = new Response(
346330
_client,
347331
request,
348-
redirectStatus,
349-
redirectHeaders,
350-
fromDiskCache,
351-
fromServiceWorker,
352-
securityDetails);
332+
responseMessage);
353333

354334
request.Response = response;
355335
request.RedirectChainList.Add(request);

lib/PuppeteerSharp/Response.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,25 @@ public class Response
2020
internal Response(
2121
CDPSession client,
2222
Request request,
23-
HttpStatusCode status,
24-
Dictionary<string, object> headers,
25-
bool fromDiskCache,
26-
bool fromServiceWorker,
27-
SecurityDetails securityDetails)
23+
ResponsePayload responseMessage)
2824
{
2925
_client = client;
3026
Request = request;
31-
Status = status;
27+
Status = responseMessage.Status;
28+
StatusText = responseMessage.StatusText;
3229
Url = request.Url;
33-
_fromDiskCache = fromDiskCache;
34-
FromServiceWorker = fromServiceWorker;
30+
_fromDiskCache = responseMessage.FromDiskCache;
31+
FromServiceWorker = responseMessage.FromServiceWorker;
3532

3633
Headers = new Dictionary<string, object>();
37-
if (headers != null)
34+
if (responseMessage.Headers != null)
3835
{
39-
foreach (var keyValue in headers)
36+
foreach (var keyValue in responseMessage.Headers)
4037
{
4138
Headers[keyValue.Key] = keyValue.Value;
4239
}
4340
}
44-
SecurityDetails = securityDetails;
41+
SecurityDetails = responseMessage.SecurityDetails;
4542
BodyLoadedTaskWrapper = new TaskCompletionSource<bool>();
4643
}
4744

@@ -85,7 +82,11 @@ internal Response(
8582
/// </summary>
8683
/// <value><c>true</c> if the <see cref="Response"/> was served by a service worker; otherwise, <c>false</c>.</value>
8784
public bool FromServiceWorker { get; }
88-
85+
/// <summary>
86+
/// Contains the status text of the response (e.g. usually an "OK" for a success).
87+
/// </summary>
88+
/// <value>The status text.</value>
89+
public string StatusText { get; }
8990
internal TaskCompletionSource<bool> BodyLoadedTaskWrapper { get; }
9091
#endregion
9192

0 commit comments

Comments
 (0)