Add WireMock.Net.AspNetCore.Middleware (#1175)

* Add WireMock.Net.AspNetCore.Middleware

* .

* WireMock.Net.Middleware.Tests

* .

* X-WireMock-Response-Delay
This commit is contained in:
Stef Heyenrath
2024-09-27 20:39:57 +02:00
committed by GitHub
parent c57590b2ba
commit 42306d1864
23 changed files with 641 additions and 1 deletions

View File

@@ -0,0 +1,25 @@
// Copyright © WireMock.Net
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
namespace WireMock.Net.Middleware.Tests;
internal class CustomWebApplicationFactory<TEntryPoint> : WebApplicationFactory<TEntryPoint>
where TEntryPoint : class
{
private readonly List<(string Key, string Value)> _settings = new();
public CustomWebApplicationFactory(bool alwaysRedirectToWireMock = true)
{
_settings.Add(("AlwaysRedirectToWireMock", alwaysRedirectToWireMock.ToString().ToLowerInvariant()));
}
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
foreach (var arg in _settings)
{
builder.UseSetting(arg.Key, arg.Value);
}
}
}

View File

@@ -0,0 +1,49 @@
// Copyright © WireMock.Net
using FluentAssertions;
using WireMock.Net.TestWebApplication;
namespace WireMock.Net.Middleware.Tests;
public class IntegrationTests
{
[Theory]
[InlineData("/real1", "Hello 1 from WireMock.Net !")]
[InlineData("/real2", "Hello 2 from WireMock.Net !")]
public async Task CallingRealApi_WithAlwaysRedirectToWireMockIsTrue(string requestUri, string expectedResponse)
{
// Arrange
await using var factory = new CustomWebApplicationFactory<Program>();
using var client = factory.CreateClient();
// Act
var response = await client.GetAsync(requestUri);
// Assert
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
stringResponse.Should().Be(expectedResponse);
}
[Theory]
[InlineData("/real1", "Hello 1 from WireMock.Net !")]
[InlineData("/real2", "Hello 2 from WireMock.Net !")]
public async Task CallingRealApi_WithAlwaysRedirectToWireMockIsFalse(string requestUri, string expectedResponse)
{
// Arrange
await using var factory = new CustomWebApplicationFactory<Program>(false);
using var client = factory.CreateClient();
var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
request.Headers.Add("X-WireMock-Redirect", "true");
request.Headers.Add("X-WireMock-Response-Delay", "10");
// Act
var response = await client.SendAsync(request);
// Assert
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
stringResponse.Should().Be(expectedResponse);
}
}

View File

@@ -0,0 +1,43 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>../../src/WireMock.Net/WireMock.Net.snk</AssemblyOriginatorKeyFile>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Codecov" Version="1.13.0" />
<PackageReference Include="coverlet.msbuild" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WireMock.Net.TestWebApplication\WireMock.Net.TestWebApplication.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
</Project>