cleaned up tests

This commit is contained in:
QkoSad
2024-12-09 19:36:12 +02:00
parent 1054889c65
commit 98e689bfc5
5 changed files with 144 additions and 241 deletions
-26
View File
@@ -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"
}
]
}
-41
View File
@@ -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"
}
]
}
+27 -79
View File
@@ -9,171 +9,124 @@ public class UnitTest1
[TestMethod] [TestMethod]
public async Task TestMethodReset() public async Task TestMethodReset()
{ {
using (HttpClient client = new HttpClient()) using HttpClient client = new();
{
// Make a GET request to a URL
HttpResponseMessage response = await client.GetAsync("http://localhost:5000/api/reset"); HttpResponseMessage response = await client.GetAsync("http://localhost:5000/api/reset");
// Ensure we have a successful response Assert.AreEqual((int)response.StatusCode, 200);
Assert.AreEqual<Int32>((int)response.StatusCode, 200);
}
} }
[TestMethod] [TestMethod]
public async Task TestMethodGetall1() public async Task TestMethodGetall1()
{ {
using (HttpClient client = new HttpClient()) using HttpClient client = new();
{
// Make a GET request to a URL
HttpResponseMessage response = await client.GetAsync( HttpResponseMessage response = await client.GetAsync(
"http://localhost:5000/api/getall?offset=10" "http://localhost:5000/api/getall?offset=10"
); );
// Ensure we have a successful response Assert.AreEqual((int)response.StatusCode, 200);
Assert.AreEqual<Int32>((int)response.StatusCode, 200);
}
} }
[TestMethod] [TestMethod]
public async Task TestMethodGetall2() public async Task TestMethodGetall2()
{ {
using (HttpClient client = new HttpClient()) using HttpClient client = new();
{
// Make a GET request to a URL
HttpResponseMessage response = await client.GetAsync( HttpResponseMessage response = await client.GetAsync(
"http://localhost:5000/api/getall?offset=" "http://localhost:5000/api/getall?offset="
); );
// Ensure we have a successful response Assert.AreEqual((int)response.StatusCode, 400);
Assert.AreEqual<Int32>((int)response.StatusCode, 400);
}
} }
[TestMethod] [TestMethod]
public async Task TestMethodGetall3() public async Task TestMethodGetall3()
{ {
using (HttpClient client = new HttpClient()) using HttpClient client = new();
{
// Make a GET request to a URL
HttpResponseMessage response = await client.GetAsync( HttpResponseMessage response = await client.GetAsync(
"http://localhost:5000/api/getall?offset=10&from=2020-01-01&to=2024-01-01&orderby=time&order=true" "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<Int32>((int)response.StatusCode, 200);
}
} }
[TestMethod] [TestMethod]
public async Task TestMethodGettopten1() public async Task TestMethodGettopten1()
{ {
using (HttpClient client = new HttpClient()) using HttpClient client = new();
{
// Make a GET request to a URL
HttpResponseMessage response = await client.GetAsync( HttpResponseMessage response = await client.GetAsync(
"http://localhost:5000/api/gettopten?from=2000-01-01&to=2024-01-01&filterby=project" "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<Int32>((int)response.StatusCode, 200);
}
} }
[TestMethod] [TestMethod]
public async Task TestMethodGettopten2() public async Task TestMethodGettopten2()
{ {
using (HttpClient client = new HttpClient()) using HttpClient client = new();
{
// Make a GET request to a URL
HttpResponseMessage response = await client.GetAsync( HttpResponseMessage response = await client.GetAsync(
"http://localhost:5000/api/gettopten?from=2000-01-01&to=2024-01-01&filterby=user" "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<Int32>((int)response.StatusCode, 200);
}
} }
[TestMethod] [TestMethod]
public async Task TestMethodGettopten3() public async Task TestMethodGettopten3()
{ {
using (HttpClient client = new HttpClient()) using HttpClient client = new();
{
// Make a GET request to a URL
HttpResponseMessage response = await client.GetAsync( HttpResponseMessage response = await client.GetAsync(
"http://localhost:5000/api/gettopten?to=2024-01-01&filterby=project" "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<Int32>((int)response.StatusCode, 400);
}
} }
[TestMethod] [TestMethod]
public async Task TestMethodGettopten4() public async Task TestMethodGettopten4()
{ {
using (HttpClient client = new HttpClient()) using HttpClient client = new();
{
// Make a GET request to a URL
HttpResponseMessage response = await client.GetAsync( HttpResponseMessage response = await client.GetAsync(
"http://localhost:5000/api/gettopten?from=2000-01-01&filterby=project" "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<Int32>((int)response.StatusCode, 400);
}
} }
[TestMethod] [TestMethod]
public async Task TestMethodGettopten5() public async Task TestMethodGettopten5()
{ {
using (HttpClient client = new HttpClient()) using HttpClient client = new();
{
// Make a GET request to a URL
HttpResponseMessage response = await client.GetAsync( HttpResponseMessage response = await client.GetAsync(
"http://localhost:5000/api/gettopten?from=2000-01-01&to=2024-01-01" "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<Int32>((int)response.StatusCode, 400);
}
} }
[TestMethod] [TestMethod]
public async Task TestMethodGetuser1() public async Task TestMethodGetuser1()
{ {
using (HttpClient client = new HttpClient()) using HttpClient client = new();
{
// Make a GET request to a URL
HttpResponseMessage response = await client.GetAsync( HttpResponseMessage response = await client.GetAsync(
"http://localhost:5000/api/getuser?userid=1" "http://localhost:5000/api/getuser?userid=1"
); );
// Ensure we have a successful response Assert.AreEqual((int)response.StatusCode, 200);
Assert.AreEqual<Int32>((int)response.StatusCode, 200);
}
} }
[TestMethod] [TestMethod]
public async Task TestMethodGetuser2() public async Task TestMethodGetuser2()
{ {
using (HttpClient client = new HttpClient()) using HttpClient client = new();
{ HttpResponseMessage response = await client.GetAsync("http://localhost:5000/api/getuser");
// Make a GET request to a URL
HttpResponseMessage response = await client.GetAsync(
"http://localhost:5000/api/getuser"
);
// Ensure we have a successful response Assert.AreEqual((int)response.StatusCode, 400);
Assert.AreEqual<Int32>((int)response.StatusCode, 400);
}
} }
[TestMethod] [TestMethod]
public async Task TestMethodRegister1() public async Task TestMethodRegister1()
{ {
using (HttpClient client = new()) using HttpClient client = new();
{
// Make a GET request to a URL // Make a GET request to a URL
var jsonData = var jsonData =
"{ \"f_name\": \"donna\", \"l_name\": \"cow\", \"mail\": \"tombo@mail.com\", \"password\": \"1234567890\" }"; "{ \"f_name\": \"donna\", \"l_name\": \"cow\", \"mail\": \"tombo@mail.com\", \"password\": \"1234567890\" }";
@@ -184,16 +137,13 @@ public class UnitTest1
content content
); );
// Ensure we have a successful response Assert.AreEqual((int)response.StatusCode, 200);
Assert.AreEqual<Int32>((int)response.StatusCode, 200);
}
} }
[TestMethod] [TestMethod]
public async Task TestMethodLogin() public async Task TestMethodLogin()
{ {
using (HttpClient client = new()) using HttpClient client = new();
{
// Make a GET request to a URL // Make a GET request to a URL
var jsonData = "{ \"mail\": \"tombo@mail.com\", \"password\": \"1234567890\" }"; var jsonData = "{ \"mail\": \"tombo@mail.com\", \"password\": \"1234567890\" }";
var content = new StringContent(jsonData, Encoding.UTF8, "application/json"); var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
@@ -203,8 +153,6 @@ public class UnitTest1
content content
); );
// Ensure we have a successful response Assert.AreEqual((int)response.StatusCode, 200);
Assert.AreEqual<Int32>((int)response.StatusCode, 200);
}
} }
} }
+4
View File
@@ -16,4 +16,8 @@
<PackageReference Include="coverlet.collector" Version="6.0.0" /> <PackageReference Include="coverlet.collector" Version="6.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\backendCS\TimelogBackend.csproj" />
</ItemGroup>
</Project> </Project>
+56 -38
View File
@@ -5,17 +5,6 @@ namespace TimelogBackend;
class Program 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() static void Main()
{ {
// create server // create server
@@ -51,6 +40,52 @@ class Program
switch (request.HttpMethod) switch (request.HttpMethod)
{ {
case "GET": case "GET":
HandleGet(uri, request, response);
break;
case "POST":
HandlePost(uri, request, response);
break;
default:
HandleMissingPath(response);
break;
}
}
}
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) switch (uri)
{ {
case "/api/reset": case "/api/reset":
@@ -72,33 +107,16 @@ class Program
HandleMissingPath(response); HandleMissingPath(response);
break; break;
} }
break; }
case "POST":
if (request.HasEntityBody) private static void HandleMissingPath(HttpListenerResponse response)
switch (uri)
{ {
case "/api/register": response.StatusCode = 404;
Register.HandleRequest(request, response); string errorMessage = "Not Found";
break; byte[] buffer = Encoding.UTF8.GetBytes(errorMessage);
case "/api/login": response.ContentType = "text/plain";
Login.HandleRequest(request, response); response.ContentLength64 = buffer.Length;
break; response.OutputStream.Write(buffer, 0, buffer.Length);
case "/api/createlog": response.OutputStream.Write(buffer, 0, buffer.Length);
CreateLog.HandleRequest(request, response);
break;
default:
HandleMissingPath(response);
break;
}
else
{
HandleMissingPath(response);
}
break;
default:
HandleMissingPath(response);
break;
}
}
} }
} }