@@ -408,6 +408,79 @@ describe('Constructor', function () {
408408 } ) ;
409409 } ) ;
410410
411+ describe ( 'User-Agent header based on environment' , function ( ) {
412+ afterEach ( function ( ) {
413+ nock . cleanAll ( ) ;
414+ sinon . restore ( ) ;
415+ } ) ;
416+
417+ it ( 'should set User-Agent header when running in Node.js (typeof window === undefined)' , async function ( ) {
418+ const bitgo = new BitGoAPI ( {
419+ env : 'custom' ,
420+ customRootURI : 'https://app.example.local' ,
421+ userAgent : 'TestAgent/1.0' ,
422+ } ) ;
423+
424+ // Ensure we're in a Node.js environment by verifying window is undefined
425+ ( typeof window ) . should . equal ( 'undefined' ) ;
426+
427+ const scope = nock ( 'https://app.example.local' )
428+ . get ( '/api/v1/ping' )
429+ . matchHeader ( 'User-Agent' , 'TestAgent/1.0' )
430+ . reply ( 200 , { status : 'ok' } ) ;
431+
432+ await bitgo . ping ( {
433+ reqId : {
434+ toString : ( ) => 'test-123' ,
435+ inc : ( ) => {
436+ /* mock */
437+ } ,
438+ } as any ,
439+ } ) ;
440+
441+ scope . isDone ( ) . should . be . true ( ) ;
442+ } ) ;
443+
444+ it ( 'should not set User-Agent header when running in browser (typeof window !== undefined)' , async function ( ) {
445+ // Mock the window object to simulate browser environment
446+ const windowStub = { location : 'mock' } ;
447+ ( global as any ) . window = windowStub ;
448+
449+ try {
450+ const bitgo = new BitGoAPI ( {
451+ env : 'custom' ,
452+ customRootURI : 'https://app.example.local' ,
453+ userAgent : 'TestAgent/1.0' ,
454+ } ) ;
455+
456+ const scope = nock ( 'https://app.example.local' )
457+ . get ( '/api/v1/ping' )
458+ . reply ( function ( ) {
459+ // Verify User-Agent header is NOT set to our custom value
460+ const userAgent = this . req . headers [ 'user-agent' ] ;
461+ if ( userAgent && userAgent . includes ( 'TestAgent/1.0' ) ) {
462+ throw new Error ( 'User-Agent should not be set in browser environment' ) ;
463+ }
464+ return [ 200 , { status : 'ok' } ] ;
465+ } ) ;
466+
467+ await bitgo . ping ( {
468+ reqId : {
469+ toString : ( ) => 'test-123' ,
470+ inc : ( ) => {
471+ /* mock */
472+ } ,
473+ } as any ,
474+ } ) ;
475+
476+ scope . isDone ( ) . should . be . true ( ) ;
477+ } finally {
478+ // Clean up the global window mock
479+ delete ( global as any ) . window ;
480+ }
481+ } ) ;
482+ } ) ;
483+
411484 describe ( 'constants parameter' , function ( ) {
412485 it ( 'should allow passing constants via options and expose via fetchConstants' , async function ( ) {
413486 const bitgo = new BitGoAPI ( {
0 commit comments