|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { CreateChargebee } from '../src/createChargebee.js'; |
| 3 | +import { Environment } from '../src/environment.js'; |
| 4 | + |
| 5 | +let capturedRequests: Request[] = []; |
| 6 | +let responseFactory: ((attempt: number) => Response) | null = null; |
| 7 | +let callCount = 0; |
| 8 | + |
| 9 | +const mockHttpClient = { |
| 10 | + makeApiRequest: async (request: Request): Promise<Response> => { |
| 11 | + capturedRequests.push(request.clone()); |
| 12 | + const attempt = callCount++; |
| 13 | + if (responseFactory) { |
| 14 | + return responseFactory(attempt); |
| 15 | + } |
| 16 | + return new Response(JSON.stringify({ list: [], next_offset: null }), { |
| 17 | + status: 200, |
| 18 | + headers: { 'Content-Type': 'application/json' }, |
| 19 | + }); |
| 20 | + }, |
| 21 | +}; |
| 22 | + |
| 23 | +const Chargebee = CreateChargebee(mockHttpClient); |
| 24 | + |
| 25 | +function createChargebee(conf: Record<string, any> = {}) { |
| 26 | + return new (Chargebee as any)({ |
| 27 | + site: 'test-site', |
| 28 | + apiKey: 'test-api-key', |
| 29 | + ...conf, |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +beforeEach(() => { |
| 34 | + capturedRequests = []; |
| 35 | + responseFactory = null; |
| 36 | + callCount = 0; |
| 37 | +}); |
| 38 | + |
| 39 | +describe('RequestWrapper - request headers', () => { |
| 40 | + describe('User-Agent header', () => { |
| 41 | + it('should set User-Agent to Chargebee-NodeJs-Client with clientVersion only when __clientIdentifier() is not called', async () => { |
| 42 | + const chargebee = createChargebee(); |
| 43 | + await chargebee.customer.list(); |
| 44 | + |
| 45 | + const userAgent = capturedRequests[0].headers.get('User-Agent'); |
| 46 | + expect(userAgent).to.equal( |
| 47 | + `Chargebee-NodeJs-Client ${Environment.clientVersion}`, |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should append service name with semicolon after calling chargebee.__clientIdentifier()', async () => { |
| 52 | + const chargebee = createChargebee(); |
| 53 | + chargebee.__clientIdentifier('local-test-suffix'); |
| 54 | + |
| 55 | + await chargebee.customer.list(); |
| 56 | + |
| 57 | + const userAgent = capturedRequests[0].headers.get('User-Agent'); |
| 58 | + expect(userAgent).to.equal( |
| 59 | + `Chargebee-NodeJs-Client ${Environment.clientVersion};local-test-suffix`, |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should reflect updated service name if chargebee.__clientIdentifier() is called again', async () => { |
| 64 | + const chargebee = createChargebee(); |
| 65 | + chargebee.__clientIdentifier('first-service'); |
| 66 | + chargebee.__clientIdentifier('second-service'); |
| 67 | + |
| 68 | + await chargebee.customer.list(); |
| 69 | + |
| 70 | + const userAgent = capturedRequests[0].headers.get('User-Agent'); |
| 71 | + expect(userAgent).to.equal( |
| 72 | + `Chargebee-NodeJs-Client ${Environment.clientVersion};second-service`, |
| 73 | + ); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('Authorization header', () => { |
| 78 | + it('should set Authorization as Basic base64(apiKey:)', async () => { |
| 79 | + const chargebee = createChargebee({ apiKey: 'test-key-123' }); |
| 80 | + await chargebee.customer.list(); |
| 81 | + |
| 82 | + const expected = |
| 83 | + 'Basic ' + Buffer.from('test-key-123:').toString('base64'); |
| 84 | + expect(capturedRequests[0].headers.get('Authorization')).to.equal( |
| 85 | + expected, |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should include the trailing colon in the base64-encoded value', async () => { |
| 90 | + const chargebee = createChargebee({ apiKey: 'my_secret_key' }); |
| 91 | + await chargebee.customer.list(); |
| 92 | + |
| 93 | + const raw = capturedRequests[0].headers |
| 94 | + .get('Authorization')! |
| 95 | + .replace('Basic ', ''); |
| 96 | + const decoded = Buffer.from(raw, 'base64').toString('utf-8'); |
| 97 | + expect(decoded).to.equal('my_secret_key:'); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('Accept header', () => { |
| 102 | + it('should set Accept to application/json', async () => { |
| 103 | + const chargebee = createChargebee(); |
| 104 | + await chargebee.customer.list(); |
| 105 | + |
| 106 | + expect(capturedRequests[0].headers.get('Accept')).to.equal( |
| 107 | + 'application/json', |
| 108 | + ); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('Content-Type header', () => { |
| 113 | + it('should set Content-Type to application/x-www-form-urlencoded for POST requests', async () => { |
| 114 | + responseFactory = () => |
| 115 | + new Response(JSON.stringify({ customer: { id: 'cust_123' } }), { |
| 116 | + status: 200, |
| 117 | + headers: { 'Content-Type': 'application/json' }, |
| 118 | + }); |
| 119 | + |
| 120 | + const chargebee = createChargebee(); |
| 121 | + await chargebee.customer.create({ first_name: 'John' }); |
| 122 | + |
| 123 | + expect(capturedRequests[0].headers.get('Content-Type')).to.equal( |
| 124 | + 'application/x-www-form-urlencoded; charset=utf-8', |
| 125 | + ); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should set Content-Type to application/x-www-form-urlencoded for GET requests', async () => { |
| 129 | + const chargebee = createChargebee(); |
| 130 | + await chargebee.customer.list(); |
| 131 | + |
| 132 | + expect(capturedRequests[0].headers.get('Content-Type')).to.equal( |
| 133 | + 'application/x-www-form-urlencoded; charset=utf-8', |
| 134 | + ); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('Lang-Version header', () => { |
| 139 | + it('should set Lang-Version to the current Node.js process.version', async () => { |
| 140 | + const chargebee = createChargebee(); |
| 141 | + await chargebee.customer.list(); |
| 142 | + |
| 143 | + expect(capturedRequests[0].headers.get('Lang-Version')).to.equal( |
| 144 | + process.version, |
| 145 | + ); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + describe('X-CB-Retry-Attempt header', () => { |
| 150 | + it('should NOT include X-CB-Retry-Attempt on the first attempt', async () => { |
| 151 | + const chargebee = createChargebee(); |
| 152 | + await chargebee.customer.list(); |
| 153 | + |
| 154 | + expect( |
| 155 | + capturedRequests[0].headers.get('X-CB-Retry-Attempt'), |
| 156 | + ).to.be.null; |
| 157 | + }); |
| 158 | + |
| 159 | + it('should set X-CB-Retry-Attempt to "1" on the first retry', async () => { |
| 160 | + responseFactory = (attempt) => { |
| 161 | + if (attempt === 0) { |
| 162 | + return new Response( |
| 163 | + JSON.stringify({ http_status_code: 500, message: 'server error' }), |
| 164 | + { status: 500, headers: { 'Content-Type': 'application/json' } }, |
| 165 | + ); |
| 166 | + } |
| 167 | + return new Response(JSON.stringify({ list: [] }), { |
| 168 | + status: 200, |
| 169 | + headers: { 'Content-Type': 'application/json' }, |
| 170 | + }); |
| 171 | + }; |
| 172 | + |
| 173 | + const chargebee = createChargebee({ |
| 174 | + retryConfig: { enabled: true, maxRetries: 2, delayMs: 0, retryOn: [500] }, |
| 175 | + }); |
| 176 | + await chargebee.customer.list(); |
| 177 | + |
| 178 | + expect(capturedRequests.length).to.equal(2); |
| 179 | + expect( |
| 180 | + capturedRequests[0].headers.get('X-CB-Retry-Attempt'), |
| 181 | + ).to.be.null; |
| 182 | + expect(capturedRequests[1].headers.get('X-CB-Retry-Attempt')).to.equal( |
| 183 | + '1', |
| 184 | + ); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should increment X-CB-Retry-Attempt on each subsequent retry', async () => { |
| 188 | + responseFactory = (attempt) => { |
| 189 | + if (attempt < 2) { |
| 190 | + return new Response( |
| 191 | + JSON.stringify({ http_status_code: 500, message: 'server error' }), |
| 192 | + { status: 500, headers: { 'Content-Type': 'application/json' } }, |
| 193 | + ); |
| 194 | + } |
| 195 | + return new Response(JSON.stringify({ list: [] }), { |
| 196 | + status: 200, |
| 197 | + headers: { 'Content-Type': 'application/json' }, |
| 198 | + }); |
| 199 | + }; |
| 200 | + |
| 201 | + const chargebee = createChargebee({ |
| 202 | + retryConfig: { enabled: true, maxRetries: 3, delayMs: 0, retryOn: [500] }, |
| 203 | + }); |
| 204 | + await chargebee.customer.list(); |
| 205 | + |
| 206 | + expect(capturedRequests.length).to.equal(3); |
| 207 | + expect(capturedRequests[0].headers.get('X-CB-Retry-Attempt')).to.be.null; |
| 208 | + expect(capturedRequests[1].headers.get('X-CB-Retry-Attempt')).to.equal('1'); |
| 209 | + expect(capturedRequests[2].headers.get('X-CB-Retry-Attempt')).to.equal('2'); |
| 210 | + }); |
| 211 | + }); |
| 212 | +}); |
0 commit comments