summaryrefslogtreecommitdiff
path: root/packages/integrations/node/test/sessions.test.js
blob: e3cac3f11f2d8bd08f7fa120d743cb374627a1af (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// @ts-check
import assert from 'node:assert/strict';
import { after, 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', () => {
	describe('Production', () => {
		/** @type {import('./test-utils').Fixture} */
		let fixture;

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

		/** @type {import('../../../astro/src/types/public/preview.js').PreviewServer} */
		let app;
		before(async () => {
			await fixture.build({});
			app = await fixture.preview({});
		});

		after(async () => {
			await app.stop();
		});

		it('can regenerate session cookies upon request', async () => {
			const firstResponse = await fixture.fetch('/regenerate');
			// @ts-ignore
			const firstHeaders = firstResponse.headers.get('set-cookie').split(',');
			const firstSessionId = firstHeaders[0].split(';')[0].split('=')[1];

			const secondResponse = await fixture.fetch('/regenerate', {
				method: 'GET',
				headers: {
					cookie: `astro-session=${firstSessionId}`,
				},
			});
			// @ts-ignore
			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 fixture.fetch('/update');
			const firstValue = await firstResponse.json();
			assert.equal(firstValue.previousValue, 'none');

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

			assert.equal(firstResponse.ok, true);
			// @ts-ignore
			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 fixture.fetch('/_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/',
			);
		});
	});

	describe('Development', () => {
		/** @type {import('./test-utils').Fixture} */
		let fixture;
		let devServer;
		before(async () => {
			fixture = await loadFixture({
				root: './fixtures/sessions/',
				output: 'server',
				adapter: nodejs({ mode: 'middleware' }),
				experimental: {
					session: true,
				},
			});
			devServer = await fixture.startDevServer();
		});

		after(async () => {
			await devServer.stop();
		});

		it('can regenerate session cookies upon request', async () => {
			const firstResponse = await fixture.fetch('/regenerate');
			// @ts-ignore
			const firstHeaders = firstResponse.headers.get('set-cookie').split(',');
			const firstSessionId = firstHeaders[0].split(';')[0].split('=')[1];

			const secondResponse = await fixture.fetch('/regenerate', {
				method: 'GET',
				headers: {
					cookie: `astro-session=${firstSessionId}`,
				},
			});
			// @ts-ignore
			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 fixture.fetch('/update');
			const firstValue = await firstResponse.json();
			assert.equal(firstValue.previousValue, 'none');

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

			assert.equal(firstResponse.ok, true);
			// @ts-ignore
			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 fixture.fetch('/_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/',
			);
		});
	});
});