Skip to content

Commit

Permalink
test: update session test
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 15, 2024
1 parent 22807f2 commit ba275c3
Showing 1 changed file with 39 additions and 36 deletions.
75 changes: 39 additions & 36 deletions test/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import supertest from "supertest";
import { describe, it, expect, beforeEach } from "vitest";
import {
createApp,
createRouter,
toNodeListener,
App,
eventHandler,
Expand All @@ -12,18 +13,23 @@ import {

describe("session", () => {
let app: App;
let router: ReturnType<typeof createRouter>;
let request: ReturnType<typeof supertest>;
let cookie: string | undefined;
let cookie = "";

let sessionIdCtr = 0;
const sessionConfig: SessionConfig = {
name: "h3-test",
password: "1234567123456712345671234567123456712345671234567",
generateId: () => ++sessionIdCtr + "",
};

beforeEach(() => {
app = createApp({ debug: true });
let sessionIdCtr = 0;
const sessionConfig: SessionConfig = {
name: "h3-test",
password: "1234567123456712345671234567123456712345671234567",
generateId: () => ++sessionIdCtr + "",
};
app.use(
router = createRouter({ preemptive: true });
app = createApp({ debug: true }).use(router);
request = supertest(toNodeListener(app));

router.use(
"/",
eventHandler(async (event) => {
const session = await useSession(event, sessionConfig);
Expand All @@ -33,40 +39,37 @@ describe("session", () => {
return { session };
}),
);
request = supertest(toNodeListener(app));
});

describe("useSession", () => {
it("initiates session", async () => {
const result = await request.get("/");
expect(result.headers["set-cookie"]).toHaveLength(1);
cookie = result.headers["set-cookie"][0];
expect(result.body).toMatchObject({
session: { id: "1", data: {} },
});
it("initiates session", async () => {
const result = await request.get("/");
expect(result.headers["set-cookie"]).toHaveLength(1);
cookie = result.headers["set-cookie"][0];
expect(result.body).toMatchObject({
session: { id: "1", data: {} },
});
});

it("gets same session back", async () => {
const result = await request.get("/");
expect(result.body).toMatchObject({
session: { id: "1", data: {} },
});
it("gets same session back", async () => {
const result = await request.get("/").set("Cookie", cookie);
expect(result.body).toMatchObject({
session: { id: "1", data: {} },
});
});

it("set session data", async () => {
const result = await request
.post("/")
.set("Cookie", cookie as string)
.send({ foo: "bar" });
cookie = result.headers["set-cookie"][0];
expect(result.body).toMatchObject({
session: { id: "1", data: { foo: "bar" } },
});
it("set session data", async () => {
const result = await request
.post("/")
.set("Cookie", cookie)
.send({ foo: "bar" });
cookie = result.headers["set-cookie"][0];
expect(result.body).toMatchObject({
session: { id: "1", data: { foo: "bar" } },
});

const result2 = await request.get("/").set("Cookie", cookie as string);
expect(result2.body).toMatchObject({
session: { id: "1", data: { foo: "bar" } },
});
const result2 = await request.get("/").set("Cookie", cookie);
expect(result2.body).toMatchObject({
session: { id: "1", data: { foo: "bar" } },
});
});
});

0 comments on commit ba275c3

Please sign in to comment.