From 98e689bfc5e26900bf9e168e4db160e2220f5abd Mon Sep 17 00:00:00 2001 From: QkoSad Date: Mon, 9 Dec 2024 19:36:12 +0200 Subject: [PATCH] cleaned up tests --- backendCS.Tests/.vscode/launch.json | 26 ---- backendCS.Tests/.vscode/tasks.json | 41 ------ backendCS.Tests/UnitTest1.cs | 192 ++++++++++---------------- backendCS.Tests/backendCs.Test.csproj | 4 + backendCS/Program.cs | 122 +++++++++------- 5 files changed, 144 insertions(+), 241 deletions(-) delete mode 100644 backendCS.Tests/.vscode/launch.json delete mode 100644 backendCS.Tests/.vscode/tasks.json diff --git a/backendCS.Tests/.vscode/launch.json b/backendCS.Tests/.vscode/launch.json deleted file mode 100644 index e395607..0000000 --- a/backendCS.Tests/.vscode/launch.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - { - // Use IntelliSense to find out which attributes exist for C# debugging - // Use hover for the description of the existing attributes - // For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md - "name": ".NET Core Launch (console)", - "type": "coreclr", - "request": "launch", - "preLaunchTask": "build", - // If you have changed target frameworks, make sure to update the program path. - "program": "${workspaceFolder}/bin/Debug/net8.0/backendCs.Test.dll", - "args": [], - "cwd": "${workspaceFolder}", - // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console - "console": "internalConsole", - "stopAtEntry": false - }, - { - "name": ".NET Core Attach", - "type": "coreclr", - "request": "attach" - } - ] -} \ No newline at end of file diff --git a/backendCS.Tests/.vscode/tasks.json b/backendCS.Tests/.vscode/tasks.json deleted file mode 100644 index 10f826c..0000000 --- a/backendCS.Tests/.vscode/tasks.json +++ /dev/null @@ -1,41 +0,0 @@ -{ - "version": "2.0.0", - "tasks": [ - { - "label": "build", - "command": "dotnet", - "type": "process", - "args": [ - "build", - "${workspaceFolder}/backendCs.Test.csproj", - "/property:GenerateFullPaths=true", - "/consoleloggerparameters:NoSummary;ForceNoAlign" - ], - "problemMatcher": "$msCompile" - }, - { - "label": "publish", - "command": "dotnet", - "type": "process", - "args": [ - "publish", - "${workspaceFolder}/backendCs.Test.csproj", - "/property:GenerateFullPaths=true", - "/consoleloggerparameters:NoSummary;ForceNoAlign" - ], - "problemMatcher": "$msCompile" - }, - { - "label": "watch", - "command": "dotnet", - "type": "process", - "args": [ - "watch", - "run", - "--project", - "${workspaceFolder}/backendCs.Test.csproj" - ], - "problemMatcher": "$msCompile" - } - ] -} \ No newline at end of file diff --git a/backendCS.Tests/UnitTest1.cs b/backendCS.Tests/UnitTest1.cs index b76bd10..2bb7fc4 100644 --- a/backendCS.Tests/UnitTest1.cs +++ b/backendCS.Tests/UnitTest1.cs @@ -9,202 +9,150 @@ public class UnitTest1 [TestMethod] public async Task TestMethodReset() { - using (HttpClient client = new HttpClient()) - { - // Make a GET request to a URL - HttpResponseMessage response = await client.GetAsync("http://localhost:5000/api/reset"); + using HttpClient client = new(); + HttpResponseMessage response = await client.GetAsync("http://localhost:5000/api/reset"); - // Ensure we have a successful response - Assert.AreEqual((int)response.StatusCode, 200); - } + Assert.AreEqual((int)response.StatusCode, 200); } [TestMethod] public async Task TestMethodGetall1() { - using (HttpClient client = new HttpClient()) - { - // Make a GET request to a URL - HttpResponseMessage response = await client.GetAsync( - "http://localhost:5000/api/getall?offset=10" - ); + using HttpClient client = new(); + HttpResponseMessage response = await client.GetAsync( + "http://localhost:5000/api/getall?offset=10" + ); - // Ensure we have a successful response - Assert.AreEqual((int)response.StatusCode, 200); - } + Assert.AreEqual((int)response.StatusCode, 200); } [TestMethod] public async Task TestMethodGetall2() { - using (HttpClient client = new HttpClient()) - { - // Make a GET request to a URL - HttpResponseMessage response = await client.GetAsync( - "http://localhost:5000/api/getall?offset=" - ); + using HttpClient client = new(); + HttpResponseMessage response = await client.GetAsync( + "http://localhost:5000/api/getall?offset=" + ); - // Ensure we have a successful response - Assert.AreEqual((int)response.StatusCode, 400); - } + Assert.AreEqual((int)response.StatusCode, 400); } [TestMethod] public async Task TestMethodGetall3() { - using (HttpClient client = new HttpClient()) - { - // Make a GET request to a URL - HttpResponseMessage response = await client.GetAsync( - "http://localhost:5000/api/getall?offset=10&from=2020-01-01&to=2024-01-01&orderby=time&order=true" - ); + using HttpClient client = new(); + HttpResponseMessage response = await client.GetAsync( + "http://localhost:5000/api/getall?offset=10&from=2020-01-01&to=2024-01-01&orderby=time&order=true" + ); - // Ensure we have a successful response - Assert.AreEqual((int)response.StatusCode, 200); - } + Assert.AreEqual((int)response.StatusCode, 200); } [TestMethod] public async Task TestMethodGettopten1() { - using (HttpClient client = new HttpClient()) - { - // Make a GET request to a URL - HttpResponseMessage response = await client.GetAsync( - "http://localhost:5000/api/gettopten?from=2000-01-01&to=2024-01-01&filterby=project" - ); + using HttpClient client = new(); + HttpResponseMessage response = await client.GetAsync( + "http://localhost:5000/api/gettopten?from=2000-01-01&to=2024-01-01&filterby=project" + ); - // Ensure we have a successful response - Assert.AreEqual((int)response.StatusCode, 200); - } + Assert.AreEqual((int)response.StatusCode, 200); } [TestMethod] public async Task TestMethodGettopten2() { - using (HttpClient client = new HttpClient()) - { - // Make a GET request to a URL - HttpResponseMessage response = await client.GetAsync( - "http://localhost:5000/api/gettopten?from=2000-01-01&to=2024-01-01&filterby=user" - ); + using HttpClient client = new(); + HttpResponseMessage response = await client.GetAsync( + "http://localhost:5000/api/gettopten?from=2000-01-01&to=2024-01-01&filterby=user" + ); - // Ensure we have a successful response - Assert.AreEqual((int)response.StatusCode, 200); - } + Assert.AreEqual((int)response.StatusCode, 200); } [TestMethod] public async Task TestMethodGettopten3() { - using (HttpClient client = new HttpClient()) - { - // Make a GET request to a URL - HttpResponseMessage response = await client.GetAsync( - "http://localhost:5000/api/gettopten?to=2024-01-01&filterby=project" - ); + using HttpClient client = new(); + HttpResponseMessage response = await client.GetAsync( + "http://localhost:5000/api/gettopten?to=2024-01-01&filterby=project" + ); - // Ensure we have a successful response - Assert.AreEqual((int)response.StatusCode, 400); - } + Assert.AreEqual((int)response.StatusCode, 400); } [TestMethod] public async Task TestMethodGettopten4() { - using (HttpClient client = new HttpClient()) - { - // Make a GET request to a URL - HttpResponseMessage response = await client.GetAsync( - "http://localhost:5000/api/gettopten?from=2000-01-01&filterby=project" - ); + using HttpClient client = new(); + HttpResponseMessage response = await client.GetAsync( + "http://localhost:5000/api/gettopten?from=2000-01-01&filterby=project" + ); - // Ensure we have a successful response - Assert.AreEqual((int)response.StatusCode, 400); - } + Assert.AreEqual((int)response.StatusCode, 400); } [TestMethod] public async Task TestMethodGettopten5() { - using (HttpClient client = new HttpClient()) - { - // Make a GET request to a URL - HttpResponseMessage response = await client.GetAsync( - "http://localhost:5000/api/gettopten?from=2000-01-01&to=2024-01-01" - ); + using HttpClient client = new(); + HttpResponseMessage response = await client.GetAsync( + "http://localhost:5000/api/gettopten?from=2000-01-01&to=2024-01-01" + ); - // Ensure we have a successful response - Assert.AreEqual((int)response.StatusCode, 400); - } + Assert.AreEqual((int)response.StatusCode, 400); } [TestMethod] public async Task TestMethodGetuser1() { - using (HttpClient client = new HttpClient()) - { - // Make a GET request to a URL - HttpResponseMessage response = await client.GetAsync( - "http://localhost:5000/api/getuser?userid=1" - ); + using HttpClient client = new(); + HttpResponseMessage response = await client.GetAsync( + "http://localhost:5000/api/getuser?userid=1" + ); - // Ensure we have a successful response - Assert.AreEqual((int)response.StatusCode, 200); - } + Assert.AreEqual((int)response.StatusCode, 200); } [TestMethod] public async Task TestMethodGetuser2() { - using (HttpClient client = new HttpClient()) - { - // Make a GET request to a URL - HttpResponseMessage response = await client.GetAsync( - "http://localhost:5000/api/getuser" - ); + using HttpClient client = new(); + HttpResponseMessage response = await client.GetAsync("http://localhost:5000/api/getuser"); - // Ensure we have a successful response - Assert.AreEqual((int)response.StatusCode, 400); - } + Assert.AreEqual((int)response.StatusCode, 400); } [TestMethod] public async Task TestMethodRegister1() { - using (HttpClient client = new()) - { - // Make a GET request to a URL - var jsonData = - "{ \"f_name\": \"donna\", \"l_name\": \"cow\", \"mail\": \"tombo@mail.com\", \"password\": \"1234567890\" }"; - var content = new StringContent(jsonData, Encoding.UTF8, "application/json"); + using HttpClient client = new(); + // Make a GET request to a URL + var jsonData = + "{ \"f_name\": \"donna\", \"l_name\": \"cow\", \"mail\": \"tombo@mail.com\", \"password\": \"1234567890\" }"; + var content = new StringContent(jsonData, Encoding.UTF8, "application/json"); - HttpResponseMessage response = await client.PostAsync( - "http://localhost:5000/api/register", - content - ); + HttpResponseMessage response = await client.PostAsync( + "http://localhost:5000/api/register", + content + ); - // Ensure we have a successful response - Assert.AreEqual((int)response.StatusCode, 200); - } + Assert.AreEqual((int)response.StatusCode, 200); } [TestMethod] public async Task TestMethodLogin() { - using (HttpClient client = new()) - { - // Make a GET request to a URL - var jsonData = "{ \"mail\": \"tombo@mail.com\", \"password\": \"1234567890\" }"; - var content = new StringContent(jsonData, Encoding.UTF8, "application/json"); + using HttpClient client = new(); + // Make a GET request to a URL + var jsonData = "{ \"mail\": \"tombo@mail.com\", \"password\": \"1234567890\" }"; + var content = new StringContent(jsonData, Encoding.UTF8, "application/json"); - HttpResponseMessage response = await client.PostAsync( - "http://localhost:5000/api/login", - content - ); + HttpResponseMessage response = await client.PostAsync( + "http://localhost:5000/api/login", + content + ); - // Ensure we have a successful response - Assert.AreEqual((int)response.StatusCode, 200); - } + Assert.AreEqual((int)response.StatusCode, 200); } } diff --git a/backendCS.Tests/backendCs.Test.csproj b/backendCS.Tests/backendCs.Test.csproj index e79c322..30aa586 100644 --- a/backendCS.Tests/backendCs.Test.csproj +++ b/backendCS.Tests/backendCs.Test.csproj @@ -16,4 +16,8 @@ + + + + diff --git a/backendCS/Program.cs b/backendCS/Program.cs index 9fda554..77d622f 100644 --- a/backendCS/Program.cs +++ b/backendCS/Program.cs @@ -5,17 +5,6 @@ namespace TimelogBackend; class Program { - static void HandleMissingPath(HttpListenerResponse response) - { - response.StatusCode = 404; - string errorMessage = "Not Found"; - byte[] buffer = Encoding.UTF8.GetBytes(errorMessage); - response.ContentType = "text/plain"; - response.ContentLength64 = buffer.Length; - response.OutputStream.Write(buffer, 0, buffer.Length); - response.OutputStream.Write(buffer, 0, buffer.Length); - } - static void Main() { // create server @@ -51,49 +40,10 @@ class Program switch (request.HttpMethod) { case "GET": - switch (uri) - { - case "/api/reset": - Reset.HandleRequest(response); - break; - case "/api/getall": - Getall.HandleRequest(request, response); - break; - case "/api/gettopten": - Gettopten.HandleRequest(request, response); - break; - case "/api/getuser": - Getuser.HandleRequest(request, response); - break; - case "/api/createp": - CreateProcedure.HandleRequest(response); - break; - default: - HandleMissingPath(response); - break; - } + HandleGet(uri, request, response); break; case "POST": - if (request.HasEntityBody) - switch (uri) - { - case "/api/register": - Register.HandleRequest(request, response); - break; - case "/api/login": - Login.HandleRequest(request, response); - break; - case "/api/createlog": - CreateLog.HandleRequest(request, response); - break; - default: - HandleMissingPath(response); - break; - } - else - { - HandleMissingPath(response); - } + HandlePost(uri, request, response); break; default: HandleMissingPath(response); @@ -101,4 +51,72 @@ class Program } } } + + private static void HandlePost( + string uri, + HttpListenerRequest request, + HttpListenerResponse response + ) + { + if (request.HasEntityBody) + switch (uri) + { + case "/api/register": + Register.HandleRequest(request, response); + break; + case "/api/login": + Login.HandleRequest(request, response); + break; + case "/api/createlog": + CreateLog.HandleRequest(request, response); + break; + default: + HandleMissingPath(response); + break; + } + else + { + HandleMissingPath(response); + } + } + + private static void HandleGet( + string uri, + HttpListenerRequest request, + HttpListenerResponse response + ) + { + switch (uri) + { + case "/api/reset": + Reset.HandleRequest(response); + break; + case "/api/getall": + Getall.HandleRequest(request, response); + break; + case "/api/gettopten": + Gettopten.HandleRequest(request, response); + break; + case "/api/getuser": + Getuser.HandleRequest(request, response); + break; + case "/api/createp": + CreateProcedure.HandleRequest(response); + break; + default: + HandleMissingPath(response); + break; + } + } + + private static void HandleMissingPath(HttpListenerResponse response) + { + response.StatusCode = 404; + string errorMessage = "Not Found"; + byte[] buffer = Encoding.UTF8.GetBytes(errorMessage); + response.ContentType = "text/plain"; + response.ContentLength64 = buffer.Length; + response.OutputStream.Write(buffer, 0, buffer.Length); + response.OutputStream.Write(buffer, 0, buffer.Length); + } }