1 /* eslint-disable capitalized-comments */
5 const assert
= require('assert');
6 const sinon
= require('sinon'); // eslint-disable-line node/no-unpublished-require
7 const fs
= require('fs');
9 const Dingus
= require('../../lib/dingus');
10 const { DingusError
} = require('../../lib/errors');
11 const Enum
= require('../../lib/enum');
13 const noExpectedException
= 'did not get expected exception';
20 describe('Dingus', function () {
22 beforeEach(function () {
23 dingus
= new Dingus(noLogger
, {});
25 afterEach(function () {
29 describe('constructor', function () {
30 it('covers', function () {
31 const d
= new Dingus();
36 describe('_normalizePath', function () {
37 it('returns normal path', function () {
39 const r
= dingus
._normalizePath(p
);
40 assert
.strictEqual(r
, p
);
42 it('returns normal path', function () {
43 const p
= '////a///b/./bar/..///c';
44 const expected
= '/a/b/c';
45 const r
= dingus
._normalizePath(p
);
46 assert
.strictEqual(r
, expected
);
50 describe('_splitUrl', function () {
51 const nullObject
= Object
.create(null);
53 it('splits a simple path', function () {
57 queryParams: nullObject
,
59 const r
= dingus
._splitUrl(p
);
60 assert
.deepStrictEqual(r
, expected
);
62 it('splits a path with trailing slash preserved', function () {
66 queryParams: nullObject
,
68 const r
= dingus
._splitUrl(p
);
69 assert
.deepStrictEqual(r
, expected
);
71 it('splits a path with trailing slash ignored', function () {
75 queryParams: nullObject
,
77 dingus
.ignoreTrailingSlash
= true;
78 const r
= dingus
._splitUrl(p
);
79 assert
.deepStrictEqual(r
, expected
);
81 it('splits a path with empty query string', function () {
85 queryParams: nullObject
,
87 const r
= dingus
._splitUrl(p
);
88 assert
.deepStrictEqual(r
, expected
);
90 it('splits a path with query string', function () {
91 const p
= '/a/b/c?x=1&y=2&z';
94 queryParams: Object
.assign(Object
.create(null), {
97 z: '', // Subjective Editorial: disagree with the default querystring parser behavior here: null would be better than empty string, esp as result is null-prototyped object.
100 const r
= dingus
._splitUrl(p
);
101 assert
.deepStrictEqual(r
, expected
);
105 describe('tagContext', function () {
107 beforeEach(function () {
109 getHeader: sinon
.stub(),
110 setHeader: sinon
.stub(),
113 getHeader: sinon
.stub(),
114 setHeader: sinon
.stub(),
118 it ('sets id in context', function () {
119 const result
= Dingus
.tagContext(req
, res
, ctx
);
120 assert
.strictEqual(ctx
.requestId
, result
);
121 assert(res
.setHeader
.called
);
123 it ('sets provided header', function () {
124 req
.getHeader
.onCall(0).returns('abc'); // X-Request-ID
125 const result
= Dingus
.tagContext(req
, res
, ctx
);
126 assert
.strictEqual(ctx
.requestId
, result
);
127 assert
.strictEqual(res
.setHeader
.getCall(0).args
[0], 'Request-ID');
128 assert
.strictEqual(res
.setHeader
.getCall(1).args
[0], 'X-Request-ID');
129 assert
.strictEqual(res
.setHeader
.getCall(1).args
[1], 'abc');
130 assert
.strictEqual(res
.setHeader
.callCount
, 2);
134 describe('clientAddressContext', function () {
138 _tp
= dingus
.trustProxy
;
141 dingus
.trustProxy
= _tp
;
143 beforeEach(function () {
145 getHeader: sinon
.stub(),
146 setHeader: sinon
.stub(),
150 getHeader: sinon
.stub(),
151 setHeader: sinon
.stub(),
155 it ('covers untrusted proxy', function () {
156 dingus
.trustProxy
= false;
159 clientProtocol: 'http',
161 dingus
.clientAddressContext(req
, res
, ctx
);
162 assert
.deepStrictEqual(ctx
, expected
);
163 assert(!req
.getHeader
.called
);
165 it ('covers missing', function () {
166 dingus
.trustProxy
= true;
168 clientAddress: '::1',
169 clientProtocol: 'https',
171 req
.connection
.remoteAddress
= '::1';
172 req
.connection
.encrypted
= true;
173 dingus
.clientAddressContext(req
, res
, ctx
);
174 assert(req
.getHeader
.called
);
175 assert
.deepStrictEqual(ctx
, expected
);
177 }); // clientAddressContext
179 describe('getRequestContentType', function () {
181 beforeEach(function () {
183 getHeader: sinon
.stub(),
184 setHeader: sinon
.stub(),
187 it('handles missing header', function () {
188 const result
= Dingus
.getRequestContentType(req
);
189 assert
.strictEqual(result
, '');
191 it('parses simple type', function () {
192 req
.getHeader
.onCall(0).returns(Enum
.ContentType
.ApplicationJson
);
193 const result
= Dingus
.getRequestContentType(req
);
194 assert
.strictEqual(result
, Enum
.ContentType
.ApplicationJson
);
196 it('parses complex type', function () {
197 req
.getHeader
.onCall(0).returns('application/json ; charset=UTF-8');
198 const result
= Dingus
.getRequestContentType(req
);
199 assert
.strictEqual(result
, Enum
.ContentType
.ApplicationJson
);
201 }); // getRequestContentType
203 describe('setResponseContentType', function () {
204 let req
, responseTypes
;
205 beforeEach(function () {
208 setHeader: sinon
.stub(),
209 getHeader: sinon
.stub(),
212 it('handles missing header', function () {
213 const result
= Dingus
.getResponseContentType(responseTypes
, req
);
214 assert
.strictEqual(result
, undefined);
216 it('behaves as expected', function () {
217 responseTypes
.push(Enum
.ContentType
.ApplicationJson
);
218 req
.getHeader
.onCall(0).returns('text, image/png;q=0.5, application/*;q=0.2, audio;q=0.1');
219 const result
= Dingus
.getResponseContentType(responseTypes
, req
);
220 assert
.strictEqual(result
, Enum
.ContentType
.ApplicationJson
);
222 }); // setResponseContentType
224 describe('on', function () {
226 beforeEach(function () {
227 stubOn
= sinon
.stub(dingus
.router
, 'on');
229 it('covers', function () {
230 dingus
.on('GET', '/', () => {});
231 assert(stubOn
.called
);
235 describe('setEndBodyHandler', function () {
236 let req
, res
, ctx
, handler
, origEnd
, origWrite
;
237 beforeEach(function () {
238 origEnd
= sinon
.stub();
239 origWrite
= sinon
.stub();
246 handler
= sinon
.stub();
248 it('collects body and handles', function () {
249 Dingus
.setEndBodyHandler(req
, res
, ctx
, handler
);
250 res
.write(Buffer
.from('foo'));
254 assert(origWrite
.called
);
255 assert(origEnd
.called
);
256 assert
.deepStrictEqual(ctx
.responseBody
, Buffer
.from('foobazquux'));
257 assert(handler
.called
);
259 }); // setEndBodyHandler
261 describe('setHeadHandler', function () {
262 let req
, res
, ctx
, origEnd
, origWrite
;
263 beforeEach(function () {
264 origEnd
= sinon
.stub();
265 origWrite
= sinon
.stub();
272 setHeader: sinon
.stub(),
276 it('collects response without writing', function () {
277 Dingus
.setHeadHandler(req
, res
, ctx
);
278 res
.write(Buffer
.from('foo'));
282 assert(!origWrite
.called
);
283 assert(origEnd
.called
);
284 assert
.deepStrictEqual(ctx
.responseBody
, undefined);
286 it('collects response without writing, persists written data', function () {
287 Dingus
.setHeadHandler(req
, res
, ctx
, true);
288 res
.write(Buffer
.from('foo'));
292 assert(!origWrite
.called
);
293 assert(origEnd
.called
);
294 assert
.deepStrictEqual(ctx
.responseBody
, Buffer
.from('foobazquux'));
296 it('ignores non-head method', function () {
298 Dingus
.setHeadHandler(req
, res
, ctx
);
299 res
.write(Buffer
.from('foo'));
301 assert(origWrite
.called
);
302 assert(origEnd
.called
);
304 }); // setHeadHandler
306 describe('addEncodingHeader', function () {
308 beforeEach(function () {
311 // eslint-disable-next-line security/detect-object-injection
312 getHeader: (h
) => res
._headers
[h
],
313 // eslint-disable-next-line security/detect-object-injection
314 setHeader: (h
, v
) => res
._headers
[h
] = v
,
317 it('adds', function () {
319 Dingus
.addEncodingHeader(res
, encoding
);
320 assert
.strictEqual(res
._headers
[Enum
.Header
.ContentEncoding
], 'gzip');
322 it('extends', function () {
324 Dingus
.addEncodingHeader(res
, encoding
);
325 assert
.strictEqual(res
._headers
[Enum
.Header
.ContentEncoding
], 'utf8');
327 Dingus
.addEncodingHeader(res
, encoding
);
328 assert
.strictEqual(res
._headers
[Enum
.Header
.ContentEncoding
], 'gzip, utf8');
330 }); // addEncodingHeader
332 describe('dispatch', function () {
333 let pathsByLengthOrig
;
337 beforeEach(function () {
341 setHeader: sinon
.stub(),
342 getHeader: sinon
.stub(),
347 setHeader: sinon
.stub(),
348 hasHeader: sinon
.stub(),
349 getHeader: sinon
.stub(),
350 getHeaders: sinon
.stub(),
353 pathsByLengthOrig
= dingus
.pathsByLength
;
354 sinon
.spy(dingus
, 'handlerMethodNotAllowed');
355 sinon
.spy(dingus
, 'handlerNotFound');
356 sinon
.spy(dingus
, 'handlerBadRequest');
357 sinon
.spy(dingus
, 'handlerInternalServerError');
358 stubHandler
= sinon
.stub();
360 afterEach(function () {
361 dingus
.pathsByLength
= pathsByLengthOrig
;
364 it('calls handler', async
function () {
365 const urlPath
= '/:id';
366 const method
= 'GET';
367 dingus
.on(method
, urlPath
, stubHandler
);
371 await dingus
.dispatch(req
, res
, ctx
);
372 assert(stubHandler
.called
);
373 assert(!dingus
.handlerMethodNotAllowed
.called
);
374 assert(!dingus
.handlerNotFound
.called
);
376 it('calls handler without context', async
function () {
377 const urlPath
= '/:id';
378 const method
= 'GET';
379 dingus
.on(method
, urlPath
, stubHandler
);
383 await dingus
.dispatch(req
, res
);
384 assert(stubHandler
.called
);
385 assert(!dingus
.handlerMethodNotAllowed
.called
);
386 assert(!dingus
.handlerNotFound
.called
);
388 it('calls fallback handler', async
function () {
389 const urlPath
= '/abc/:id';
390 const method
= 'GET';
391 dingus
.on('*', urlPath
, stubHandler
);
392 req
.url
= '/abc/def';
395 await dingus
.dispatch(req
, res
, ctx
);
396 assert(stubHandler
.called
);
397 assert(!dingus
.handlerMethodNotAllowed
.called
);
398 assert(!dingus
.handlerNotFound
.called
);
400 it('handles error in handler', async
function () {
401 const urlPath
= '/:id';
402 const method
= 'GET';
403 dingus
.on(method
, urlPath
, stubHandler
);
406 stubHandler
.rejects(new Error('blah'));
408 await dingus
.dispatch(req
, res
, ctx
);
409 assert(stubHandler
.called
);
410 assert(!dingus
.handlerMethodNotAllowed
.called
);
411 assert(!dingus
.handlerNotFound
.called
);
413 it('calls unsupported method', async
function () {
414 const urlPath
= '/:id';
415 const method
= 'POST';
416 dingus
.on('GET', urlPath
, stubHandler
);
420 await dingus
.dispatch(req
, res
, ctx
);
421 assert(!stubHandler
.called
);
422 assert(dingus
.handlerMethodNotAllowed
.called
);
423 assert(!dingus
.handlerNotFound
.called
);
425 it('does not lookup nonexistent path', async
function () {
426 req
.url
= '/foo/bar';
429 await dingus
.dispatch(req
, res
, ctx
);
430 assert(!stubHandler
.called
);
431 assert(!dingus
.handlerMethodNotAllowed
.called
);
432 assert(dingus
.handlerNotFound
.called
);
434 it('covers unhandled dingus exception', async
function () {
435 const expectedException
= new DingusError('blah');
436 sinon
.stub(dingus
.router
, 'lookup').throws(expectedException
);
438 await dingus
.dispatch(req
, res
, ctx
);
439 assert(!stubHandler
.called
);
440 assert(dingus
.handlerInternalServerError
.called
);
442 it('covers other exception', async
function () {
443 const expectedException
= new Error('blah');
444 sinon
.stub(dingus
.router
, 'lookup').throws(expectedException
);
446 await dingus
.dispatch(req
, res
, ctx
);
447 assert(!stubHandler
.called
);
448 assert(dingus
.handlerInternalServerError
.called
);
450 it('covers bad uri', async
function () {
453 await dingus
.dispatch(req
, res
, ctx
);
454 assert(dingus
.handlerBadRequest
.called
);
456 it('calls handler with additional arguments', async
function () {
457 dingus
.on('GET', '/', stubHandler
, 'foo', 'bar');
458 await dingus
.dispatch(req
, res
, ctx
);
459 assert(stubHandler
.called
);
460 assert
.strictEqual(stubHandler
.args
[0][3], 'foo');
461 assert
.strictEqual(stubHandler
.args
[0][4], 'bar');
465 describe('parseBody', function () {
467 beforeEach(function () {
470 it('does not parse unknown type', function () {
472 dingus
.parseBody('unknown/type', ctx
);
473 assert
.fail(noExpectedException
);
475 assert
.strictEqual(e
.statusCode
, 415);
478 it('parses json', function () {
479 const src
= { foo: 'bar' };
480 const rawBody
= JSON
.stringify(src
);
481 dingus
.parseBody(Enum
.ContentType
.ApplicationJson
, ctx
, rawBody
);
482 assert
.deepStrictEqual(ctx
.parsedBody
, src
);
484 it('handles unparsable json', function () {
485 const rawBody
= 'not json';
487 dingus
.parseBody(Enum
.ContentType
.ApplicationJson
, ctx
, rawBody
);
488 assert
.fail(noExpectedException
);
490 assert
.strictEqual(e
.statusCode
, 400);
493 it('parses form', function () {
494 const expected
= Object
.assign(Object
.create(null), {
497 const rawBody
= 'foo=bar';
498 dingus
.parseBody('application/x-www-form-urlencoded', ctx
, rawBody
);
499 assert
.deepStrictEqual(ctx
.parsedBody
, expected
);
504 describe('bodyData', function () {
506 beforeEach(function () {
509 // eslint-disable-next-line security/detect-object-injection
510 on: (ev
, fn
) => resEvents
[ev
] = fn
,
513 it('provides data', async
function () {
514 const p
= dingus
.bodyData(res
);
515 resEvents
['data'](Buffer
.from('foo'));
516 resEvents
['data'](Buffer
.from('bar'));
518 const result
= await p
;
519 assert
.strictEqual(result
, 'foobar');
521 it('handles error', async
function () {
522 const p
= dingus
.bodyData(res
);
523 resEvents
['error']('foo');
526 assert
.fail(noExpectedException
);
528 assert
.strictEqual(e
, 'foo');
531 it('limits size', async
function () {
532 const p
= dingus
.bodyData(res
, 8);
533 resEvents
['data'](Buffer
.from('foobar'));
534 resEvents
['data'](Buffer
.from('bazquux'));
537 assert
.fail(noExpectedException
);
539 assert
.strictEqual(e
.statusCode
, 413);
542 it('provides buffer', async
function () {
543 const p
= dingus
.bodyData(res
, 0, false);
544 const expected
= Buffer
.from('bleat');
545 resEvents
['data'](expected
);
547 const result
= await p
;
548 assert
.deepStrictEqual(result
, expected
);
552 describe('ingestBody', function () {
553 it('ingests json', async
function () {
557 sinon
.stub(dingus
, 'bodyData').resolves('{"foo":"bar"}');
558 sinon
.stub(Dingus
, 'getRequestContentType').returns(Enum
.ContentType
.ApplicationJson
);
559 await dingus
.ingestBody(req
, res
, ctx
);
560 assert
.deepStrictEqual(ctx
.parsedBody
, { foo: 'bar' });
561 assert
.deepStrictEqual(ctx
.rawBody
, undefined);
563 it('persists rawBody', async
function () {
567 const body
= '{"foo":"bar"}';
568 sinon
.stub(dingus
, 'bodyData').resolves(body
);
569 sinon
.stub(Dingus
, 'getRequestContentType').returns(Enum
.ContentType
.ApplicationJson
);
570 await dingus
.ingestBody(req
, res
, ctx
, { persistRawBody: true });
571 assert
.deepStrictEqual(ctx
.parsedBody
, { foo: 'bar' });
572 assert
.deepStrictEqual(ctx
.rawBody
, body
);
574 it('skips parsing empty body', async
function () {
579 sinon
.stub(dingus
, 'bodyData').resolves(body
);
580 sinon
.stub(Dingus
, 'getRequestContentType').returns(Enum
.ContentType
.ApplicationJson
);
581 sinon
.spy(dingus
, 'parseBody');
582 await dingus
.ingestBody(req
, res
, ctx
, { parseEmptyBody: false });
583 assert
.deepStrictEqual(ctx
.parsedBody
, undefined);
584 assert(dingus
.parseBody
.notCalled
);
588 describe('setResponseType', function () {
590 let _sa
; // Preserve strictAccept
592 _sa
= dingus
.strictAccept
;
595 dingus
.strictAccept
= _sa
;
597 beforeEach(function () {
601 setHeader: sinon
.stub(),
603 sinon
.stub(Dingus
, 'getResponseContentType').returns();
605 it('rejects missing', function () {
606 dingus
.strictAccept
= true;
608 dingus
.setResponseType(['my/type'], req
, res
, ctx
);
609 assert
.fail(noExpectedException
);
611 assert
.strictEqual(e
.statusCode
, 406, 'did not get expected status code');
614 it('accepts missing', function () {
615 dingus
.strictAccept
= false;
616 dingus
.setResponseType(['my/type'], req
, res
, ctx
);
617 assert
.strictEqual(ctx
.responseType
, 'my/type');
620 }); // setResponseType
622 describe('_readFileInfo', function () {
623 let stat
, data
, statRes
, dataRes
, filename
;
624 beforeEach(function () {
625 sinon
.stub(fs
.promises
, 'stat');
626 sinon
.stub(fs
.promises
, 'readFile');
628 mtimeMs:1612553697186,
631 filename
= 'dummy.txt';
633 it('succeeds', async
function () {
634 fs
.promises
.stat
.resolves(statRes
);
635 fs
.promises
.readFile
.resolves('data');
636 [stat
, data
] = await dingus
._readFileInfo(filename
);
637 assert
.deepStrictEqual(stat
, statRes
);
638 assert
.deepStrictEqual(data
, dataRes
);
640 it('returns null for non-existant file', async
function () {
644 fs
.promises
.stat
.rejects(noEnt
);
645 fs
.promises
.readFile
.rejects(noEnt
);
646 [stat
, data
] = await dingus
._readFileInfo(filename
);
647 assert
.strictEqual(stat
, null);
648 assert
.strictEqual(data
, null);
650 it('throws unexpected error', async
function () {
651 const expectedException
= new Error('blah');
652 fs
.promises
.stat
.rejects(expectedException
);
653 await assert
.rejects(async () => {
654 await dingus
._readFileInfo(filename
);
655 }, expectedException
);
659 describe('_serveFileMetaHeaders', function () {
660 let res
, directory
, fileName
;
661 beforeEach(function () {
662 sinon
.stub(dingus
, '_readFileInfo');
664 setHeader: sinon
.stub(),
667 fileName
= 'filename';
669 it('covers no meta file', async
function() {
670 dingus
._readFileInfo
.resolves([null, null]);
671 await dingus
._serveFileMetaHeaders(res
, directory
, fileName
);
672 assert(!res
.setHeader
.called
);
674 it('adds extra headers', async
function () {
675 dingus
._readFileInfo
.resolves([{}, Buffer
.from(`Link: <https://example.com/>; rel="relation"
676 X-Folded-Header: data
679 Content-Type: image/sgi
681 await dingus
._serveFileMetaHeaders(res
, directory
, fileName
);
682 assert(res
.setHeader
.called
);
684 }); // _serveFileMetaHeaders
686 describe('serveFile', function () {
687 const path
= require('path');
688 let ctx
, req
, res
, directory
, fileName
, filestats
;
689 beforeEach(function () {
690 directory
= path
.join(__dirname
, '..', 'test-data');
691 fileName
= 'example.html';
695 [Enum
.Header
.Accept
]: undefined,
696 [Enum
.Header
.IfModifiedSince
]: undefined,
697 [Enum
.Header
.AcceptEncoding
]: undefined,
698 [Enum
.Header
.IfNoneMatch
]: undefined,
700 getHeader: (header
) => {
701 if (header
in req
._headers
) {
702 // eslint-disable-next-line security/detect-object-injection
703 return req
._headers
[header
];
705 assert
.fail(`unexpected getHeader ${header}`);
710 getHeader: sinon
.stub(),
711 getHeaders: sinon
.stub(),
712 hasHeader: sinon
.stub().returns(true),
713 setHeader: sinon
.stub(),
726 atimeMs: 1613253436842.815,
727 mtimeMs: 1603485933192.861,
728 ctimeMs: 1603485933192.861,
730 atime: '2021-02-13T21:57:16.843Z',
731 mtime: '2020-10-23T13:45:33.193Z',
732 ctime: '2020-10-23T13:45:33.193Z',
733 birthtime: '1970-01-01T00:00:00.000Z',
735 sinon
.stub(dingus
, 'handlerNotFound');
736 sinon
.stub(fs
.promises
, 'stat').resolves(filestats
);
737 sinon
.spy(fs
.promises
, 'readFile');
739 it('serves a file', async
function () {
740 await dingus
.serveFile(req
, res
, ctx
, directory
, fileName
);
741 assert(fs
.promises
.readFile
.called
);
742 assert(!dingus
.handlerNotFound
.called
);
744 it('covers no meta headers', async
function () {
745 dingus
.staticMetadata
= false;
746 await dingus
.serveFile(req
, res
, ctx
, directory
, fileName
);
747 assert(fs
.promises
.readFile
.called
);
748 assert(!dingus
.handlerNotFound
.called
);
750 it('does not serve dot-file', async
function () {
751 fileName
= '.example';
752 await dingus
.serveFile(req
, res
, ctx
, directory
, fileName
);
753 assert(!fs
.promises
.readFile
.called
);
754 assert(dingus
.handlerNotFound
.called
);
756 it('does not serve encoded navigation', async
function () {
757 fileName
= '/example.html';
758 await dingus
.serveFile(req
, res
, ctx
, directory
, fileName
);
759 assert(!fs
.promises
.readFile
.called
);
760 assert(dingus
.handlerNotFound
.called
);
762 it('does not serve missing file', async
function () {
763 fileName
= 'no-file.here';
764 await dingus
.serveFile(req
, res
, ctx
, directory
, fileName
);
765 assert(dingus
.handlerNotFound
.called
);
767 it('requires directory be specified', async
function () {
768 await dingus
.serveFile(req
, res
, ctx
, '', fileName
);
769 assert(!fs
.promises
.readFile
.called
);
770 assert(dingus
.handlerNotFound
.called
);
772 it('covers fs error', async
function () {
773 const expectedException
= new Error('blah');
774 fs
.promises
.stat
.restore();
775 sinon
.stub(fs
.promises
, 'stat').rejects(expectedException
);
777 await dingus
.serveFile(req
, res
, ctx
, directory
, fileName
);
778 assert
.fail('should have thrown');
780 assert
.strictEqual(e
, expectedException
);
783 it('caches by modified', async
function () {
784 req
._headers
[Enum
.Header
.IfModifiedSince
] = 'Fri, 23 Oct 2020 23:11:16 GMT';
785 await dingus
.serveFile(req
, res
, ctx
, directory
, fileName
);
786 assert
.strictEqual(res
.statusCode
, 304);
788 it('does not cache old modified', async
function () {
789 req
._headers
[Enum
.Header
.IfModifiedSince
] = 'Fri, 23 Oct 2020 01:11:16 GMT';
790 await dingus
.serveFile(req
, res
, ctx
, directory
, fileName
);
791 assert
.notStrictEqual(res
.statusCode
, 304);
792 assert(!dingus
.handlerNotFound
.called
);
794 it('caches ETag match', async
function () {
795 req
._headers
[Enum
.Header
.IfNoneMatch
] = '"zPPQVfXV36sgXq4fRLdsm+7rRMb8IUfb/eJ6N6mnwWs"';
796 await dingus
.serveFile(req
, res
, ctx
, directory
, fileName
);
797 assert
.strictEqual(res
.statusCode
, 304);
799 it('does not cache ETag non-match', async
function () {
800 req
._headers
[Enum
.Header
.IfNoneMatch
] = '"foo", "bar"';
801 await dingus
.serveFile(req
, res
, ctx
, directory
, fileName
);
802 assert
.notStrictEqual(res
.statusCode
, 304);
803 assert(!dingus
.handlerNotFound
.called
);
805 it('handles no possible encodings', async
function () {
806 req
._headers
[Enum
.Header
.AcceptEncoding
] = '*;q=0';
807 await assert
.rejects(async () => {
808 await dingus
.serveFile(req
, res
, ctx
, directory
, fileName
);
810 name: 'ResponseError',
813 it('handles a valid encoding', async
function () {
814 req
._headers
[Enum
.Header
.AcceptEncoding
] = 'gzip';
815 await dingus
.serveFile(req
, res
, ctx
, directory
, fileName
);
816 assert(res
.end
.called
);
818 it('handles a valid encoding among others', async
function () {
819 req
._headers
[Enum
.Header
.AcceptEncoding
] = 'flarp, br, gzip';
820 fs
.promises
.stat
.restore();
821 sinon
.stub(fs
.promises
, 'stat')
822 .onCall(0).resolves(filestats
) // identity file
823 .onCall(1).resolves(null) // br encoding
824 .onCall(2).resolves(filestats
); // gzip encoding
825 await dingus
.serveFile(req
, res
, ctx
, directory
, fileName
);
826 assert(res
.end
.called
);
828 it('handles misconfigured encoding', async
function () {
829 Enum
.EncodingType
.Flarp
= 'flarp';
830 req
._headers
[Enum
.Header
.AcceptEncoding
] = 'flarp, gzip';
831 await dingus
.serveFile(req
, res
, ctx
, directory
, fileName
);
832 delete Enum
.EncodingType
.Flarp
;
833 assert(res
.end
.called
);
837 describe('renderError', function () {
839 beforeEach(function () {
843 details: 'hunkydorey',
846 it('renders unknown type', function () {
847 const contentType
= 'unknown/type';
848 const result
= dingus
.renderError(contentType
, err
);
849 assert
.deepStrictEqual(result
, 'OK\r\nhunkydorey');
851 it('renders text', function () {
852 const contentType
= 'text/plain';
853 const result
= dingus
.renderError(contentType
, err
);
854 assert
.deepStrictEqual(result
, 'OK\r\nhunkydorey');
856 it('renders json', function () {
857 const contentType
= Enum
.ContentType
.ApplicationJson
;
858 const result
= dingus
.renderError(contentType
, err
);
859 assert
.deepStrictEqual(result
, JSON
.stringify(err
));
861 it('renders html without details', function () {
864 errorMessage: 'Created',
866 const contentType
= 'text/html';
867 const result
= dingus
.renderError(contentType
, err
);
868 assert
.deepStrictEqual(result
, `<!DOCTYPE html>
871 <title>${err.statusCode} ${err.errorMessage}</title>
874 <h1>${err.errorMessage}</h1>
878 it('renders html', function () {
879 const contentType
= 'text/html';
880 const result
= dingus
.renderError(contentType
, err
);
881 assert
.deepStrictEqual(result
, `<!DOCTYPE html>
884 <title>${err.statusCode} ${err.errorMessage}</title>
887 <h1>${err.errorMessage}</h1>
888 <p>${err.details}</p>
892 it('renders html, multiple details', function () {
893 const contentType
= 'text/html';
894 err
.details
= ['one detail', 'two detail'];
895 const result
= dingus
.renderError(contentType
, err
);
896 assert
.deepStrictEqual(result
, `<!DOCTYPE html>
899 <title>${err.statusCode} ${err.errorMessage}</title>
902 <h1>${err.errorMessage}</h1>
910 describe('sendErrorResponse', function () {
912 beforeEach(function () {
917 getHeader: sinon
.stub(),
918 getHeaders: sinon
.stub(),
919 hasHeader: sinon
.stub().returns(true),
920 setHeader: sinon
.stub(),
922 sinon
.stub(dingus
, 'renderError');
924 it('covers', function () {
928 dingus
.sendErrorResponse(err
, req
, res
, ctx
);
929 assert(res
.end
.called
);
931 }); // sendErrorResponse
933 describe('proxyPrefix', function () {
934 let req
, res
, ctx
, stubHandler
, pfxDingus
;
937 beforeEach(function () {
938 pfxDingus
= new Dingus(console
, { proxyPrefix: pfx
});
940 setHeader: sinon
.stub(),
941 getHeader: sinon
.stub(),
946 setHeader: sinon
.stub(),
947 getHeader: sinon
.stub(),
950 sinon
.stub(pfxDingus
, 'handlerMethodNotAllowed');
951 sinon
.stub(pfxDingus
, 'handlerNotFound');
952 stubHandler
= sinon
.stub();
954 afterEach(function () {
958 it('handles prefixed route', async
function () {
959 const urlPath
= '/:id';
960 const method
= 'GET';
961 pfxDingus
.on(method
, urlPath
, stubHandler
);
962 req
.url
= pfx
+ '/abc';
965 await pfxDingus
.dispatch(req
, res
, ctx
);
966 assert(stubHandler
.called
);
967 assert(!pfxDingus
.handlerMethodNotAllowed
.called
);
968 assert(!pfxDingus
.handlerNotFound
.called
);
970 it('does not handle prefixed route', async
function () {
971 const urlPath
= '/:id';
972 const method
= 'GET';
973 pfxDingus
.on(method
, urlPath
, stubHandler
);
974 req
.url
= '/wrongpfx/abc';
977 await pfxDingus
.dispatch(req
, res
, ctx
);
978 assert(!stubHandler
.called
);
979 assert(!pfxDingus
.handlerMethodNotAllowed
.called
);
980 assert(pfxDingus
.handlerNotFound
.called
);
984 describe('handlerRedirect', function () {
986 beforeEach(function () {
988 getHeader: sinon
.stub(),
991 setHeader: sinon
.stub(),
996 it('covers', async
function () {
997 await dingus
.handlerRedirect(req
, res
, ctx
);
998 assert(res
.setHeader
.called
);
999 assert(res
.end
.called
);
1001 it('covers non-defaults', async
function () {
1002 await dingus
.handlerRedirect(req
, res
, ctx
, 308);
1003 assert(res
.setHeader
.called
);
1004 assert(res
.end
.called
);
1006 }); // handlerRedirect
1008 describe('handlerGetStaticFile', function () {
1010 beforeEach(function () {
1012 getHeader: sinon
.stub(),
1015 setHeader: sinon
.stub(),
1022 sinon
.stub(dingus
, 'serveFile');
1024 it('covers', async
function () {
1025 await dingus
.handlerGetStaticFile(req
, res
, ctx
);
1026 assert(dingus
.serveFile
.called
);
1028 it('covers specified file', async
function () {
1029 await dingus
.handlerGetStaticFile(req
, res
, ctx
, 'file.txt');
1030 assert(dingus
.serveFile
.called
);
1032 }); // handlerGetStaticFile