|
| 1 | +const expect = require('expect.js'); |
| 2 | +const sinon = require('sinon'); |
| 3 | +const https = require('https'); |
| 4 | +const { EventEmitter } = require('events'); |
| 5 | +const cloudinary = require('../../lib/cloudinary'); |
| 6 | +const createTestConfig = require('../testUtils/createTestConfig'); |
| 7 | + |
| 8 | +describe('debug mode', function () { |
| 9 | + let requestStub; |
| 10 | + let mockResponse; |
| 11 | + |
| 12 | + beforeEach(function () { |
| 13 | + cloudinary.config(createTestConfig()); |
| 14 | + |
| 15 | + // Create a mock response that extends EventEmitter |
| 16 | + mockResponse = new EventEmitter(); |
| 17 | + mockResponse.statusCode = 200; |
| 18 | + mockResponse.headers = { |
| 19 | + 'x-request-id': 'test-request-id-12345678', |
| 20 | + 'x-featureratelimit-limit': '500', |
| 21 | + 'x-featureratelimit-reset': new Date().toUTCString(), |
| 22 | + 'x-featureratelimit-remaining': '499' |
| 23 | + }; |
| 24 | + |
| 25 | + // Stub the https.request method |
| 26 | + requestStub = sinon.stub(https, 'request').callsFake(function (options, callback) { |
| 27 | + // Call the callback with our mock response |
| 28 | + setTimeout(() => callback(mockResponse), 0); |
| 29 | + |
| 30 | + // Return a mock request object |
| 31 | + const mockRequest = new EventEmitter(); |
| 32 | + mockRequest.write = sinon.stub(); |
| 33 | + mockRequest.end = function () { |
| 34 | + // Simulate response data after end() is called |
| 35 | + setTimeout(() => { |
| 36 | + mockResponse.emit('data', JSON.stringify({ status: 'ok' })); |
| 37 | + mockResponse.emit('end'); |
| 38 | + }, 10); |
| 39 | + }; |
| 40 | + mockRequest.setTimeout = sinon.stub(); |
| 41 | + |
| 42 | + return mockRequest; |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(function () { |
| 47 | + requestStub.restore(); |
| 48 | + }); |
| 49 | + |
| 50 | + describe('when debug mode is enabled', function () { |
| 51 | + beforeEach(function () { |
| 52 | + cloudinary.config({ debug: true }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should include request_id in successful responses', function (done) { |
| 56 | + cloudinary.v2.api.ping() |
| 57 | + .then((result) => { |
| 58 | + expect(result.request_id).to.be('test-request-id-12345678'); |
| 59 | + expect(result.status).to.be('ok'); |
| 60 | + done(); |
| 61 | + }) |
| 62 | + .catch(done); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should include request_id in error responses', function (done) { |
| 66 | + // Override the mock response to simulate an error |
| 67 | + mockResponse.statusCode = 404; |
| 68 | + requestStub.restore(); |
| 69 | + |
| 70 | + requestStub = sinon.stub(https, 'request').callsFake(function (options, callback) { |
| 71 | + setTimeout(() => callback(mockResponse), 0); |
| 72 | + |
| 73 | + const mockRequest = new EventEmitter(); |
| 74 | + mockRequest.write = sinon.stub(); |
| 75 | + mockRequest.end = function () { |
| 76 | + setTimeout(() => { |
| 77 | + mockResponse.emit('data', JSON.stringify({ |
| 78 | + error: { |
| 79 | + message: 'Resource not found' |
| 80 | + } |
| 81 | + })); |
| 82 | + mockResponse.emit('end'); |
| 83 | + }, 10); |
| 84 | + }; |
| 85 | + mockRequest.setTimeout = sinon.stub(); |
| 86 | + |
| 87 | + return mockRequest; |
| 88 | + }); |
| 89 | + |
| 90 | + cloudinary.v2.api.resource('nonexistent').catch((error) => { |
| 91 | + expect(error.error.request_id).to.be('test-request-id-12345678'); |
| 92 | + expect(error.error.message).to.be('Resource not found'); |
| 93 | + expect(error.error.http_code).to.be(404); |
| 94 | + done(); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should include request_id even when X-Request-Id header has different casing', function (done) { |
| 99 | + // Test case insensitivity (Node.js lowercases headers) |
| 100 | + mockResponse.headers = { |
| 101 | + 'x-request-id': 'case-insensitive-id' |
| 102 | + }; |
| 103 | + |
| 104 | + cloudinary.v2.api.ping() |
| 105 | + .then((result) => { |
| 106 | + expect(result.request_id).to.be('case-insensitive-id'); |
| 107 | + done(); |
| 108 | + }) |
| 109 | + .catch(done); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('when debug mode is disabled', function () { |
| 114 | + beforeEach(function () { |
| 115 | + cloudinary.config({ debug: false }); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should NOT include request_id in successful responses', function (done) { |
| 119 | + cloudinary.v2.api.ping() |
| 120 | + .then((result) => { |
| 121 | + expect(result.request_id).to.be(undefined); |
| 122 | + expect(result.status).to.be('ok'); |
| 123 | + done(); |
| 124 | + }) |
| 125 | + .catch(done); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should NOT include request_id in error responses', function (done) { |
| 129 | + mockResponse.statusCode = 404; |
| 130 | + requestStub.restore(); |
| 131 | + |
| 132 | + requestStub = sinon.stub(https, 'request').callsFake(function (options, callback) { |
| 133 | + setTimeout(() => callback(mockResponse), 0); |
| 134 | + |
| 135 | + const mockRequest = new EventEmitter(); |
| 136 | + mockRequest.write = sinon.stub(); |
| 137 | + mockRequest.end = function () { |
| 138 | + setTimeout(() => { |
| 139 | + mockResponse.emit('data', JSON.stringify({ |
| 140 | + error: { |
| 141 | + message: 'Resource not found' |
| 142 | + } |
| 143 | + })); |
| 144 | + mockResponse.emit('end'); |
| 145 | + }, 10); |
| 146 | + }; |
| 147 | + mockRequest.setTimeout = sinon.stub(); |
| 148 | + |
| 149 | + return mockRequest; |
| 150 | + }); |
| 151 | + |
| 152 | + cloudinary.v2.api.resource('nonexistent').catch((error) => { |
| 153 | + expect(error.error.request_id).to.be(undefined); |
| 154 | + expect(error.error.message).to.be('Resource not found'); |
| 155 | + done(); |
| 156 | + }); |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + describe('when X-Request-Id header is missing', function () { |
| 161 | + beforeEach(function () { |
| 162 | + cloudinary.config({ debug: true }); |
| 163 | + // Remove the x-request-id header |
| 164 | + delete mockResponse.headers['x-request-id']; |
| 165 | + }); |
| 166 | + |
| 167 | + it('should not break when request_id is missing from headers', function (done) { |
| 168 | + cloudinary.v2.api.ping() |
| 169 | + .then((result) => { |
| 170 | + expect(result.request_id).to.be(undefined); |
| 171 | + expect(result.status).to.be('ok'); |
| 172 | + done(); |
| 173 | + }) |
| 174 | + .catch(done); |
| 175 | + }); |
| 176 | + }); |
| 177 | + |
| 178 | + describe('config option', function () { |
| 179 | + it('should accept debug option in config', function () { |
| 180 | + cloudinary.config({ debug: true }); |
| 181 | + expect(cloudinary.config('debug')).to.be(true); |
| 182 | + |
| 183 | + cloudinary.config({ debug: false }); |
| 184 | + expect(cloudinary.config('debug')).to.be(false); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should be undefined when not explicitly set', function () { |
| 188 | + const testConfig = createTestConfig(); |
| 189 | + delete testConfig.debug; // Ensure debug is not in the config |
| 190 | + cloudinary.config(testConfig); |
| 191 | + // When not set, it should be undefined (falsy) |
| 192 | + expect(cloudinary.config('debug')).to.not.be.ok(); |
| 193 | + }); |
| 194 | + }); |
| 195 | +}); |
0 commit comments