-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add proper DI, FinallyThrow and improve the documentation
- Loading branch information
Showing
43 changed files
with
1,032 additions
and
46 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
32 changes: 32 additions & 0 deletions
32
src/PipelineNet.ServiceProvider.AspNetCore.Tests/Controllers/MyController.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,32 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using PipelineNet.Pipelines; | ||
using PipelineNet.ServiceProvider.Pipelines.Factories; | ||
using static PipelineNet.ServiceProvider.AspNetCore.Tests.ServiceCollectionExtensionsTests; | ||
|
||
namespace PipelineNet.ServiceProvider.AspNetCore.Tests.Controllers | ||
{ | ||
[ApiController] | ||
[Route("[controller]")] | ||
public class MyController : ControllerBase | ||
{ | ||
private readonly IAsyncPipelineFactory<Bitmap> _pipelineFactory; | ||
|
||
public MyController(IAsyncPipelineFactory<Bitmap> pipelineFactory) | ||
{ | ||
_pipelineFactory = pipelineFactory; | ||
} | ||
|
||
[HttpPost] | ||
public async Task DoSomething() | ||
{ | ||
IAsyncPipeline<Bitmap> pipeline = _pipelineFactory.Create() | ||
.Add<RoudCornersAsyncMiddleware>() | ||
.Add<AddTransparencyAsyncMiddleware>() | ||
.Add<AddWatermarkAsyncMiddleware>(); | ||
|
||
Bitmap image = new Bitmap(); | ||
|
||
await pipeline.Execute(image); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...eNet.ServiceProvider.AspNetCore.Tests/PipelineNet.ServiceProvider.AspNetCore.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,30 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.7" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="xunit" Version="2.5.3" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\PipelineNet.ServiceProvider.AspNetCore\PipelineNet.ServiceProvider.AspNetCore.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
</Project> |
29 changes: 29 additions & 0 deletions
29
src/PipelineNet.ServiceProvider.AspNetCore.Tests/Program.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,29 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using PipelineNet.ServiceProvider.AspNetCore; | ||
using static PipelineNet.ServiceProvider.AspNetCore.Tests.ServiceCollectionExtensionsTests; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add services to the container. | ||
|
||
builder.Services.AddLogging(builder => | ||
builder.Services.AddSingleton<ILoggerProvider, TestOutputHelperLoggerProvider>()); | ||
|
||
builder.Services.AddControllers() | ||
.AddPipelineNet(typeof(RoudCornersAsyncMiddleware).Assembly); | ||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.UseAuthorization(); | ||
|
||
app.MapControllers(); | ||
|
||
app.Run(); | ||
|
||
public partial class Program { } |
138 changes: 138 additions & 0 deletions
138
src/PipelineNet.ServiceProvider.AspNetCore.Tests/ServiceCollectionExtensionsTests.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,138 @@ | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using PipelineNet.Middleware; | ||
using Xunit.Abstractions; | ||
|
||
namespace PipelineNet.ServiceProvider.AspNetCore.Tests | ||
{ | ||
public class ServiceCollectionExtensionsTests | ||
: IClassFixture<WebApplicationFactory<Program>> | ||
{ | ||
#region Service defintions | ||
public class Bitmap | ||
{ | ||
} | ||
|
||
public class TestOutputHelperLogger : ILogger | ||
{ | ||
private readonly ITestOutputHelper _output; | ||
|
||
public TestOutputHelperLogger(ITestOutputHelper output) | ||
{ | ||
_output = output; | ||
} | ||
|
||
public IDisposable? BeginScope<TState>(TState state) | ||
where TState : notnull => | ||
new NullScope(); | ||
|
||
public bool IsEnabled(LogLevel logLevel) => true; | ||
|
||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) => | ||
_output.WriteLine($"{logLevel}:{eventId}:{formatter(state, exception)}"); | ||
|
||
private class NullScope : IDisposable | ||
{ | ||
public void Dispose() | ||
{ | ||
} | ||
} | ||
} | ||
|
||
public class TestOutputHelperLoggerProvider : ILoggerProvider | ||
{ | ||
private readonly ITestOutputHelper _output; | ||
|
||
public TestOutputHelperLoggerProvider(ITestOutputHelper output) | ||
{ | ||
_output = output; | ||
} | ||
|
||
public ILogger CreateLogger(string categoryName) => | ||
new TestOutputHelperLogger(_output); | ||
|
||
public void Dispose() | ||
{ | ||
} | ||
} | ||
#endregion | ||
|
||
#region Middleware definitions | ||
public class RoudCornersAsyncMiddleware : IAsyncMiddleware<Bitmap> | ||
{ | ||
private readonly ILogger<RoudCornersAsyncMiddleware> _logger; | ||
|
||
// The following constructor arguments will be provided by the IServiceProvider | ||
public RoudCornersAsyncMiddleware(ILogger<RoudCornersAsyncMiddleware> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public async Task Run(Bitmap parameter, Func<Bitmap, Task> next) | ||
{ | ||
_logger.LogInformation("Running RoudCornersAsyncMiddleware."); | ||
// Handle somehow | ||
await next(parameter); | ||
} | ||
} | ||
|
||
public class AddTransparencyAsyncMiddleware : IAsyncMiddleware<Bitmap> | ||
{ | ||
private readonly ILogger<AddTransparencyAsyncMiddleware> _logger; | ||
|
||
public AddTransparencyAsyncMiddleware(ILogger<AddTransparencyAsyncMiddleware> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public async Task Run(Bitmap parameter, Func<Bitmap, Task> next) | ||
{ | ||
_logger.LogInformation("Running AddTransparencyAsyncMiddleware."); | ||
await next(parameter); | ||
} | ||
} | ||
|
||
public class AddWatermarkAsyncMiddleware : IAsyncMiddleware<Bitmap> | ||
{ | ||
private readonly ILogger<AddWatermarkAsyncMiddleware> _logger; | ||
|
||
public AddWatermarkAsyncMiddleware(ILogger<AddWatermarkAsyncMiddleware> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public async Task Run(Bitmap parameter, Func<Bitmap, Task> next) | ||
{ | ||
_logger.LogInformation("Running AddWatermarkAsyncMiddleware."); | ||
await next(parameter); | ||
} | ||
} | ||
#endregion | ||
|
||
private readonly WebApplicationFactory<Program> _factory; | ||
private readonly ITestOutputHelper _output; | ||
|
||
public ServiceCollectionExtensionsTests(WebApplicationFactory<Program> factory, ITestOutputHelper output) | ||
{ | ||
_factory = factory; | ||
_output = output; | ||
} | ||
|
||
[Fact] | ||
public async Task AcceptanceTest() | ||
{ | ||
var client = _factory.WithWebHostBuilder(builder => | ||
{ | ||
builder.ConfigureTestServices(services => | ||
{ | ||
services.AddSingleton(_output); // Add ITestOutputHelper for TestOutputHelperLoggerProvider. | ||
}); | ||
}) | ||
.CreateClient(); | ||
|
||
await client.PostAsync("/My", null); | ||
} | ||
} | ||
} |
Oops, something went wrong.