Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add initial bits for playwright (#1349)
- Loading branch information
Showing
4 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) 2022 DHGMS Solutions and Contributors. All rights reserved. | ||
// This file is licensed to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Microsoft.Playwright; | ||
|
||
namespace Whipstaff.Playwright | ||
{ | ||
/// <summary> | ||
/// Factory methods for <see cref="RouteFulfillOptions"/>. | ||
/// </summary> | ||
public static class RouteFulfillOptionsFactory | ||
{ | ||
/// <summary> | ||
/// Creates a <see cref="RouteFulfillOptions"/> from a <see cref="HttpResponseMessage"/>. Can be used when carrying out manual HTTP client work inside Playwright. | ||
/// </summary> | ||
/// <param name="httpResponseMessage">HTTP response message to process.</param> | ||
/// <returns>Route Fulfill Options to return to Playwright.</returns> | ||
public static async Task<RouteFulfillOptions> FromHttpResponseMessageAsync(HttpResponseMessage httpResponseMessage) | ||
{ | ||
ArgumentNullException.ThrowIfNull(httpResponseMessage); | ||
|
||
var routeFulfillOptions = new RouteFulfillOptions | ||
{ | ||
Status = (int)httpResponseMessage.StatusCode, | ||
BodyBytes = await httpResponseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false) | ||
}; | ||
|
||
if (httpResponseMessage.Content.Headers.ContentType != null) | ||
{ | ||
routeFulfillOptions.ContentType = httpResponseMessage.Content.Headers.ContentType.ToString(); | ||
} | ||
|
||
return routeFulfillOptions; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Playwright" Version="1.32.0" /> | ||
</ItemGroup> | ||
</Project> |
49 changes: 49 additions & 0 deletions
49
src/Whipstaff.Runtime/Http/HttpRequestMessageExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) 2022 DHGMS Solutions and Contributors. All rights reserved. | ||
// This file is licensed to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
|
||
namespace Whipstaff.Runtime.Http | ||
{ | ||
/// <summary> | ||
/// Extension methods for <see cref="HttpRequestMessage"/>. | ||
/// </summary> | ||
public static class HttpRequestMessageExtensions | ||
{ | ||
/// <summary> | ||
/// Adds a dictionary of single value headers. | ||
/// </summary> | ||
/// <param name="httpRequestMessage">Http Request Message to add headers to.</param> | ||
/// <param name="requestHeaders">Request headers to add.</param> | ||
public static void AddHeaders( | ||
this HttpRequestMessage httpRequestMessage, | ||
IDictionary<string, string> requestHeaders) | ||
{ | ||
var targetHeaders = httpRequestMessage.Headers; | ||
|
||
foreach (var requestHeader in requestHeaders) | ||
{ | ||
targetHeaders.Add(requestHeader.Key, requestHeader.Value); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Adds a dictionary of headers that can contain one of more values.. | ||
/// </summary> | ||
/// <param name="httpRequestMessage">Http Request Message to add headers to.</param> | ||
/// <param name="requestHeaders">Request headers to add.</param> | ||
public static void AddHeaders( | ||
this HttpRequestMessage httpRequestMessage, | ||
IDictionary<string, IEnumerable<string>> requestHeaders) | ||
{ | ||
var targetHeaders = httpRequestMessage.Headers; | ||
|
||
foreach (var requestHeader in requestHeaders) | ||
{ | ||
targetHeaders.Add(requestHeader.Key, requestHeader.Value); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters