mirror of
https://github.com/Mercantec-GHC/h4-projekt-gruppe-0-sm.git
synced 2025-04-28 08:44:06 +02:00
send connection close
This commit is contained in:
parent
1b8ffa54a0
commit
903ee83a1e
@ -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)
|
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 res;
|
||||||
string_construct(&res);
|
string_construct(&res);
|
||||||
|
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
import { assertEquals, assertMatch } from "jsr:@std/assert";
|
import { assertEquals, assertMatch } from "jsr:@std/assert";
|
||||||
|
|
||||||
import axios from "npm:axios";
|
|
||||||
|
|
||||||
const url = `http://127.0.0.1:8080`;
|
const url = `http://127.0.0.1:8080`;
|
||||||
// const url = `http://10.135.51.114: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";
|
const password = "Merc1234";
|
||||||
|
|
||||||
Deno.test("test", async () => {
|
Deno.test("test", async () => {
|
||||||
const registerRes = await axios.post(
|
const registerRes = await post<{ ok: boolean }>(
|
||||||
`${url}/api/users/register`,
|
`/api/users/register`,
|
||||||
{ name, email, password },
|
{ name, email, password },
|
||||||
{ responseType: "json" },
|
);
|
||||||
)
|
|
||||||
.then((res) => res.data);
|
|
||||||
|
|
||||||
assertEquals(registerRes, { ok: true });
|
assertEquals(registerRes, { ok: true });
|
||||||
|
|
||||||
const loginRes = await axios.post(
|
const loginRes = await post<{
|
||||||
`${url}/api/sessions/login`,
|
ok: true;
|
||||||
|
token: string;
|
||||||
|
}>(
|
||||||
|
"/api/sessions/login",
|
||||||
{ email, password },
|
{ email, password },
|
||||||
{ responseType: "json" },
|
);
|
||||||
).then((res) => res.data);
|
|
||||||
|
|
||||||
assertEquals(loginRes.ok, true);
|
assertEquals(loginRes.ok, true);
|
||||||
assertMatch(loginRes.token, /^[0-9a-zA-Z]+$/);
|
assertMatch(loginRes.token, /^[0-9a-zA-Z]+$/);
|
||||||
const token = loginRes.token;
|
const token = loginRes.token;
|
||||||
|
|
||||||
const sessionUserRes = await axios.get(
|
const sessionUserRes = await get<{
|
||||||
`${url}/api/sessions/user`,
|
ok: boolean;
|
||||||
{ headers: { "Session-Token": token } },
|
user: unknown;
|
||||||
)
|
}>(
|
||||||
.then((res) => res.data)
|
"/api/sessions/user",
|
||||||
.catch((error) => error.response.data);
|
{ "Session-Token": token },
|
||||||
|
);
|
||||||
|
|
||||||
assertEquals(sessionUserRes.ok, true);
|
assertEquals(sessionUserRes.ok, true);
|
||||||
console.log(sessionUserRes.user);
|
console.log(sessionUserRes.user);
|
||||||
|
|
||||||
const logoutRes = await axios.post(
|
const logoutRes = await post<{ ok: boolean }>(
|
||||||
`${url}/api/sessions/logout`,
|
"/api/sessions/logout",
|
||||||
{},
|
{},
|
||||||
{ responseType: "json", headers: { "Session-Token": token } },
|
{ "Session-Token": token },
|
||||||
).then((res) => res.data);
|
);
|
||||||
|
|
||||||
assertEquals(logoutRes, { ok: true });
|
assertEquals(logoutRes, { ok: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function get<Res>(
|
||||||
|
path: string,
|
||||||
|
headers: Record<string, string>,
|
||||||
|
): Promise<Res> {
|
||||||
|
return fetch(`${url}${path}`, { headers })
|
||||||
|
.then((res) => res.json());
|
||||||
|
}
|
||||||
|
|
||||||
|
function post<Res, Req = unknown>(
|
||||||
|
path: string,
|
||||||
|
body: Req,
|
||||||
|
headers: Record<string, string> = {},
|
||||||
|
): Promise<Res> {
|
||||||
|
return fetch(`${url}${path}`, {
|
||||||
|
method: "post",
|
||||||
|
headers: { ...headers, "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
}).then((res) => res.json());
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user