-
-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
210 additions
and
6 deletions.
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
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
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
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
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
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
25 changes: 25 additions & 0 deletions
25
test/WireMock.Net.Middleware.Tests/CustomWebApplicationFactory.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,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); | ||
} | ||
} | ||
} |
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,48 @@ | ||
// 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"); | ||
|
||
// Act | ||
var response = await client.SendAsync(request); | ||
|
||
// Assert | ||
response.EnsureSuccessStatusCode(); | ||
var stringResponse = await response.Content.ReadAsStringAsync(); | ||
stringResponse.Should().Be(expectedResponse); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
test/WireMock.Net.Middleware.Tests/WireMock.Net.Middleware.Tests.csproj
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,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> |
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 @@ | ||
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(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
test/WireMock.Net.TestWebApplication/WireMock.Net.TestWebApplication.csproj
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,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> |
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,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Debug", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |