add option to persist response body in context for HEAD requests
[squeep-api-dingus] / test / lib / dingus.js
index 7036384494cf7d265b7c287d038541c22063c1b8..b478a454aa3eb5b4b3ad8027382e4f5f23bd0681 100644 (file)
@@ -269,7 +269,7 @@ describe('Dingus', function () {
       };
       ctx = {};
     });
-    it('collects body without writing', function () {
+    it('collects response without writing', function () {
       Dingus.setHeadHandler(req, res, ctx);
       res.write(Buffer.from('foo'));
       res.write('baz');
@@ -277,6 +277,16 @@ describe('Dingus', function () {
       res.end('quux');
       assert(!origWrite.called);
       assert(origEnd.called);
+      assert.deepStrictEqual(ctx.responseBody, undefined);
+    });
+    it('collects response without writing, persists written data', function () {
+      Dingus.setHeadHandler(req, res, ctx, true);
+      res.write(Buffer.from('foo'));
+      res.write('baz');
+      res.write();
+      res.end('quux');
+      assert(!origWrite.called);
+      assert(origEnd.called);
       assert.deepStrictEqual(ctx.responseBody, Buffer.from('foobazquux'));
     });
     it('ignores non-head method', function () {
@@ -514,6 +524,17 @@ describe('Dingus', function () {
         assert.strictEqual(e, 'foo');
       }
     });
+    it('limits size', async function () {
+      const p = dingus.bodyData(res, 8);
+      resEvents['data'](Buffer.from('foobar'));
+      resEvents['data'](Buffer.from('bazquux'));
+      try {
+        await p;
+        assert.fail(noExpectedException);
+      } catch (e) {
+        assert.strictEqual(e.statusCode, 413);
+      }
+    });
   }); // bodyData
 
   describe('ingestBody', function () {
@@ -667,8 +688,8 @@ Content-Type: image/sgi
         size: 8,
         blocks: 17,
         atimeMs: 1613253436842.815,
-        mtimeMs: 1603485933192.8610,
-        ctimeMs: 1603485933192.8610,
+        mtimeMs: 1603485933192.861,
+        ctimeMs: 1603485933192.861,
         birthtimeMs: 0,
         atime: '2021-02-13T21:57:16.843Z',
         mtime: '2020-10-23T13:45:33.193Z',
@@ -707,6 +728,11 @@ Content-Type: image/sgi
       await dingus.serveFile(req, res, ctx, directory, fileName);
       assert(dingus.handlerNotFound.called);
     });
+    it('requires directory be specified', async function () {
+      await dingus.serveFile(req, res, ctx, '', fileName);
+      assert(!fs.promises.readFile.called);
+      assert(dingus.handlerNotFound.called);
+    });
     it('covers fs error', async function () {
       const expectedException = new Error('blah');
       fs.promises.stat.restore();