Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/node-standalone-400-content-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/node': patch
---

Adds `Content-Type: text/plain; charset=utf-8` header to `400` responses for undecodable URLs in standalone mode.
2 changes: 1 addition & 1 deletion packages/integrations/node/src/standalone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function createStandaloneHandler(
// validate request path
decodeURI(req.url!);
} catch {
res.writeHead(400);
res.writeHead(400, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('Bad request.');
return;
}
Expand Down
42 changes: 42 additions & 0 deletions packages/integrations/node/test/units/standalone-handler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import assert from 'node:assert/strict';
import http from 'node:http';
import type { AddressInfo } from 'node:net';
import { after, before, describe, it } from 'node:test';
import { createStandaloneHandler } from '../../dist/standalone.js';

describe('createStandaloneHandler', () => {
let server: http.Server;
let port: number;

before(async () => {
const server_ = new URL('../../dist/', import.meta.url).href;
const client = new URL('../../dist-client-missing/', import.meta.url).href;
const app = {
removeBase: (p: string) => p,
match: () => undefined,
manifest: { trailingSlash: 'ignore', buildFormat: 'directory', assetsDir: '_astro' },
};
const handler = createStandaloneHandler(
app as any,
{ client, server: server_, port: 0, host: '127.0.0.1', mode: 'standalone' } as any,
undefined,
);

server = http.createServer(handler);
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
port = (server.address() as AddressInfo).port;
});

after(() => {
server.close();
});

it('answers undecodable URLs with 400 and text/plain Content-Type', async () => {
for (const badPath of ['/%C0%AF', '/?q=100%']) {
const res = await fetch(`http://127.0.0.1:${port}${badPath}`);
assert.equal(res.status, 400);
assert.equal(res.headers.get('content-type'), 'text/plain; charset=utf-8');
assert.equal(await res.text(), 'Bad request.');
}
});
});
Loading