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/132ba0e79dfd63ec114a7cf5aa789270 to your computer and use it in GitHub Desktop.
Save marcusramberg/132ba0e79dfd63ec114a7cf5aa789270 to your computer and use it in GitHub Desktop.
diff --git a/src/body.ts b/src/body.ts
index 7699b13..e360a37 100644
--- a/src/body.ts
+++ b/src/body.ts
@@ -64,6 +64,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();
diff --git a/test/app.js b/test/app.js
index f509513..2d5354b 100644
--- a/test/app.js
+++ b/test/app.js
@@ -342,6 +342,16 @@ 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 +922,9 @@ 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