Skip to content
This repository has been archived by the owner on Feb 10, 2025. It is now read-only.

Commit

Permalink
wip tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Jan 28, 2025
1 parent a9eb6b2 commit 093a380
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 126 deletions.
1 change: 1 addition & 0 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"astro": "^5.1.6",
"astro-scripts": "workspace:*",
"cheerio": "1.0.0",
"devalue": "^5.1.1",
"express": "^4.21.2",
"node-mocks-http": "^1.16.2"
},
Expand Down
11 changes: 0 additions & 11 deletions packages/node/test/fixtures/session/src/pages/index.astro

This file was deleted.

11 changes: 11 additions & 0 deletions packages/node/test/fixtures/session/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "astro/tsconfigs/base",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"~/assets/*": ["src/assets/*"]
},
},
"include": [".astro/types.d.ts", "**/*"],
"exclude": ["dist"]
}
115 changes: 0 additions & 115 deletions packages/node/test/session.test.js

This file was deleted.

98 changes: 98 additions & 0 deletions packages/node/test/sessions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import * as devalue from 'devalue';
import nodejs from '../dist/index.js';
import { loadFixture } from './test-utils.js';

describe('Astro.session', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/session/',
output: 'server',
adapter: nodejs({
mode: 'standalone',
experimentalSessions: true,
}),
});
});

describe('Production', () => {
let app;
before(async () => {
await fixture.build();
app = await fixture.loadTestAdapterApp();
});

async function fetchResponse(path, requestInit) {
const request = new Request(`http://example.com${path}`, requestInit);
const response = await app.render(request);
return response;
}

it('can regenerate session cookies upon request', async () => {
const firstResponse = await fetchResponse('/regenerate', { method: 'GET' });
const firstHeaders = Array.from(app.setCookieHeaders(firstResponse));
const firstSessionId = firstHeaders[0].split(';')[0].split('=')[1];

const secondResponse = await fetchResponse('/regenerate', {
method: 'GET',
headers: {
cookie: `astro-session=${firstSessionId}`,
},
});
const secondHeaders = Array.from(app.setCookieHeaders(secondResponse));
const secondSessionId = secondHeaders[0].split(';')[0].split('=')[1];
assert.notEqual(firstSessionId, secondSessionId);
});

it('can save session data by value', async () => {
const firstResponse = await fetchResponse('/update', { method: 'GET' });
const firstValue = await firstResponse.json();
assert.equal(firstValue.previousValue, 'none');

const firstHeaders = Array.from(app.setCookieHeaders(firstResponse));
const firstSessionId = firstHeaders[0].split(';')[0].split('=')[1];
const secondResponse = await fetchResponse('/update', {
method: 'GET',
headers: {
cookie: `astro-session=${firstSessionId}`,
},
});
const secondValue = await secondResponse.json();
assert.equal(secondValue.previousValue, 'expected');
});

it('can save and restore URLs in session data', async () => {
const firstResponse = await fetchResponse('/_actions/addUrl', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ favoriteUrl: 'https://domain.invalid' }),
});

assert.equal(firstResponse.ok, true);
const firstHeaders = Array.from(app.setCookieHeaders(firstResponse));
const firstSessionId = firstHeaders[0].split(';')[0].split('=')[1];

const data = devalue.parse(await firstResponse.text());
assert.equal(data.message, 'Favorite URL set to https://domain.invalid/ from nothing');
const secondResponse = await fetchResponse('/_actions/addUrl', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
cookie: `astro-session=${firstSessionId}`,
},
body: JSON.stringify({ favoriteUrl: 'https://example.com' }),
});
const secondData = devalue.parse(await secondResponse.text());
assert.equal(
secondData.message,
'Favorite URL set to https://example.com/ from https://domain.invalid/'
);
});
});
});
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 093a380

Please sign in to comment.