-
Notifications
You must be signed in to change notification settings - Fork 652
Expand file tree
/
Copy pathaposPageFetch.test.js
More file actions
189 lines (176 loc) · 5.39 KB
/
Copy pathaposPageFetch.test.js
File metadata and controls
189 lines (176 loc) · 5.39 KB
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
import assert from 'node:assert/strict';
import esmock from 'esmock';
const mockConfig = {
aposHost: 'http://localhost:3000',
aposPrefix: '',
staticBuild: null,
excludeRequestHeaders: []
};
function jsonResponse(data) {
return new Response(JSON.stringify(data), {
headers: { 'content-type': 'application/json' }
});
}
// Returns { aposPageFetch, calls } where calls records the URL of every
// request passed to the mocked aposResponse, and responses are served
// from the given queue in order.
async function load(responses, configOverrides = {}) {
const calls = [];
const queue = [ ...responses ];
const mod = await esmock('../../lib/aposPageFetch.js', {
'apostrophe-astro-config/config': {
default: { ...mockConfig, ...configOverrides }
},
'../../lib/aposResponse.js': {
default: async (request) => {
calls.push(request.url);
if (!queue.length) {
throw new Error('aposResponse called more times than expected');
}
const next = queue.shift();
return typeof next === 'function' ? next() : jsonResponse(next);
}
}
});
return {
aposPageFetch: mod.default,
calls
};
}
describe('aposPageFetch redirect handling', () => {
beforeEach(() => {
process.env.APOS_EXTERNAL_FRONT_KEY = 'test-secret-key';
});
afterEach(() => {
delete process.env.APOS_EXTERNAL_FRONT_KEY;
delete process.env.APOS_ASTRO_STATIC_BUILD;
});
it('preserves the query string when following a trailing-slash redirect', async () => {
const { aposPageFetch, calls } = await load([
{
redirect: true,
url: '/articles',
status: 302
},
{
template: 'article-page:index',
currentPage: 2
}
]);
const data = await aposPageFetch(
new Request('http://localhost:4321/articles/?page=2')
);
assert.equal(calls.length, 2);
assert.equal(calls[1], 'http://localhost:4321/articles?page=2');
assert.equal(data.currentPage, 2);
assert.ok(!data.redirect);
});
it('still follows a slash-adding locale home redirect internally', async () => {
const { aposPageFetch, calls } = await load([
{
redirect: true,
url: '/fr/',
status: 302
},
{
template: '@apostrophecms/home-page:page'
}
]);
const data = await aposPageFetch(new Request('http://localhost:4321/fr'));
assert.equal(calls.length, 2);
assert.equal(calls[1], 'http://localhost:4321/fr/');
assert.ok(!data.redirect);
});
it('prefers the redirect target\'s own query string over the request\'s', async () => {
const { aposPageFetch, calls } = await load([
{
redirect: true,
url: '/articles?category=tech',
status: 302
},
{
template: 'article-page:index'
}
]);
await aposPageFetch(
new Request('http://localhost:4321/articles/?page=2')
);
assert.equal(calls.length, 2);
assert.equal(calls[1], 'http://localhost:4321/articles?category=tech');
});
it('passes through a redirect to a different path without retrying', async () => {
const { aposPageFetch, calls } = await load([
{
redirect: true,
url: '/new-location',
status: 301
}
]);
const data = await aposPageFetch(new Request('http://localhost:4321/old-location'));
assert.equal(calls.length, 1);
assert.equal(data.redirect, true);
assert.equal(data.url, '/new-location');
assert.equal(data.status, 301);
});
it('passes through an absolute redirect to another host even when paths match', async () => {
const { aposPageFetch, calls } = await load([
{
redirect: true,
url: 'https://other.example.com/articles',
status: 302
}
]);
const data = await aposPageFetch(new Request('http://localhost:4321/articles/'));
assert.equal(calls.length, 1);
assert.equal(data.redirect, true);
assert.equal(data.url, 'https://other.example.com/articles');
});
it('strips and restores the prefix around the internal retry', async () => {
const { aposPageFetch, calls } = await load([
{
redirect: true,
url: '/articles',
status: 302
},
{
template: 'article-page:index'
}
], { aposPrefix: '/base' });
await aposPageFetch(
new Request('http://localhost:4321/base/articles/?page=2')
);
assert.equal(calls.length, 2);
assert.equal(calls[1], 'http://localhost:4321/base/articles?page=2');
});
it('reports an infinite redirect instead of looping', async () => {
const { aposPageFetch, calls } = await load([
{
redirect: true,
url: '/articles',
status: 302
},
{
redirect: true,
url: '/articles',
status: 302
}
]);
const data = await aposPageFetch(new Request('http://localhost:4321/articles/'));
assert.equal(calls.length, 2);
assert.ok(data.errorFetchingPage);
assert.match(data.errorFetchingPage.message, /Infinite redirect/);
assert.equal(data.page.type, 'apos-fetch-error');
});
it('never retries a redirect to the site root', async () => {
const { aposPageFetch, calls } = await load([
{
redirect: true,
url: '/',
status: 302
}
]);
const data = await aposPageFetch(new Request('http://localhost:4321//'));
assert.equal(calls.length, 1);
assert.equal(data.redirect, true);
});
});