Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcusramberg/ca44effd7fecf16c444d8c16778f025b to your computer and use it in GitHub Desktop.
Save marcusramberg/ca44effd7fecf16c444d8c16778f025b to your computer and use it in GitHub Desktop.
diff --git a/src/body.ts b/src/body.ts
index 7699b13..b2ab8bd 100644
--- a/src/body.ts
+++ b/src/body.ts
@@ -1,5 +1,6 @@
import type {JSONValue} from './types.js';
import type {Readable, Writable} from 'node:stream';
+import type {IncomingMessage} from 'node:http';
import {on} from 'node:events';
import zlib from 'node:zlib';
import {Params} from './body/params.js';
@@ -64,6 +65,7 @@ export class Body {
* Get message body as a readable stream.
*/
createReadStream(): Readable {
+ if (this._stream.readableEnded) throw "Request already parsed, trying to read more than once?";
if (this.autoDecompress !== true || this.get('content-encoding') !== 'gzip') return this._stream;
const gunzip = zlib.createGunzip();
@@ -164,7 +166,7 @@ export class Body {
this.createReadStream()
.on('data', chunk => chunks.push(Buffer.from(chunk)))
.on('error', reject)
- .on('end', () => resolve(chunks));
+ .on('end', () => { resolve(chunks)});
});
}
diff --git a/test/app.js b/test/app.js
index f509513..ec94919 100644
--- a/test/app.js
+++ b/test/app.js
@@ -342,6 +342,18 @@ t.test('App', async t => {
// GET /gzip
app.get('/gzip', ctx => ctx.render({text: 'a'.repeat(2048)}));
+
+ // Don't read request twice
+ app.post('/req-twice', async ctx => {
+ ctx.log.debug(await ctx.req.text());
+ try {
+ ctx.render({text: await ctx.req.text()});
+ }
+ catch {
+ ctx.render({text: "request twice fails"})
+ }
+ });
+
const ua = await app.newTestUserAgent({tap: t});
t.test('Options', t => {
@@ -912,6 +924,12 @@ t.test('App', async t => {
.headerIs('Vary', 'Accept-Encoding')
.bodyIs('a'.repeat(2048));
});
+ await t.test('Request errors', async () => {
+ (await ua.postOk('/req-twice'))
+ .statusIs(200)
+ .bodyIs('request twice fails')
+ })
+
await t.test('Mock context', async () => {
app.defaults.test = 'works';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment