Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaliumhexacyanoferrat committed Dec 12, 2024
1 parent 47912da commit eca75f0
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Modules/IO/Providers/DownloadProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
75 changes: 75 additions & 0 deletions Testing/Acceptance/Modules/ErrorHandling/HtmlErrorMapperTest.cs
Original file line number Diff line number Diff line change
@@ -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;
}

}
2 changes: 2 additions & 0 deletions Testing/Acceptance/Modules/IO/DownloadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 2 additions & 0 deletions Testing/Acceptance/Modules/ServerSentEvents/ProtocolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion Testing/Acceptance/Modules/Webservices/WebserviceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit eca75f0

Please sign in to comment.