This commit is contained in:
femsci 2023-10-22 00:50:11 +02:00
parent fef18d40ba
commit f29cd5689a
Signed by: femsci
GPG key ID: 08F7911F0E650C67
3 changed files with 32 additions and 1 deletions

View file

@ -13,7 +13,8 @@ public class AspapajMiddleware : IMiddleware
ctx.Response.StatusCode = 418; ctx.Response.StatusCode = 418;
ctx.Response.ContentType = "image/jpeg"; ctx.Response.ContentType = "image/jpeg";
await ctx.Response.StartAsync(); await ctx.Response.StartAsync();
//await Assembly.GetExecutingAssembly()!.GetFile("papiez.jpg")!.CopyToAsync(ctx.Response.Body); using var fs = Assembly.GetExecutingAssembly()!.GetManifestResourceStream("Femsci.AspapajNet.papiez.jpg")!;
await fs.CopyToAsync(ctx.Response.Body);
await ctx.Response.CompleteAsync(); await ctx.Response.CompleteAsync();
return; return;
} }

View file

@ -4,10 +4,25 @@
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<PackageId>Femsci.AspapajNet</PackageId>
<Version>1.0.1</Version>
<Authors>femsci</Authors>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" /> <FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Content Remove="papiez.jpg" />
</ItemGroup>
<ItemGroup>
<Content Include="papiez.jpg" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="papiez.jpg" />
</ItemGroup>
</Project> </Project>

View file

@ -7,6 +7,9 @@ using Microsoft.Extensions.Hosting;
using Femsci.AspapajNet; using Femsci.AspapajNet;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using System.Net.Mime;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace Femsci.AspapajNet.Test; namespace Femsci.AspapajNet.Test;
@ -38,6 +41,12 @@ public class PapajTest
Assert.Equal(418, (int)response.StatusCode); Assert.Equal(418, (int)response.StatusCode);
Assert.Equal("image/jpeg", response.Content.Headers.ContentType?.MediaType); Assert.Equal("image/jpeg", response.Content.Headers.ContentType?.MediaType);
var bytes = await response.Content.ReadAsByteArrayAsync();
var orig = new MemoryStream();
await Assembly.GetAssembly(typeof(AspapajMiddleware))!.GetManifestResourceStream("Femsci.AspapajNet.papiez.jpg")!.CopyToAsync(orig);
Assert.True(orig.ToArray().SequenceEqual(bytes));
} }
[Fact] [Fact]
@ -53,6 +62,12 @@ public class PapajTest
{ {
Assert.Equal(418, (int)response.StatusCode); Assert.Equal(418, (int)response.StatusCode);
Assert.Equal("image/jpeg", response.Content.Headers.ContentType?.MediaType); Assert.Equal("image/jpeg", response.Content.Headers.ContentType?.MediaType);
var bytes = await response.Content.ReadAsByteArrayAsync();
var orig = new MemoryStream();
await Assembly.GetAssembly(typeof(AspapajMiddleware))!.GetManifestResourceStream("Femsci.AspapajNet.papiez.jpg")!.CopyToAsync(orig);
Assert.True(orig.ToArray().SequenceEqual(bytes));
} }
else else
{ {