From eca75f0a52c7b05c3c478e60e4598994f518052d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20N=C3=A4geli?= Date: Thu, 12 Dec 2024 14:55:04 +0100 Subject: [PATCH] Add tests --- Modules/IO/Providers/DownloadProvider.cs | 2 +- .../ErrorHandling/HtmlErrorMapperTest.cs | 75 +++++++++++++++++++ .../Acceptance/Modules/IO/DownloadTests.cs | 2 + .../Modules/ServerSentEvents/ProtocolTests.cs | 2 + .../Modules/Webservices/WebserviceTests.cs | 2 +- 5 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 Testing/Acceptance/Modules/ErrorHandling/HtmlErrorMapperTest.cs diff --git a/Modules/IO/Providers/DownloadProvider.cs b/Modules/IO/Providers/DownloadProvider.cs index 2e087d1f..c6d707f5 100644 --- a/Modules/IO/Providers/DownloadProvider.cs +++ b/Modules/IO/Providers/DownloadProvider.cs @@ -42,7 +42,7 @@ public DownloadProvider(IResource resourceProvider, string? fileName, FlexibleCo if (!request.HasType(RequestMethod.Get, RequestMethod.Head)) { - throw new ProviderException(ResponseStatus.MethodNotAllowed, "Only GET requests are allowed by this handler", (b) => b.Header("Allow", "GET, HEAD")); + throw new ProviderException(ResponseStatus.MethodNotAllowed, "Only GET requests are allowed by this handler", (b) => b.Header("Allow", "GET")); } var response = request.Respond() diff --git a/Testing/Acceptance/Modules/ErrorHandling/HtmlErrorMapperTest.cs b/Testing/Acceptance/Modules/ErrorHandling/HtmlErrorMapperTest.cs new file mode 100644 index 00000000..e0112734 --- /dev/null +++ b/Testing/Acceptance/Modules/ErrorHandling/HtmlErrorMapperTest.cs @@ -0,0 +1,75 @@ +using System.Net; +using GenHTTP.Api.Content; +using GenHTTP.Api.Protocol; +using GenHTTP.Modules.ErrorHandling; +using GenHTTP.Modules.Functional; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace GenHTTP.Testing.Acceptance.Modules.ErrorHandling; + +[TestClass] +public class HtmlErrorMapperTest +{ + + [TestMethod] + [MultiEngineTest] + public async Task TestNotFound(TestEngine engine) + { + await using var host = await TestHost.RunAsync(Inline.Create().Add(ErrorHandler.Html()), engine: engine); + + using var response = await host.GetResponseAsync(); + + await response.AssertStatusAsync(HttpStatusCode.NotFound); + } + + [TestMethod] + [MultiEngineTest] + public async Task TestGeneralError(TestEngine engine) + { + var handler = Inline.Create() + .Get(() => DoThrow(new Exception("Oops"))) + .Add(ErrorHandler.Html()); + + await using var host = await TestHost.RunAsync(handler, engine: engine); + + using var response = await host.GetResponseAsync(); + + await response.AssertStatusAsync(HttpStatusCode.InternalServerError); + } + + [TestMethod] + [MultiEngineTest] + public async Task TestProviderError(TestEngine engine) + { + var handler = Inline.Create() + .Get(() => DoThrow(new ProviderException(ResponseStatus.Locked, "Locked up!"))) + .Add(ErrorHandler.Html()); + + await using var host = await TestHost.RunAsync(handler, engine: engine); + + using var response = await host.GetResponseAsync(); + + await response.AssertStatusAsync(HttpStatusCode.Locked); + } + + [TestMethod] + [MultiEngineTest] + public async Task TestNoTraceInProduction(TestEngine engine) + { + var handler = Inline.Create() + .Get(() => DoThrow(new Exception("Oops"))) + .Add(ErrorHandler.Html()); + + await using var host = await TestHost.RunAsync(handler, development: false, engine: engine); + + using var response = await host.GetResponseAsync(); + + await response.AssertStatusAsync(HttpStatusCode.InternalServerError); + } + + private static void DoThrow(Exception e) + { + throw e; + } + +} diff --git a/Testing/Acceptance/Modules/IO/DownloadTests.cs b/Testing/Acceptance/Modules/IO/DownloadTests.cs index e1447a78..513859a1 100644 --- a/Testing/Acceptance/Modules/IO/DownloadTests.cs +++ b/Testing/Acceptance/Modules/IO/DownloadTests.cs @@ -54,6 +54,8 @@ public async Task DownloadsCannotBeModified(TestEngine engine) using var response = await runner.GetResponseAsync(request); await response.AssertStatusAsync(HttpStatusCode.MethodNotAllowed); + + Assert.AreEqual("GET", response.GetContentHeader("Allow")); } [TestMethod] diff --git a/Testing/Acceptance/Modules/ServerSentEvents/ProtocolTests.cs b/Testing/Acceptance/Modules/ServerSentEvents/ProtocolTests.cs index e5284dd9..4c2da352 100644 --- a/Testing/Acceptance/Modules/ServerSentEvents/ProtocolTests.cs +++ b/Testing/Acceptance/Modules/ServerSentEvents/ProtocolTests.cs @@ -105,6 +105,8 @@ public async Task TestGetOnly(TestEngine engine) using var response = await host.GetResponseAsync(request); await response.AssertStatusAsync(HttpStatusCode.MethodNotAllowed); + + Assert.AreEqual("GET", response.GetContentHeader("Allow")); } [TestMethod] diff --git a/Testing/Acceptance/Modules/Webservices/WebserviceTests.cs b/Testing/Acceptance/Modules/Webservices/WebserviceTests.cs index 43ff91ae..6c3d87c4 100644 --- a/Testing/Acceptance/Modules/Webservices/WebserviceTests.cs +++ b/Testing/Acceptance/Modules/Webservices/WebserviceTests.cs @@ -201,7 +201,7 @@ public async Task TestUnsupportedDownloadEnforcesDefault(TestEngine engine) [MultiEngineTest] public async Task TestWrongMethod(TestEngine engine) { - await WithResponse(engine, "entity", HttpMethod.Put, "123", null, null, async r => { await r.AssertStatusAsync(HttpStatusCode.MethodNotAllowed); }); + await WithResponse(engine, "entity", HttpMethod.Put, "123", null, null, async r => { await r.AssertStatusAsync(HttpStatusCode.MethodNotAllowed); Assert.AreEqual("POST", r.GetContentHeader("Allow")); }); } [TestMethod]