mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-22 17:19:00 +01:00
Add WireMock.Net.AspNetCore.Middleware (#1175)
* Add WireMock.Net.AspNetCore.Middleware * . * WireMock.Net.Middleware.Tests * . * X-WireMock-Response-Delay
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
49
test/WireMock.Net.Middleware.Tests/IntegrationTests.cs
Normal file
49
test/WireMock.Net.Middleware.Tests/IntegrationTests.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
49
test/WireMock.Net.TestWebApplication/Program.cs
Normal file
49
test/WireMock.Net.TestWebApplication/Program.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using WireMock.Net.AspNetCore.Middleware;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
|
||||
namespace WireMock.Net.TestWebApplication;
|
||||
|
||||
// Make the implicit Program class public so test projects can access it.
|
||||
public class Program
|
||||
{
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
var alwaysRedirectToWireMock = args.Contains("--AlwaysRedirectToWireMock=true");
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddWireMockService(server =>
|
||||
{
|
||||
server.Given(Request.Create()
|
||||
.WithPath("/test1")
|
||||
.UsingAnyMethod()
|
||||
).RespondWith(Response.Create()
|
||||
.WithBody("Hello 1 from WireMock.Net !")
|
||||
);
|
||||
|
||||
server.Given(Request.Create()
|
||||
.WithPath("/test2")
|
||||
.UsingAnyMethod()
|
||||
).RespondWith(Response.Create()
|
||||
.WithBody("Hello 2 from WireMock.Net !")
|
||||
);
|
||||
}, alwaysRedirectToWireMock);
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.MapGet("/real1", async (HttpClient client) =>
|
||||
{
|
||||
var result = await client.GetStringAsync("https://real-api:12345/test1");
|
||||
return result;
|
||||
});
|
||||
|
||||
app.MapGet("/real2", async (IHttpClientFactory factory) =>
|
||||
{
|
||||
using var client = factory.CreateClient();
|
||||
return await client.GetStringAsync("https://real-api:12345/test2");
|
||||
});
|
||||
|
||||
await app.RunAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"profiles": {
|
||||
"WireMock.Net.TestWebApplication": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"applicationUrl": "https://localhost:57712;http://localhost:57713"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\WireMock.Net.AspNetCore.Middleware\WireMock.Net.AspNetCore.Middleware.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
9
test/WireMock.Net.TestWebApplication/appsettings.json
Normal file
9
test/WireMock.Net.TestWebApplication/appsettings.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Reference in New Issue
Block a user