/
middleware.test.js
223 lines (191 loc) · 7.4 KB
/
middleware.test.js
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import testAdapter from './test-adapter.js';
describe('Middleware in DEV mode', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let devServer;
before(async () => {
fixture = await loadFixture({
root: './fixtures/middleware-dev/',
});
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('should render locals data', async () => {
const html = await fixture.fetch('/').then((res) => res.text());
const $ = cheerio.load(html);
expect($('p').html()).to.equal('bar');
});
it('should change locals data based on URL', async () => {
let html = await fixture.fetch('/').then((res) => res.text());
let $ = cheerio.load(html);
expect($('p').html()).to.equal('bar');
html = await fixture.fetch('/lorem').then((res) => res.text());
$ = cheerio.load(html);
expect($('p').html()).to.equal('ipsum');
});
it('should call a second middleware', async () => {
let html = await fixture.fetch('/second').then((res) => res.text());
let $ = cheerio.load(html);
expect($('p').html()).to.equal('second');
});
it('should successfully create a new response', async () => {
let html = await fixture.fetch('/rewrite').then((res) => res.text());
let $ = cheerio.load(html);
expect($('p').html()).to.be.null;
expect($('span').html()).to.equal('New content!!');
});
it('should return a new response that is a 500', async () => {
await fixture.fetch('/broken-500').then((res) => {
expect(res.status).to.equal(500);
return res.text();
});
});
it('should successfully render a page if the middleware calls only next() and returns nothing', async () => {
let html = await fixture.fetch('/not-interested').then((res) => res.text());
let $ = cheerio.load(html);
expect($('p').html()).to.equal('Not interested');
});
it("should throw an error when the middleware doesn't call next or doesn't return a response", async () => {
let html = await fixture.fetch('/does-nothing').then((res) => res.text());
let $ = cheerio.load(html);
expect($('title').html()).to.equal('MiddlewareNoDataOrNextCalled');
});
});
describe('Middleware in PROD mode, SSG', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/middleware-ssg/',
});
await fixture.build();
});
it('should render locals data', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
expect($('p').html()).to.equal('bar');
});
it('should change locals data based on URL', async () => {
let html = await fixture.readFile('/index.html');
let $ = cheerio.load(html);
expect($('p').html()).to.equal('bar');
html = await fixture.readFile('/second/index.html');
$ = cheerio.load(html);
expect($('p').html()).to.equal('second');
});
});
describe('Middleware API in PROD mode, SSR', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/middleware-dev/',
output: 'server',
adapter: testAdapter({
// exports: ['manifest', 'createApp', 'middleware'],
}),
});
await fixture.build();
});
it('should render locals data', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/');
const response = await app.render(request);
const html = await response.text();
const $ = cheerio.load(html);
expect($('p').html()).to.equal('bar');
});
it('should change locals data based on URL', async () => {
const app = await fixture.loadTestAdapterApp();
let response = await app.render(new Request('http://example.com/'));
let html = await response.text();
let $ = cheerio.load(html);
expect($('p').html()).to.equal('bar');
response = await app.render(new Request('http://example.com/lorem'));
html = await response.text();
$ = cheerio.load(html);
expect($('p').html()).to.equal('ipsum');
});
it('should successfully redirect to another page', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/redirect');
const response = await app.render(request);
expect(response.status).to.equal(302);
});
it('should call a second middleware', async () => {
const app = await fixture.loadTestAdapterApp();
const response = await app.render(new Request('http://example.com/second'));
const html = await response.text();
const $ = cheerio.load(html);
expect($('p').html()).to.equal('second');
});
it('should successfully create a new response', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/rewrite');
const response = await app.render(request);
const html = await response.text();
const $ = cheerio.load(html);
expect($('p').html()).to.be.null;
expect($('span').html()).to.equal('New content!!');
});
it('should return a new response that is a 500', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/broken-500');
const response = await app.render(request);
expect(response.status).to.equal(500);
});
it('should successfully render a page if the middleware calls only next() and returns nothing', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/not-interested');
const response = await app.render(request);
const html = await response.text();
const $ = cheerio.load(html);
expect($('p').html()).to.equal('Not interested');
});
it("should throws an error when the middleware doesn't call next or doesn't return a response", async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/does-nothing');
const response = await app.render(request);
const html = await response.text();
const $ = cheerio.load(html);
expect($('title').html()).to.not.equal('MiddlewareNoDataReturned');
});
it('should correctly work for API endpoints that return a Response object', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/api/endpoint');
const response = await app.render(request);
expect(response.status).to.equal(200);
expect(response.headers.get('Content-Type')).to.equal('application/json');
});
it('should correctly manipulate the response coming from API endpoints (not simple)', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/api/endpoint');
const response = await app.render(request);
const text = await response.text();
expect(text.includes('REDACTED')).to.be.true;
});
});
describe('Middleware with tailwind', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/middleware-tailwind/',
});
await fixture.build();
});
it('should correctly emit the tailwind CSS file', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
const bundledCSSHREF = $('link[rel=stylesheet][href^=/_astro/]').attr('href');
const bundledCSS = (await fixture.readFile(bundledCSSHREF.replace(/^\/?/, '/')))
.replace(/\s/g, '')
.replace('/n', '');
expect(bundledCSS.includes('--tw-content')).to.be.true;
});
});