Skip to content

Commit

Permalink
add initial bits for playwright (#1349)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpvreony committed May 2, 2023
1 parent 0fd6896 commit b3c7f2d
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/Whipstaff.Playwright/RouteFulfillOptionsFactory.cs
@@ -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;
}
}
}
11 changes: 11 additions & 0 deletions src/Whipstaff.Playwright/Whipstaff.Playwright.csproj
@@ -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 src/Whipstaff.Runtime/Http/HttpRequestMessageExtensions.cs
@@ -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);
}
}
}
}
8 changes: 7 additions & 1 deletion src/Whipstaff.sln
Expand Up @@ -47,7 +47,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Whipstaff.Entityframework.R
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Whipstaff.Couchbase", "Whipstaff.Couchbase\Whipstaff.Couchbase.csproj", "{8107A239-69D2-40AC-918B-59A77159EC52}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Whipstaff.NodaTime", "Whipstaff.NodaTime\Whipstaff.NodaTime.csproj", "{74CF7649-3848-4265-BC96-A5DCA8D176AE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Whipstaff.NodaTime", "Whipstaff.NodaTime\Whipstaff.NodaTime.csproj", "{74CF7649-3848-4265-BC96-A5DCA8D176AE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Whipstaff.Playwright", "Whipstaff.Playwright\Whipstaff.Playwright.csproj", "{6C010B59-155B-4E48-9FDF-DCCD549706A4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -143,6 +145,10 @@ Global
{74CF7649-3848-4265-BC96-A5DCA8D176AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{74CF7649-3848-4265-BC96-A5DCA8D176AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{74CF7649-3848-4265-BC96-A5DCA8D176AE}.Release|Any CPU.Build.0 = Release|Any CPU
{6C010B59-155B-4E48-9FDF-DCCD549706A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6C010B59-155B-4E48-9FDF-DCCD549706A4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6C010B59-155B-4E48-9FDF-DCCD549706A4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6C010B59-155B-4E48-9FDF-DCCD549706A4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit b3c7f2d

Please sign in to comment.