|
2 | 2 |
|
3 | 3 | import OpenAI from 'openai';
|
4 | 4 | import { APIUserAbortError } from 'openai';
|
5 |
| -import { Headers } from 'openai/core'; |
| 5 | +import { debug, Headers } from 'openai/core'; |
6 | 6 | import defaultFetch, { Response, type RequestInit, type RequestInfo } from 'node-fetch';
|
7 | 7 |
|
8 | 8 | describe('instantiate client', () => {
|
@@ -424,3 +424,95 @@ describe('retries', () => {
|
424 | 424 | expect(count).toEqual(3);
|
425 | 425 | });
|
426 | 426 | });
|
| 427 | + |
| 428 | +describe('debug()', () => { |
| 429 | + const env = process.env; |
| 430 | + const spy = jest.spyOn(console, 'log'); |
| 431 | + |
| 432 | + beforeEach(() => { |
| 433 | + jest.resetModules(); |
| 434 | + process.env = { ...env }; |
| 435 | + process.env['DEBUG'] = 'true'; |
| 436 | + }); |
| 437 | + |
| 438 | + afterEach(() => { |
| 439 | + process.env = env; |
| 440 | + }); |
| 441 | + |
| 442 | + test('body request object with Authorization header', function () { |
| 443 | + // Test request body includes headers object with Authorization |
| 444 | + const headersTest = { |
| 445 | + headers: { |
| 446 | + Authorization: 'fakeAuthorization', |
| 447 | + }, |
| 448 | + }; |
| 449 | + debug('request', headersTest); |
| 450 | + expect(spy).toHaveBeenCalledWith('OpenAI:DEBUG:request', { |
| 451 | + headers: { |
| 452 | + Authorization: 'REDACTED', |
| 453 | + }, |
| 454 | + }); |
| 455 | + }); |
| 456 | + |
| 457 | + test('body request object with api-key header', function () { |
| 458 | + // Test request body includes headers object with api-ley |
| 459 | + const apiKeyTest = { |
| 460 | + headers: { |
| 461 | + 'api-key': 'fakeKey', |
| 462 | + }, |
| 463 | + }; |
| 464 | + debug('request', apiKeyTest); |
| 465 | + expect(spy).toHaveBeenCalledWith('OpenAI:DEBUG:request', { |
| 466 | + headers: { |
| 467 | + 'api-key': 'REDACTED', |
| 468 | + }, |
| 469 | + }); |
| 470 | + }); |
| 471 | + |
| 472 | + test('header object with Authorization header', function () { |
| 473 | + // Test headers object with authorization header |
| 474 | + const authorizationTest = { |
| 475 | + authorization: 'fakeValue', |
| 476 | + }; |
| 477 | + debug('request', authorizationTest); |
| 478 | + expect(spy).toHaveBeenCalledWith('OpenAI:DEBUG:request', { |
| 479 | + authorization: 'REDACTED', |
| 480 | + }); |
| 481 | + }); |
| 482 | + |
| 483 | + test('input args are not mutated', function () { |
| 484 | + const authorizationTest = { |
| 485 | + authorization: 'fakeValue', |
| 486 | + }; |
| 487 | + const client = new OpenAI({ |
| 488 | + baseURL: 'http://localhost:5000/', |
| 489 | + defaultHeaders: authorizationTest, |
| 490 | + apiKey: 'api-key', |
| 491 | + }); |
| 492 | + |
| 493 | + const { req } = client.buildRequest({ path: '/foo', method: 'post' }); |
| 494 | + debug('request', authorizationTest); |
| 495 | + expect((req.headers as Headers)['authorization']).toEqual('fakeValue'); |
| 496 | + expect(spy).toHaveBeenCalledWith('OpenAI:DEBUG:request', { |
| 497 | + authorization: 'REDACTED', |
| 498 | + }); |
| 499 | + }); |
| 500 | + |
| 501 | + test('input headers are not mutated', function () { |
| 502 | + const authorizationTest = { |
| 503 | + authorization: 'fakeValue', |
| 504 | + }; |
| 505 | + const client = new OpenAI({ |
| 506 | + baseURL: 'http://localhost:5000/', |
| 507 | + defaultHeaders: authorizationTest, |
| 508 | + apiKey: 'api-key', |
| 509 | + }); |
| 510 | + |
| 511 | + const { req } = client.buildRequest({ path: '/foo', method: 'post' }); |
| 512 | + debug('request', { headers: req.headers }); |
| 513 | + expect((req.headers as Headers)['authorization']).toEqual('fakeValue'); |
| 514 | + expect(spy).toHaveBeenCalledWith('OpenAI:DEBUG:request', { |
| 515 | + authorization: 'REDACTED', |
| 516 | + }); |
| 517 | + }); |
| 518 | +}); |
0 commit comments