summaryrefslogtreecommitdiff
path: root/packages/integrations/cloudflare/test/sessions.test.js
blob: d8c1874250afb5f5a01f305a2c2ef85de1864cf9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import { fileURLToPath } from 'node:url';
import * as devalue from 'devalue';
import { astroCli, wranglerCli } from './_test-utils.js';

const root = new URL('./fixtures/sessions/', import.meta.url);

describe('astro:env', () => {
	let wrangler;

	before(async () => {
		await astroCli(fileURLToPath(root), 'build');

		wrangler = wranglerCli(fileURLToPath(root));
		await new Promise((resolve) => {
			wrangler.stdout.on('data', (data) => {
				// console.log('[stdout]', data.toString());
				if (data.toString().includes('http://127.0.0.1:8788')) resolve();
			});
			wrangler.stderr.on('data', (_data) => {
				// console.log('[stderr]', _data.toString());
			});
		});
	});

	after(() => {
		wrangler.kill();
	});

	it('can regenerate session cookies upon request', async () => {
		const firstResponse = await fetch('http://127.0.0.1:8788/regenerate', { method: 'GET' });
		const firstHeaders = firstResponse.headers.get('set-cookie').split(',');
		const firstSessionId = firstHeaders[0].split(';')[0].split('=')[1];

		const secondResponse = await fetch('http://127.0.0.1:8788/regenerate', {
			method: 'GET',
			headers: {
				cookie: `astro-session=${firstSessionId}`,
			},
		});
		const secondHeaders = secondResponse.headers.get('set-cookie').split(',');
		const secondSessionId = secondHeaders[0].split(';')[0].split('=')[1];
		assert.notEqual(firstSessionId, secondSessionId);
	});

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

		const firstHeaders = firstResponse.headers.get('set-cookie').split(',');
		const firstSessionId = firstHeaders[0].split(';')[0].split('=')[1];
		const secondResponse = await fetch('http://127.0.0.1:8788/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 fetch('http://127.0.0.1:8788/_actions/addUrl', {
			method: 'POST',
			headers: {
				'Content-Type': 'application/json',
			},
			body: JSON.stringify({ favoriteUrl: 'https://domain.invalid' }),
		});

		assert.equal(firstResponse.ok, true);
		const firstHeaders = firstResponse.headers.get('set-cookie').split(',');
		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 fetch('http://127.0.0.1:8788/_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/',
		);
	});
});