0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-27 22:19:04 -05:00

fix: pass custom statusText in Response (#12105)

* fix: pass custom statusText in Response

* Add changeset
This commit is contained in:
Matt Kane 2024-10-02 17:08:36 +01:00 committed by GitHub
parent d49a537f2a
commit 42037f33e6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 29 additions and 6 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Returns custom statusText that has been set in a Response

View file

@ -107,7 +107,8 @@ export class NodeApp extends App {
* @param destination NodeJS ServerResponse
*/
static async writeResponse(source: Response, destination: ServerResponse) {
const { status, headers, body } = source;
const { status, headers, body, statusText } = source;
destination.statusMessage = statusText;
destination.writeHead(status, createOutgoingHttpHeaders(headers));
if (!body) return destination.end();
try {

View file

@ -53,7 +53,7 @@ export function writeHtmlResponse(res: http.ServerResponse, statusCode: number,
}
export async function writeWebResponse(res: http.ServerResponse, webResponse: Response) {
const { status, headers, body } = webResponse;
const { status, headers, body, statusText } = webResponse;
// Attach any set-cookie headers added via Astro.cookies.set()
const setCookieHeaders = Array.from(getSetCookiesFromResponse(webResponse));
@ -67,7 +67,7 @@ export async function writeWebResponse(res: http.ServerResponse, webResponse: Re
if (headers.has('set-cookie')) {
_headers['set-cookie'] = headers.getSetCookie();
}
res.statusMessage = statusText;
res.writeHead(status, _headers);
if (body) {
if (Symbol.for('astro.responseBody') in webResponse) {

View file

@ -5,14 +5,19 @@ export function GET() {
{ name: 'lettuce' },
{ name: 'broccoli' },
{ name: 'pizza' }
])
]), {
status: 200,
statusText: `tasty`,
}
)
}
export async function POST({ params, request }) {
const body = await request.text();
return new Response(body === `some data` ? `ok` : `not ok`, {
status: 200,
const ok = body === `some data`
return new Response( ok ? `ok` : `not ok`, {
status: ok ? 200 : 400,
statusText: ok ? `ok` : `not ok`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}

View file

@ -33,6 +33,7 @@ describe('API routes in SSR', () => {
const request = new Request('http://example.com/food.json');
const response = await app.render(request);
assert.equal(response.status, 200);
assert.equal(response.statusText, 'tasty');
const body = await response.json();
assert.equal(body.length, 3);
});
@ -78,6 +79,17 @@ describe('API routes in SSR', () => {
assert.equal(text, 'ok');
});
it('Can read custom status text from API routes', async () => {
const response = await fixture.fetch('/food.json', {
method: 'POST',
body: `not some data`,
});
assert.equal(response.status, 400);
assert.equal(response.statusText, 'not ok');
const text = await response.text();
assert.equal(text, 'not ok');
});
it('Can be passed binary data from multipart formdata', async () => {
const formData = new FormData();
const raw = await fs.promises.readFile(