|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var real_debug = require('debug'); |
| 4 | + |
| 5 | +var debug = require('../../lib/utils/debug'); |
| 6 | + |
| 7 | +describe('utils/debug', function () { |
| 8 | + |
| 9 | + afterEach(function () { |
| 10 | + real_debug.enable(process.env.DEBUG || ''); |
| 11 | + }); |
| 12 | + |
| 13 | + describe('.exports.getStringValue', function () { |
| 14 | + it('should return a string or undefined', function () { |
| 15 | + expect(debug.getStringValue(true)).to.be.undefined; |
| 16 | + expect(debug.getStringValue(undefined)).to.be.undefined; |
| 17 | + expect(debug.getStringValue(null)).to.be.undefined; |
| 18 | + expect(debug.getStringValue(false)).to.be.undefined; |
| 19 | + expect(debug.getStringValue(1)).to.be.undefined; |
| 20 | + expect(debug.getStringValue(1.1)).to.be.undefined; |
| 21 | + expect(debug.getStringValue(-1)).to.be.undefined; |
| 22 | + expect(debug.getStringValue(-1.1)).to.be.undefined; |
| 23 | + |
| 24 | + expect(debug.getStringValue('abc')).to.be.a('string'); |
| 25 | + expect(debug.getStringValue(Buffer.from ? Buffer.from('abc') : new Buffer('abc'))).to.be.a('string'); |
| 26 | + expect(debug.getStringValue(new Date())).to.be.a('string'); |
| 27 | + expect(debug.getStringValue({ foo: { bar: 'qux' } })).to.be.a('string'); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + // describe('.exports.genRedactedString', function () { |
| 32 | + // it('should return a string, truncated if applicable', function () { |
| 33 | + // // TODO: the unit test underneath already tests the actual string truncation logic . |
| 34 | + // // .. so this one is not needed? |
| 35 | + // }); |
| 36 | + // }); |
| 37 | + |
| 38 | + describe('.exports', function () { |
| 39 | + it('should return a function', function () { |
| 40 | + expect(debug('test')).to.be.a('function'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should output to console if DEBUG is set', function () { |
| 44 | + var dbg_ns = 'ioredis:debugtest'; |
| 45 | + |
| 46 | + real_debug.enable(dbg_ns); |
| 47 | + |
| 48 | + var logspy = spy(); |
| 49 | + var fn = debug(dbg_ns); |
| 50 | + |
| 51 | + fn.log = logspy; |
| 52 | + |
| 53 | + expect(fn.enabled).to.equal(true); |
| 54 | + expect(fn.namespace).to.equal(dbg_ns); |
| 55 | + |
| 56 | + var data = [], i = 0; |
| 57 | + |
| 58 | + while (i < 1000) { |
| 59 | + data.push(String(i)); i += 1; |
| 60 | + } |
| 61 | + |
| 62 | + var datastr = JSON.stringify(data); |
| 63 | + |
| 64 | + fn('my message %s', { json: data }); |
| 65 | + expect(logspy.called).to.equal(true); |
| 66 | + |
| 67 | + var args = logspy.getCall(0).args; |
| 68 | + |
| 69 | + var wanted_arglen = 30 // " ... <REDACTED full-length="">" |
| 70 | + + debug.MAX_ARGUMENT_LENGTH // max-length of redacted string |
| 71 | + + datastr.length.toString().length; // length of string of string length (inception much?) |
| 72 | + |
| 73 | + expect(args.length).to.be.above(1); |
| 74 | + expect(args[1]).to.be.a('string'); |
| 75 | + expect(args[1].length).to.equal(wanted_arglen); |
| 76 | + }); |
| 77 | + }); |
| 78 | +}); |
0 commit comments