Skip to content

Commit

Permalink
Improve code coverage (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaliumhexacyanoferrat authored Dec 18, 2024
1 parent a3ec049 commit 336195f
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Testing/Acceptance/Engine/RoutingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using GenHTTP.Api.Routing;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GenHTTP.Testing.Acceptance.Engine;

[TestClass]
public class RoutingTests
{

[TestMethod]
public void TestComparers()
{
var path = new WebPathPart("%C3%A4%2F");

Assert.IsTrue(path == "ä/");
Assert.IsTrue(path == "%C3%A4%2F");

Assert.IsTrue(path != "ö/");
}

[TestMethod]
public void TestEquality()
{
var one = new WebPathPart("%C3%A4%2F");
var two = new WebPathPart("%C3%A4%2F");

Assert.IsTrue(one.Equals(two));
Assert.IsTrue(two.Equals(one));

var three = new WebPathPart("ä/");

Assert.IsFalse(one.Equals(three));
Assert.IsFalse(one.Equals(new List<int>()));
Assert.IsFalse(one.Equals(null));
}

}
48 changes: 48 additions & 0 deletions Testing/Acceptance/Modules/Controllers/IntegrationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Net;
using GenHTTP.Api.Protocol;
using GenHTTP.Modules.Controllers;
using GenHTTP.Modules.Layouting;
using GenHTTP.Testing.Acceptance.Utilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GenHTTP.Testing.Acceptance.Modules.Controllers;

[TestClass]
public class IntegrationTests
{

#region Supporting data structures

public class TestController
{

[ControllerAction(RequestMethod.Get)]
public string DoWork() => "Work done";

}

#endregion

[TestMethod]
[MultiEngineTest]
public async Task TestInstance(TestEngine engine)
{
var app = Layout.Create()
.Add(Controller.From(new TestController()));

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

using var response = await host.GetResponseAsync("/do-work/");

await response.AssertStatusAsync(HttpStatusCode.OK);

Assert.AreEqual("Work done", await response.GetContentAsync());
}

[TestMethod]
public void TestChaining()
{
Chain.Works(Controller.From<TestController>());
}

}
33 changes: 33 additions & 0 deletions Testing/Acceptance/Modules/Functional/IntegrationTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Net;
using GenHTTP.Modules.Conversion;
using GenHTTP.Modules.Conversion.Formatters;
using GenHTTP.Modules.Functional;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GenHTTP.Testing.Acceptance.Modules.Functional;

[TestClass]
public class IntegrationTest
{

[TestMethod]
[MultiEngineTest]
public async Task TestFormatters(TestEngine engine)
{
var formatting = Formatting.Empty()
.Add<BoolFormatter>();

var api = Inline.Create()
.Any("get-bool", (bool value) => value)
.Formatters(formatting);

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

using var response = await host.GetResponseAsync("/get-bool?value=1");

await response.AssertStatusAsync(HttpStatusCode.OK);

Assert.AreEqual("1", await response.GetContentAsync());
}

}
73 changes: 73 additions & 0 deletions Testing/Acceptance/Modules/Functional/MethodTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System.Net;
using System.Net.Http.Headers;
using GenHTTP.Modules.Functional;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GenHTTP.Testing.Acceptance.Modules.Functional;

[TestClass]
public class MethodTest
{

[TestMethod]
[MultiEngineTest]
public async Task TestAnyMethod(TestEngine engine)
{
var app = Inline.Create()
.Any((List<int> data) => data.Count);

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

foreach (var method in new[]
{
HttpMethod.Post, HttpMethod.Put
})
{
var request = host.GetRequest(method: method);

request.Content = new StringContent("[ 1, 2 ]");
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

using var response = await host.GetResponseAsync(request);

await response.AssertStatusAsync(HttpStatusCode.OK);

Assert.AreEqual("2", await response.GetContentAsync());
}
}

[TestMethod]
[MultiEngineTest]
public async Task TestDelete(TestEngine engine)
{
var app = Inline.Create()
.Delete(() => { });

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

var request = host.GetRequest(method: HttpMethod.Delete);

using var response = await host.GetResponseAsync(request);

await response.AssertStatusAsync(HttpStatusCode.NoContent);
}

[TestMethod]
[MultiEngineTest]
public async Task TestHead(TestEngine engine)
{
var app = Inline.Create()
.Head(() => "42");

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

var request = host.GetRequest(method: HttpMethod.Head);

using var response = await host.GetResponseAsync(request);

await response.AssertStatusAsync(HttpStatusCode.OK);

Assert.AreEqual("2", response.GetContentHeader("Content-Length"));
}

}

0 comments on commit 336195f

Please sign in to comment.