ASPapaj.NET/test/Femsci.AspapajNet.Test/PapajTest.cs

80 lines
2.3 KiB
C#
Raw Permalink Normal View History

2023-10-21 23:55:06 +02:00
using System.Net;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Hosting;
using Femsci.AspapajNet;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
2023-10-22 00:50:11 +02:00
using System.Net.Mime;
using System.Reflection;
using System.Runtime.CompilerServices;
2023-10-21 23:55:06 +02:00
namespace Femsci.AspapajNet.Test;
public class PapajTest
{
private IHost BuildHost()
{
var builder = WebApplication.CreateBuilder();
builder.Services.AddPapiez();
builder.WebHost.UseTestServer();
var app = builder.Build();
app.UsePapiez();
app.MapGet("/", () => "test");
app.Start();
return app;
}
[Fact]
public async Task Get_PapajAtPapajHeader()
{
using var host = BuildHost();
var client = host.GetTestClient();
client.DefaultRequestHeaders.TryAddWithoutValidation("Papaj", "yes");
var response = await client.GetAsync("/");
Assert.Equal(418, (int)response.StatusCode);
Assert.Equal("image/jpeg", response.Content.Headers.ContentType?.MediaType);
2023-10-22 00:50:11 +02:00
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));
2023-10-21 23:55:06 +02:00
}
[Fact]
public async Task Get_PapajNoHeader()
{
using var host = BuildHost();
var client = host.GetTestClient();
var now = DateTime.Now;
var response = await client.GetAsync("/");
if (now.Hour == 21 && now.Minute == 37)
{
Assert.Equal(418, (int)response.StatusCode);
Assert.Equal("image/jpeg", response.Content.Headers.ContentType?.MediaType);
2023-10-22 00:50:11 +02:00
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));
2023-10-21 23:55:06 +02:00
}
else
{
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("test", await response.Content.ReadAsStringAsync());
}
}
}