From 336195f7f611dc73b09a1aac72bdf197a46af7af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20N=C3=A4geli?= Date: Wed, 18 Dec 2024 17:44:15 +0100 Subject: [PATCH] Improve code coverage (#598) --- Testing/Acceptance/Engine/RoutingTests.cs | 37 ++++++++++ .../Modules/Controllers/IntegrationTests.cs | 48 ++++++++++++ .../Modules/Functional/IntegrationTest.cs | 33 +++++++++ .../Modules/Functional/MethodTest.cs | 73 +++++++++++++++++++ 4 files changed, 191 insertions(+) create mode 100644 Testing/Acceptance/Engine/RoutingTests.cs create mode 100644 Testing/Acceptance/Modules/Controllers/IntegrationTests.cs create mode 100644 Testing/Acceptance/Modules/Functional/IntegrationTest.cs create mode 100644 Testing/Acceptance/Modules/Functional/MethodTest.cs diff --git a/Testing/Acceptance/Engine/RoutingTests.cs b/Testing/Acceptance/Engine/RoutingTests.cs new file mode 100644 index 00000000..abd14653 --- /dev/null +++ b/Testing/Acceptance/Engine/RoutingTests.cs @@ -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())); + Assert.IsFalse(one.Equals(null)); + } + +} diff --git a/Testing/Acceptance/Modules/Controllers/IntegrationTests.cs b/Testing/Acceptance/Modules/Controllers/IntegrationTests.cs new file mode 100644 index 00000000..4fdd9462 --- /dev/null +++ b/Testing/Acceptance/Modules/Controllers/IntegrationTests.cs @@ -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()); + } + +} diff --git a/Testing/Acceptance/Modules/Functional/IntegrationTest.cs b/Testing/Acceptance/Modules/Functional/IntegrationTest.cs new file mode 100644 index 00000000..1b87c1ae --- /dev/null +++ b/Testing/Acceptance/Modules/Functional/IntegrationTest.cs @@ -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(); + + 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()); + } + +} diff --git a/Testing/Acceptance/Modules/Functional/MethodTest.cs b/Testing/Acceptance/Modules/Functional/MethodTest.cs new file mode 100644 index 00000000..847e5bff --- /dev/null +++ b/Testing/Acceptance/Modules/Functional/MethodTest.cs @@ -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 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")); + } + +}