-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ability to use IFormFile or IFormFileCollection in HTTP middleware. C…
…loses GH-926
- Loading branch information
1 parent
466d1b9
commit b6bbb42
Showing
3 changed files
with
120 additions
and
5 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
src/Http/Wolverine.Http.Tests/Bugs/Bug_926_form_file_used_by_middleware.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,86 @@ | ||
using Alba; | ||
using IntegrationTests; | ||
using Marten; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.HttpResults; | ||
using Microsoft.Extensions.Options; | ||
using Wolverine.Attributes; | ||
using Wolverine.Marten; | ||
|
||
namespace Wolverine.Http.Tests.Bugs; | ||
|
||
public class Bug_926_form_file_used_by_middleware | ||
{ | ||
[Fact] | ||
public async Task should_only_codegen_the_form_file_once() | ||
{ | ||
var builder = WebApplication.CreateBuilder([]); | ||
|
||
builder.Services.AddMarten(opts => | ||
{ | ||
// Establish the connection string to your Marten database | ||
opts.Connection(Servers.PostgresConnectionString); | ||
opts.DatabaseSchemaName = "myapp"; | ||
|
||
// Specify that we want to use STJ as our serializer | ||
opts.UseSystemTextJsonForSerialization(); | ||
|
||
opts.Policies.AllDocumentsSoftDeleted(); | ||
opts.Policies.AllDocumentsAreMultiTenanted(); | ||
|
||
opts.DisableNpgsqlLogging = true; | ||
}).IntegrateWithWolverine(); | ||
|
||
builder.Host.UseWolverine(opts => | ||
{ | ||
opts.Discovery.DisableConventionalDiscovery(); | ||
opts.ApplicationAssembly = GetType().Assembly; | ||
}); | ||
|
||
// This is using Alba, which uses WebApplicationFactory under the covers | ||
await using var host = await AlbaHost.For(builder, app => | ||
{ | ||
app.MapWolverineEndpoints(); | ||
}); | ||
|
||
await host.Scenario(x => | ||
{ | ||
x.Post.Url("/api/files/upload").ContentType("application/x-www-form-urlencoded"); | ||
|
||
|
||
}); | ||
} | ||
} | ||
|
||
public static class FileUploadEndpoint | ||
{ | ||
[Middleware( | ||
typeof(FileLengthValidationMiddleware), | ||
typeof(FileExtensionValidationMiddleware) | ||
)] | ||
[WolverinePost("/api/files/upload")] | ||
public static async Task<Ok> HandleAsync(IFormFile file) | ||
{ | ||
// todo, generate filename, write mapping to table | ||
// todo, create sideeffect to write file with new filename | ||
|
||
return TypedResults.Ok(); // return new filename | ||
} | ||
} | ||
|
||
public static class FileLengthValidationMiddleware | ||
{ | ||
public static void Before(IFormFile file) | ||
{ | ||
// todo, return ProblemDetail if validation fails | ||
} | ||
} | ||
|
||
public static class FileExtensionValidationMiddleware | ||
{ | ||
public static void Before(IFormFile file) | ||
{ | ||
// todo, return ProblemDetail if validation fails | ||
} | ||
} |
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