X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=test%2Flib%2Fcommon.js;h=68f2599791c12faf5d106656200fd23cb7c55947;hb=356f9a3b45d5b6bbcc310a2b37b6f59dfd76b923;hp=bacd6be3d54d2aaa7e6b019c98e0e664050392dc;hpb=4778ea0b65e1f22f3d85cfa8bad0e1b29f87b7d3;p=squeep-api-dingus diff --git a/test/lib/common.js b/test/lib/common.js index bacd6be..68f2599 100644 --- a/test/lib/common.js +++ b/test/lib/common.js @@ -1,27 +1,12 @@ -/* eslint-disable capitalized-comments */ -/* eslint-env mocha */ 'use strict'; -const assert = require('assert'); -const sinon = require('sinon'); // eslint-disable-line node/no-unpublished-require +const assert = require('node:assert'); +const sinon = require('sinon'); const common = require('../../lib/common'); describe('common', function () { - describe('fileScope', function () { - it('names a file path', function () { - const filename = 'lib/foo/bar.js'; - const result = common.fileScope(filename)('baz'); - assert.strictEqual(result, 'bar:baz'); - }); - it('names an index path', function () { - const filename = 'lib/foo/index.js'; - const result = common.fileScope(filename)('baz'); - assert.strictEqual(result, 'foo:baz'); - }); - }); // fileScope - describe('generateETag', function () { it('generates a tag from data', function () { const expected = '"RHUvNyculE/SyROjU0LqzN0arxibrlBnazAashP8UGE"'; @@ -128,74 +113,6 @@ describe('common', function () { }); }); // pick - describe('requestLogData', function () { - it('gives data', function () { - const req = { - method: 'GET', - somethingElse: 'blah', - }; - const result = common.requestLogData(req); - assert.deepStrictEqual(result, { - method: 'GET', - }); - }); - }); // requestLogData - - describe('obscureAuthorizationHeader', function () { - it('obscures basic data', function () { - const authHeader = 'Basic Zm9vOmJhcg=='; - const expected = 'Basic ************'; - const result = common.obscureAuthorizationHeader(authHeader); - assert.strictEqual(result, expected); - }); - it('obscures all of other types', function () { - const authHeader = 'someWeirdAuth'; - const expected = '*************'; - const result = common.obscureAuthorizationHeader(authHeader); - assert.strictEqual(result, expected); - }); - it('does nothing when empty', function () { - const authHeader = undefined; - const expected = undefined; - const result = common.obscureAuthorizationHeader(authHeader); - assert.strictEqual(result, expected); - }); - }); // obscureAuthorizationHeader - - describe('scrubHeaderObject', function () { - it('', function () { - const data = { - headers: { - 'foo': 'bar', - 'authorization': 'Basic Zm9vOmJhcg==', - }, - }; - const expected = { - headers: { - 'foo': 'bar', - 'authorization': 'Basic ************', - }, - }; - common.scrubHeaderObject(data); - assert.deepStrictEqual(data, expected); - }); - }); // scrubHeaderObject - - describe('responseLogData', function () { - it('gives data', function () { - const res = { - getHeaders: () => ({}), - statusCode: 200, - blah: 'blah', - }; - const result = common.responseLogData(res); - assert.deepStrictEqual(result, { - headers: {}, - statusCode: 200, - }); - }); - }); // responseLogData - describe('setOptions', function () { it('sets options', function () { const expected = { @@ -298,13 +215,6 @@ describe('common', function () { }); }); // requestId - describe('ensureLoggerLevels', function () { - it('adds missing levels', function () { - const result = common.ensureLoggerLevels(); - assert.deepStrictEqual(result, common.nullLogger); - }); - }); // ensureLoggerLevels - describe('httpStatusCodeClass', function () { it('works', function () { for (const [statusCode, statusClassExpected] of Object.entries({ @@ -439,4 +349,41 @@ describe('common', function () { }); }); // unfoldHeaderLines + describe('addCookie', function () { + let res, name, value; + beforeEach(function () { + res = { + appendHeader: sinon.stub(), + }; + name = 'someCookieName'; + value = 'someCookieValue'; + }); + it('covers no options', function () { + common.addCookie(res, name, value, {}); + assert(res.appendHeader.called); + }); + it('covers all options', function () { + common.addCookie(res, name, value, { + domain: 'example.com', + expires: new Date(), + httpOnly: true, + maxAge: 9999999, + path: '/foo', + sameSite: 'Lax', + secure: true, + }); + assert(res.appendHeader.called); + }); + it('covers invalid expires', function () { + assert.throws(() => common.addCookie(res, name, value, { expires: 'never' }), TypeError); + }); + it('covers invalid sameSite', function () { + assert.throws(() => common.addCookie(res, name, value, { sameSite: 'Whatever' })); + }); + it('covers no options', function () { + common.addCookie(res, name, value); + assert(res.appendHeader.called); + }); + }); // addCookie + });