Skip to content

Commit

Permalink
Additional coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaliumhexacyanoferrat committed Nov 8, 2024
1 parent 64a303e commit c148161
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 25 deletions.
3 changes: 3 additions & 0 deletions Engine/Kestrel/Hosting/KestrelServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

namespace GenHTTP.Engine.Kestrel.Hosting;

Expand Down Expand Up @@ -83,6 +84,8 @@ public async ValueTask StartAsync()

private void Configure(WebApplicationBuilder builder)
{
builder.Logging.ClearProviders();

builder.WebHost.ConfigureKestrel(options =>
{
foreach (var endpoint in Configuration.EndPoints)
Expand Down
99 changes: 99 additions & 0 deletions Testing/Acceptance/Engine/Kestrel/KestrelTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System.Net;
using GenHTTP.Api.Protocol;
using GenHTTP.Modules.Functional;
using GenHTTP.Modules.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GenHTTP.Testing.Acceptance.Engine.Kestrel;

[TestClass]
public class KestrelTests
{

[TestMethod]
public async Task TestLifecycle()
{
var handler = Content.From(Resource.FromString("Hello Kestrel!")).Build();

await using var host = new TestHost(handler, engine: TestEngine.Kestrel);

await host.StartAsync();

using var response = await host.GetResponseAsync();

await response.AssertStatusAsync(HttpStatusCode.OK);
}

[TestMethod]
public async Task TestHeaders()
{
var app = Inline.Create().Get((IRequest request) =>
{
var count = request.Headers.Count;

Assert.AreEqual(count, request.Headers.Keys.Count());
Assert.AreEqual(count, request.Headers.Values.Count());

Assert.IsTrue(request.Headers.All(kv => kv.Value != string.Empty));

Assert.IsTrue(request.Headers.ContainsKey("Host"));

Assert.IsNotNull(request.Headers["Host"]);

return true;
});

await using var host = await TestHost.RunAsync(app, engine: TestEngine.Kestrel);

using var response = await host.GetResponseAsync();

await response.AssertStatusAsync(HttpStatusCode.OK);
}

[TestMethod]
public async Task TestQuery()
{
var app = Inline.Create().Get((IRequest request) =>
{
var count = request.Query.Count;

Assert.AreEqual(count, request.Query.Keys.Count());
Assert.AreEqual(count, request.Query.Values.Count());

Assert.IsTrue(request.Query.All(kv => kv.Value != string.Empty));

Assert.IsTrue(request.Query.ContainsKey("a"));

Assert.IsNotNull(request.Query["a"]);

return true;
});

await using var host = await TestHost.RunAsync(app, engine: TestEngine.Kestrel);

using var response = await host.GetResponseAsync("/?a=1&b=2");

await response.AssertStatusAsync(HttpStatusCode.OK);
}

[TestMethod]
public async Task TestConnection()
{
var app = Inline.Create().Get((IRequest request) =>
{
Assert.IsNotNull(request.Client.Host);

Assert.AreEqual(ClientProtocol.Http, request.Client.Protocol);

return true;
});

await using var host = await TestHost.RunAsync(app, engine: TestEngine.Kestrel);

using var response = await host.GetResponseAsync();

await response.AssertStatusAsync(HttpStatusCode.OK);
}


}
25 changes: 0 additions & 25 deletions Testing/Acceptance/Engine/KestrelTests.cs

This file was deleted.

38 changes: 38 additions & 0 deletions Testing/Acceptance/Engine/RequestPropertyTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Net;
using GenHTTP.Api.Protocol;
using GenHTTP.Modules.Functional;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GenHTTP.Testing.Acceptance.Engine;

[TestClass]
public class RequestPropertyTest
{

[TestMethod]
[MultiEngineTest]
public async Task TestRequestProperties(TestEngine engine)
{
var app = Inline.Create().Get((IRequest request) =>
{
request.Properties["my"] = "value";

Assert.IsTrue(request.Properties.TryGet<string>("my", out _));

request.Properties.Clear("my");

Assert.IsFalse(request.Properties.TryGet<string>("my", out _));

Assert.ThrowsException<KeyNotFoundException>(() => request.Properties["my"]);

return true;
});

await using var host = await TestHost.RunAsync(app, engine: engine);

using var response = await host.GetResponseAsync();

await response.AssertStatusAsync(HttpStatusCode.OK);
}

}

0 comments on commit c148161

Please sign in to comment.