diff --git a/spec/vast_parser.spec.js b/spec/vast_parser.spec.js index ed7ca1d9..0d763c28 100644 --- a/spec/vast_parser.spec.js +++ b/spec/vast_parser.spec.js @@ -80,15 +80,24 @@ describe('VASTParser', () => { }); }); - it('applies url filters and saves url in parentURLs', () => { + it('applies url filters', (done) => { VastParser.URLTemplateFilters = [(url) => url.replace('foo', 'bar')]; - return VastParser.fetchVAST('www.foo.foo').finally(() => { - expect(VastParser.parentURLs).toEqual(['www.bar.foo']); + const urlHandlerSpy = jest.spyOn(VastParser.urlHandler, 'get'); + + VastParser.fetchVAST('www.foo.foo').then(() => { + expect(urlHandlerSpy).toHaveBeenCalledWith( + 'www.bar.foo', + expect.anything(), + expect.anything() + ); + done(); }); }); - it('emits VAST-resolving and VAST-resolved events', () => { + it('emits VAST-resolving and VAST-resolved events with filtered url', () => { + VastParser.URLTemplateFilters = [(url) => url.replace('foo', 'bar')]; + return VastParser.fetchVAST( 'www.foo.foo', 2, @@ -96,7 +105,7 @@ describe('VASTParser', () => { ad ).finally(() => { expect(VastParser.emit).toHaveBeenNthCalledWith(1, 'VAST-resolving', { - url: 'www.foo.foo', + url: 'www.bar.foo', previousUrl: 'www.original.foo', wrapperDepth: 2, maxWrapperDepth: 8, @@ -105,7 +114,7 @@ describe('VASTParser', () => { }); expect(VastParser.emit).toHaveBeenNthCalledWith(2, 'VAST-resolved', { - url: 'www.foo.foo', + url: 'www.bar.foo', previousUrl: 'www.original.foo', wrapperDepth: 2, error: null, @@ -142,50 +151,47 @@ describe('VASTParser', () => { }); }); - it('applies url filters and saves url in parentURLs', () => { + it('applies url filters', () => { VastParser.URLTemplateFilters = [(url) => url.replace('foo', 'bar')]; - return VastParser.fetchVAST('www.foo.foo') - .then(() => { - expect(true).toBeFalsy(); - }) - .catch(() => { - expect(VastParser.parentURLs).toEqual(['www.bar.foo']); - }); + const urlHandlerSpy = jest.spyOn(VastParser.urlHandler, 'get'); + + return VastParser.fetchVAST('www.foo.foo').catch(() => { + expect(urlHandlerSpy).toHaveBeenCalledWith( + 'www.bar.foo', + expect.anything(), + expect.anything() + ); + }); }); - it('emits VAST-resolving and VAST-resolved events', () => { - return VastParser.fetchVAST('www.foo.foo', 2, 'www.original.foo', ad) - .then(() => { - expect(true).toBeFalsy(); - }) - .catch(() => { - expect(VastParser.emit).toHaveBeenNthCalledWith( - 1, - 'VAST-resolving', - { - url: 'www.foo.foo', - previousUrl: 'www.original.foo', - wrapperDepth: 2, - maxWrapperDepth: 8, - timeout: 120000, - wrapperAd: ad, - } - ); - - expect(VastParser.emit).toHaveBeenNthCalledWith( - 2, - 'VAST-resolved', - { - url: 'www.foo.foo', - previousUrl: 'www.original.foo', - wrapperDepth: 2, - error: new Error('timeout'), - duration: expect.any(Number), - statusCode: 408, - } - ); + it('emits VAST-resolving and VAST-resolved events with filtered url', () => { + VastParser.URLTemplateFilters = [(url) => url.replace('foo', 'bar')]; + + return VastParser.fetchVAST( + 'www.foo.foo', + 2, + 'www.original.foo', + ad + ).catch(() => { + expect(VastParser.emit).toHaveBeenNthCalledWith(1, 'VAST-resolving', { + url: 'www.bar.foo', + previousUrl: 'www.original.foo', + wrapperDepth: 2, + maxWrapperDepth: 8, + timeout: 120000, + wrapperAd: ad, }); + + expect(VastParser.emit).toHaveBeenNthCalledWith(2, 'VAST-resolved', { + url: 'www.bar.foo', + previousUrl: 'www.original.foo', + wrapperDepth: 2, + error: new Error('timeout'), + duration: expect.any(Number), + statusCode: 408, + }); + }); }); it('rejects with error', () => { @@ -213,7 +219,6 @@ describe('VASTParser', () => { expect(VastParser.rootURL).toBe(''); expect(VastParser.remainingAds).toEqual([]); - expect(VastParser.parentURLs).toEqual([]); expect(VastParser.errorURLTemplates).toEqual([]); expect(VastParser.rootErrorURLTemplates).toEqual([]); expect(VastParser.maxWrapperDepth).toBe(5); @@ -232,7 +237,6 @@ describe('VASTParser', () => { expect(VastParser.rootURL).toBe(''); expect(VastParser.remainingAds).toEqual([]); - expect(VastParser.parentURLs).toEqual([]); expect(VastParser.errorURLTemplates).toEqual([]); expect(VastParser.rootErrorURLTemplates).toEqual([]); expect(VastParser.maxWrapperDepth).toBe(10); @@ -832,7 +836,7 @@ describe('VASTParser', () => { wrapperBVastUrl, 1, wrapperAVastUrl, - adWithWrapper, + adWithWrapper ); expect(VastParser.parse).toHaveBeenCalledWith(expect.any(Object), { url: wrapperBVastUrl, diff --git a/src/parser/vast_parser.js b/src/parser/vast_parser.js index 8bfceac1..8dbf490e 100644 --- a/src/parser/vast_parser.js +++ b/src/parser/vast_parser.js @@ -28,7 +28,6 @@ export class VASTParser extends EventEmitter { super(); this.remainingAds = []; - this.parentURLs = []; this.errorURLTemplates = []; this.rootErrorURLTemplates = []; this.maxWrapperDepth = null; @@ -122,7 +121,6 @@ export class VASTParser extends EventEmitter { url = filter(url); }); - this.parentURLs.push(url); const timeBeforeGet = Date.now(); this.emit('VAST-resolving', { url, @@ -174,7 +172,6 @@ export class VASTParser extends EventEmitter { withCredentials: options.withCredentials, }; this.maxWrapperDepth = options.wrapperLimit || DEFAULT_MAX_WRAPPER_DEPTH; - this.parentURLs = []; this.parsingOptions = { allowMultipleAds: options.allowMultipleAds }; this.remainingAds = []; this.rootErrorURLTemplates = []; @@ -200,7 +197,6 @@ export class VASTParser extends EventEmitter { ? util.flatten(this.remainingAds) : this.remainingAds.shift(); this.errorURLTemplates = []; - this.parentURLs = []; return this.resolveAds(ads, { wrapperDepth: 0, @@ -449,7 +445,6 @@ export class VASTParser extends EventEmitter { resolveAds(ads = [], { wrapperDepth, previousUrl, url }) { const resolveWrappersPromises = []; previousUrl = url; - ads.forEach((ad) => { const resolveWrappersPromise = this.resolveWrappers( ad, @@ -494,11 +489,7 @@ export class VASTParser extends EventEmitter { delete ad.nextWrapperURL; return resolve(ad); } - - if ( - wrapperDepth >= this.maxWrapperDepth || - this.parentURLs.indexOf(ad.nextWrapperURL) !== -1 - ) { + if (wrapperDepth >= this.maxWrapperDepth) { // Wrapper limit reached, as defined by the video player. // Too many Wrapper responses have been received with no InLine response. ad.errorCode = 302;