mirror of
https://github.com/withastro/astro.git
synced 2025-03-10 23:01:26 -05:00
fix(test): handle redirect responses
This commit is contained in:
parent
be8d6f2548
commit
9150208e71
1 changed files with 42 additions and 6 deletions
|
@ -176,8 +176,11 @@ describe('Astro Actions', () => {
|
||||||
const req = new Request('http://example.com/user?_astroAction=getUser', {
|
const req = new Request('http://example.com/user?_astroAction=getUser', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: new FormData(),
|
body: new FormData(),
|
||||||
|
headers: {
|
||||||
|
Referer: 'http://example.com/user',
|
||||||
|
},
|
||||||
});
|
});
|
||||||
const res = await app.render(req);
|
const res = await followRedirect(req, app);
|
||||||
assert.equal(res.ok, true);
|
assert.equal(res.ok, true);
|
||||||
|
|
||||||
const html = await res.text();
|
const html = await res.text();
|
||||||
|
@ -189,12 +192,15 @@ describe('Astro Actions', () => {
|
||||||
const req = new Request('http://example.com/user-or-throw?_astroAction=getUserOrThrow', {
|
const req = new Request('http://example.com/user-or-throw?_astroAction=getUserOrThrow', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: new FormData(),
|
body: new FormData(),
|
||||||
|
headers: {
|
||||||
|
Referer: 'http://example.com/user-or-throw',
|
||||||
|
},
|
||||||
});
|
});
|
||||||
const res = await app.render(req);
|
const res = await followRedirect(req, app);
|
||||||
assert.equal(res.ok, false);
|
|
||||||
assert.equal(res.status, 401);
|
assert.equal(res.status, 401);
|
||||||
|
|
||||||
const html = await res.text();
|
const html = await res.text();
|
||||||
|
console.log({ html });
|
||||||
let $ = cheerio.load(html);
|
let $ = cheerio.load(html);
|
||||||
assert.equal($('#error-message').text(), 'Not logged in');
|
assert.equal($('#error-message').text(), 'Not logged in');
|
||||||
assert.equal($('#error-code').text(), 'UNAUTHORIZED');
|
assert.equal($('#error-code').text(), 'UNAUTHORIZED');
|
||||||
|
@ -207,8 +213,11 @@ describe('Astro Actions', () => {
|
||||||
const req = new Request('http://example.com/user', {
|
const req = new Request('http://example.com/user', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: formData,
|
body: formData,
|
||||||
|
headers: {
|
||||||
|
Referer: 'http://example.com/user',
|
||||||
|
},
|
||||||
});
|
});
|
||||||
const res = await app.render(req);
|
const res = await followRedirect(req, app);
|
||||||
assert.equal(res.ok, true);
|
assert.equal(res.ok, true);
|
||||||
|
|
||||||
const html = await res.text();
|
const html = await res.text();
|
||||||
|
@ -222,9 +231,11 @@ describe('Astro Actions', () => {
|
||||||
const req = new Request('http://example.com/user-or-throw', {
|
const req = new Request('http://example.com/user-or-throw', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: formData,
|
body: formData,
|
||||||
|
headers: {
|
||||||
|
Referer: 'http://example.com/user',
|
||||||
|
},
|
||||||
});
|
});
|
||||||
const res = await app.render(req);
|
const res = await followRedirect(req, app);
|
||||||
assert.equal(res.ok, false);
|
|
||||||
assert.equal(res.status, 401);
|
assert.equal(res.status, 401);
|
||||||
|
|
||||||
const html = await res.text();
|
const html = await res.text();
|
||||||
|
@ -325,3 +336,28 @@ describe('Astro Actions', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const validRedirectStatuses = new Set([301, 302, 303, 304, 307, 308]);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Follow an expected redirect response.
|
||||||
|
*
|
||||||
|
* @param {Request} req
|
||||||
|
* @param {*} app
|
||||||
|
* @returns {Promise<Response>}
|
||||||
|
*/
|
||||||
|
async function followRedirect(req, app) {
|
||||||
|
const redirect = await app.render(req, { addCookieHeader: true });
|
||||||
|
assert.ok(
|
||||||
|
validRedirectStatuses.has(redirect.status),
|
||||||
|
`Expected redirect status, got ${redirect.status}`
|
||||||
|
);
|
||||||
|
|
||||||
|
const redirectUrl = new URL(redirect.headers.get('Location'), req.url);
|
||||||
|
const redirectReq = new Request(redirectUrl, {
|
||||||
|
headers: {
|
||||||
|
Cookie: redirect.headers.get('Set-Cookie'),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return app.render(redirectReq);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue