Skip to content

Commit 1ed93f6

Browse files
authored
Merge pull request #7793 from BitGo/aloe/fixUserAgentCheck
fix: browser check for user agent
2 parents e67bac3 + d4bd0aa commit 1ed93f6

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

modules/sdk-api/src/bitgoAPI.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ export class BitGoAPI implements BitGoBase {
413413
// prevent IE from caching requests
414414
req.set('If-Modified-Since', 'Mon, 26 Jul 1997 05:00:00 GMT');
415415

416-
if (!(process as any).browser && this._userAgent) {
416+
if (typeof window === 'undefined' && this._userAgent) {
417417
// If not in the browser, set the User-Agent. Browsers don't allow
418418
// setting of User-Agent, so we must disable this when run in the
419419
// browser (browserify sets process.browser).

modules/sdk-api/test/unit/bitgoAPI.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)