File tree Expand file tree Collapse file tree 2 files changed +15
-2
lines changed
Expand file tree Collapse file tree 2 files changed +15
-2
lines changed Original file line number Diff line number Diff 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 ) {
Original file line number Diff line number Diff 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
106117describe ( 'RateLimiter' , ( ) => {
You can’t perform that action at this time.
0 commit comments