Skip to content

Commit f7db566

Browse files
gtamanahaalexs-mparticle
authored andcommitted
feat(reportingLogger): Introduce default user agent and URL handling in ReportingLogger, with corresponding tests for fallback behavior
1 parent 2db99dd commit f7db566

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/logging/reportingLogger.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export class ReportingLogger implements IReportingLogger {
1414
private readonly integration: string = 'mp-wsdk';
1515
private readonly rateLimiter: IRateLimiter;
1616
private readonly DEFAULT_ACCOUNT_ID: string = 'no-account-id-set';
17+
private readonly DEFAULT_USER_AGENT: string = 'no-user-agent-set';
18+
private readonly DEFAULT_URL: string = 'no-url-set';
1719

1820
constructor(
1921
private baseUrl: string,
@@ -97,11 +99,11 @@ export class ReportingLogger implements IReportingLogger {
9799
}
98100

99101
private getUrl(): string {
100-
return window.location.href;
102+
return window?.location?.href ?? this.DEFAULT_URL;
101103
}
102104

103105
private getUserAgent(): string {
104-
return window.navigator.userAgent;
106+
return window?.navigator?.userAgent ?? this.DEFAULT_USER_AGENT;
105107
}
106108

107109
private sendLogToServer(logRequest: LogRequest) {

test/jest/reportingLogger.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ describe('ReportingLogger', () => {
101101
const fetchCall = mockFetch.mock.calls[0];
102102
expect(fetchCall[1].headers['rokt-account-id']).toBe('no-account-id-set');
103103
});
104+
105+
it('uses default user agent when user agent is empty', () => {
106+
logger = new ReportingLogger(baseUrl, sdkVersion, accountId);
107+
delete (globalThis as any).navigator;
108+
delete (globalThis as any).location;
109+
logger.error('msg');
110+
expect(mockFetch).toHaveBeenCalled();
111+
const fetchCall = mockFetch.mock.calls[0];
112+
const body = JSON.parse(fetchCall[1].body);
113+
expect(body).toMatchObject({ deviceInfo: 'no-user-agent-set', url: 'no-url-set' });
114+
});
104115
});
105116

106117
describe('RateLimiter', () => {

0 commit comments

Comments
 (0)