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
|
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
import testAdapter from './test-adapter.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' }))}`,
},
});
expect(response.status).to.equal(200);
const html = await response.text();
const $ = cheerio.load(html);
expect($('dd').text()).to.equal('light');
});
it('can set the cookie value', async () => {
const response = await fixture.fetch('/set-value', {
method: 'POST',
});
expect(response.status).to.equal(200);
expect(response.headers.has('set-cookie')).to.equal(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' }))}`,
},
});
expect(response.status).to.equal(200);
const html = await response.text();
const $ = cheerio.load(html);
expect($('dd').text()).to.equal('light');
});
it('can set the cookie value', async () => {
const response = await fetchResponse('/set-value', {
method: 'POST',
});
expect(response.status).to.equal(200);
let headers = Array.from(app.setCookieHeaders(response));
expect(headers).to.have.a.lengthOf(1);
expect(headers[0]).to.match(/Expires/);
});
it('Early returning a Response still includes set headers', async () => {
const response = await fetchResponse('/early-return', {
headers: {
cookie: `prefs=${encodeURIComponent(JSON.stringify({ mode: 'light' }))}`,
},
});
expect(response.status).to.equal(302);
let headers = Array.from(app.setCookieHeaders(response));
expect(headers).to.have.a.lengthOf(1);
let raw = headers[0].slice(6);
let data = JSON.parse(decodeURIComponent(raw));
expect(data).to.be.an('object');
expect(data.mode).to.equal('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' }))}`,
},
});
expect(response.status).to.equal(302);
let headers = Array.from(app.setCookieHeaders(response));
expect(headers).to.have.a.lengthOf(1);
let raw = headers[0].slice(6);
let data = JSON.parse(decodeURIComponent(raw));
expect(data).to.be.an('object');
expect(data.mode).to.equal('dark');
});
});
});
|