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
|
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import * as cheerio from 'cheerio';
import testAdapter from './test-adapter.js';
import { loadFixture } from './test-utils.js';
describe('Astro.cookies', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/astro-cookies/',
output: 'server',
adapter: testAdapter(),
});
});
describe('Development', () => {
/** @type {import('./test-utils').DevServer} */
let devServer;
before(async () => {
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('is able to get cookies from the request', async () => {
const response = await fixture.fetch('/get-json', {
headers: {
cookie: `prefs=${encodeURIComponent(JSON.stringify({ mode: 'light' }))}`,
},
});
assert.equal(response.status, 200);
const html = await response.text();
const $ = cheerio.load(html);
assert.equal($('dd').text(), 'light');
});
it('can set the cookie value', async () => {
const response = await fixture.fetch('/set-value', {
method: 'POST',
});
assert.equal(response.status, 200);
// Bug in 18.14.1 where `set-cookie` will not be defined
// Should be fixed in 18.14.2
if (process.versions.node !== '18.14.1') {
assert.equal(response.headers.has('set-cookie'), 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('is able to get cookies from the request', async () => {
const response = await fetchResponse('/get-json', {
headers: {
cookie: `prefs=${encodeURIComponent(JSON.stringify({ mode: 'light' }))}`,
},
});
assert.equal(response.status, 200);
const html = await response.text();
const $ = cheerio.load(html);
assert.equal($('dd').text(), 'light');
});
it('can set the cookie value', async () => {
const response = await fetchResponse('/set-value', {
method: 'POST',
});
assert.equal(response.status, 200);
let headers = Array.from(app.setCookieHeaders(response));
assert.equal(headers.length, 1);
assert.match(headers[0], /Expires/);
});
it('app.render can include the cookie in the Set-Cookie header', async () => {
const request = new Request('http://example.com/set-value', {
method: 'POST',
});
const response = await app.render(request, { addCookieHeader: true });
assert.equal(response.status, 200);
const value = response.headers.get('Set-Cookie');
assert.equal(typeof value, 'string');
assert.equal(value.startsWith('admin=true; Expires='), true);
});
it('app.render can exclude the cookie from the Set-Cookie header', async () => {
const request = new Request('http://example.com/set-value', {
method: 'POST',
});
const response = await app.render(request, { addCookieHeader: false });
assert.equal(response.status, 200);
assert.equal(response.headers.get('Set-Cookie'), null);
});
it('Early returning a Response still includes set headers', async () => {
const response = await fetchResponse('/early-return', {
headers: {
cookie: `prefs=${encodeURIComponent(JSON.stringify({ mode: 'light' }))}`,
},
});
assert.equal(response.status, 302);
let headers = Array.from(app.setCookieHeaders(response));
assert.equal(headers.length, 1);
let raw = headers[0].slice(6);
let data = JSON.parse(decodeURIComponent(raw));
assert.equal(typeof data, 'object');
assert.equal(data.mode, 'dark');
});
it('API route can get and set cookies', async () => {
const response = await fetchResponse('/set-prefs', {
method: 'POST',
headers: {
cookie: `prefs=${encodeURIComponent(JSON.stringify({ mode: 'light' }))}`,
},
});
assert.equal(response.status, 302);
let headers = Array.from(app.setCookieHeaders(response));
assert.equal(headers.length, 1);
let raw = headers[0].slice(6);
let data = JSON.parse(decodeURIComponent(raw));
assert.equal(typeof data, 'object');
assert.equal(data.mode, 'dark');
});
});
});
|