diff --git a/backend/src/http_server.c b/backend/src/http_server.c index bcfab31..c122527 100644 --- a/backend/src/http_server.c +++ b/backend/src/http_server.c @@ -163,6 +163,9 @@ void http_ctx_res_headers_set(HttpCtx* ctx, const char* key, const char* value) void http_ctx_respond(HttpCtx* ctx, int status, const char* body) { + // https://httpwg.org/specs/rfc9112.html#persistent.tear-down + http_ctx_res_headers_set(ctx, "Connection", "close"); + String res; string_construct(&res); diff --git a/backend/test/test_authentication.ts b/backend/test/test_authentication.ts index a76e678..e61e92d 100644 --- a/backend/test/test_authentication.ts +++ b/backend/test/test_authentication.ts @@ -1,7 +1,5 @@ import { assertEquals, assertMatch } from "jsr:@std/assert"; -import axios from "npm:axios"; - const url = `http://127.0.0.1:8080`; // const url = `http://10.135.51.114:8080`; @@ -10,40 +8,61 @@ const email = `mash.skp_${Math.floor(Math.random() * 100000)}@edu.mercantec.dk`; const password = "Merc1234"; Deno.test("test", async () => { - const registerRes = await axios.post( - `${url}/api/users/register`, + const registerRes = await post<{ ok: boolean }>( + `/api/users/register`, { name, email, password }, - { responseType: "json" }, - ) - .then((res) => res.data); + ); assertEquals(registerRes, { ok: true }); - const loginRes = await axios.post( - `${url}/api/sessions/login`, + const loginRes = await post<{ + ok: true; + token: string; + }>( + "/api/sessions/login", { email, password }, - { responseType: "json" }, - ).then((res) => res.data); + ); assertEquals(loginRes.ok, true); assertMatch(loginRes.token, /^[0-9a-zA-Z]+$/); const token = loginRes.token; - const sessionUserRes = await axios.get( - `${url}/api/sessions/user`, - { headers: { "Session-Token": token } }, - ) - .then((res) => res.data) - .catch((error) => error.response.data); + const sessionUserRes = await get<{ + ok: boolean; + user: unknown; + }>( + "/api/sessions/user", + { "Session-Token": token }, + ); assertEquals(sessionUserRes.ok, true); console.log(sessionUserRes.user); - const logoutRes = await axios.post( - `${url}/api/sessions/logout`, + const logoutRes = await post<{ ok: boolean }>( + "/api/sessions/logout", {}, - { responseType: "json", headers: { "Session-Token": token } }, - ).then((res) => res.data); + { "Session-Token": token }, + ); assertEquals(logoutRes, { ok: true }); }); + +function get( + path: string, + headers: Record, +): Promise { + return fetch(`${url}${path}`, { headers }) + .then((res) => res.json()); +} + +function post( + path: string, + body: Req, + headers: Record = {}, +): Promise { + return fetch(`${url}${path}`, { + method: "post", + headers: { ...headers, "Content-Type": "application/json" }, + body: JSON.stringify(body), + }).then((res) => res.json()); +} diff --git a/temp.txt b/temp.txt deleted file mode 100644 index defbed0..0000000 --- a/temp.txt +++ /dev/null @@ -1,3 +0,0 @@ - -this is a temp file -