Skip to content

Commit e3bf8a9

Browse files
committed
chore: fix test failures caused by returned status code changes
1 parent 66a3498 commit e3bf8a9

1 file changed

Lines changed: 27 additions & 8 deletions

File tree

‎packages/void/test/integration/server-auth-errors.test.ts‎

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,19 @@ function responseFor(path: string, method = 'GET') {
122122
);
123123
}
124124

125-
async function expectRedirect(response: Response, destination: string, status: number) {
126-
expect(response.status()).toBe(status);
125+
async function expectRedirect(
126+
response: Response,
127+
destination: string,
128+
status: number,
129+
enhanced = false,
130+
) {
131+
// Pages negotiates redirects as HTTP 200 so fetch can read Location (including
132+
// fragments). Document requests keep the original HTTP redirect status.
133+
expect(response.status()).toBe(enhanced ? 200 : status);
134+
expect(await response.request().headerValue('X-VoidPages-Redirect')).toBe(
135+
enhanced ? 'manual' : null,
136+
);
137+
expect(await response.headerValue('X-VoidPages-Redirect')).toBe(enhanced ? String(status) : null);
127138
expect(await response.headerValue('location')).toBe(destination);
128139
await page.waitForURL(`${server.base}${destination}`);
129140
}
@@ -142,7 +153,9 @@ describe.each([
142153
await page.locator(`a[href="${path}"]`).filter({ hasText: visit }).click();
143154
const response = await pending;
144155
const expectedStatus = signedIn ? 200 : status;
145-
expect(response.status()).toBe(expectedStatus);
156+
if (expectedStatus !== 302) {
157+
expect(response.status()).toBe(expectedStatus);
158+
}
146159

147160
if (expectedStatus === 401) {
148161
await expect.poll(() => page.locator('body').innerText()).toBe('Unauthorized');
@@ -155,7 +168,7 @@ describe.each([
155168
}
156169

157170
if (expectedStatus === 302) {
158-
await expectRedirect(response, '/login', 302);
171+
await expectRedirect(response, '/login', 302, visit === 'Client navigation');
159172
} else {
160173
await page.waitForURL(`${server.base}${path}`);
161174
}
@@ -189,6 +202,8 @@ describe.each([
189202
await open(path, signedIn);
190203
const timeOrigin = await markLayout();
191204
const pending = responseFor(path, 'POST');
205+
const destination = signedIn ? '/actions/success' : '/login';
206+
const followUp = signedIn || guard !== 'require-auth' ? responseFor(destination) : undefined;
192207
await page.getByRole('button', { name: form, exact: true }).click();
193208
const response = await pending;
194209

@@ -208,9 +223,11 @@ describe.each([
208223
return;
209224
}
210225

211-
const destination = signedIn ? '/actions/success' : '/login';
212-
await expectRedirect(response, destination, 303);
213-
expect(response.request().redirectedTo()?.method()).toBe('GET');
226+
await expectRedirect(response, destination, 303, form === 'Enhanced form POST');
227+
expect((await followUp)?.status()).toBe(200);
228+
if (form === 'Native form POST') {
229+
expect(response.request().redirectedTo()?.method()).toBe('GET');
230+
}
214231
await page
215232
.getByRole('heading', {
216233
name: signedIn ? 'Action authorized' : 'Sign in',
@@ -316,6 +333,7 @@ describe('demo session and navigation', () => {
316333
});
317334
expect(signedOut.status()).toBe(200);
318335
const pending = responseFor(path, 'POST');
336+
const followUp = guard !== 'require-auth' ? responseFor('/login') : undefined;
319337
await page.getByRole('button', { name: form, exact: true }).click();
320338
const response = await pending;
321339

@@ -327,7 +345,8 @@ describe('demo session and navigation', () => {
327345
await expect.poll(() => page.getByRole('alert').innerText()).toBe('401: Unauthorized');
328346
}
329347
} else {
330-
await expectRedirect(response, '/login', 303);
348+
await expectRedirect(response, '/login', 303, form === 'Enhanced form POST');
349+
expect((await followUp)?.status()).toBe(200);
331350
await page.getByRole('heading', { name: 'Sign in', exact: true }).waitFor();
332351
}
333352
expect(await page.getByRole('heading', { name: 'Action authorized' }).count()).toBe(0);

0 commit comments

Comments
 (0)