From 63f9a33d5d279bd32ef2ffa1db80704ffa5e0e86 Mon Sep 17 00:00:00 2001 From: lannka Date: Tue, 25 Jul 2017 13:39:17 -0700 Subject: [PATCH 1/2] Fix wrong context.location in ampcontext --- 3p/ampcontext.js | 3 ++- test/integration/test-amp-ad-3p.js | 12 +++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/3p/ampcontext.js b/3p/ampcontext.js index 4908ad0f5393..a2d17c9c991a 100644 --- a/3p/ampcontext.js +++ b/3p/ampcontext.js @@ -21,6 +21,7 @@ import {nextTick} from './3p'; import {tryParseJson} from '../src/json'; import {isObject} from '../src/types'; import {AmpEvents} from '../src/amp-events'; +import {parseUrl} from '../src/url'; export class AbstractAmpContext { @@ -256,7 +257,7 @@ export class AbstractAmpContext { this.hidden = context.hidden; this.initialLayoutRect = context.initialLayoutRect; this.initialIntersection = context.initialIntersection; - this.location = context.location; + this.location = parseUrl(context.location.href); this.mode = context.mode; this.pageViewId = context.pageViewId; this.referrer = context.referrer; diff --git a/test/integration/test-amp-ad-3p.js b/test/integration/test-amp-ad-3p.js index 1111af02c7d8..43a17937e0f2 100644 --- a/test/integration/test-amp-ad-3p.js +++ b/test/integration/test-amp-ad-3p.js @@ -86,7 +86,17 @@ function createIframeWithApis(fixture) { expect(context.initialIntersection.rootBounds).to.be.defined; expect(context.isMaster).to.be.defined; expect(context.computeInMasterFrame).to.be.defined; - expect(context.location).to.be.defined; + expect(context.location).to.deep.equal({ + hash: '', + host: 'localhost:9876', + hostname: 'localhost', + href: 'http://localhost:9876/context.html', + origin: 'http://localhost:9876', + pathname: '/context.html', + port: '9876', + protocol: 'http:', + search: '', + }); expect(context.sourceUrl).to.be.a('string'); }).then(() => { // test iframe will send out render-start to amp-ad From e92ae1ebfbe55f296f555ea6884af1a564190622 Mon Sep 17 00:00:00 2001 From: lannka Date: Tue, 25 Jul 2017 15:55:17 -0700 Subject: [PATCH 2/2] fix test --- test/functional/test-amp-context.js | 60 +++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 12 deletions(-) diff --git a/test/functional/test-amp-context.js b/test/functional/test-amp-context.js index 4384286252e4..92ca925471b8 100644 --- a/test/functional/test-amp-context.js +++ b/test/functional/test-amp-context.js @@ -65,8 +65,18 @@ describe('3p ampcontext.js', () => { win.name = generateSerializedAttributes(); const context = new AmpContext(win); expect(context).to.be.ok; - expect(context.location).to.equal('foo.com'); - expect(context.canonicalUrl).to.equal('foo.com'); + expect(context.location).to.deep.equal({ + 'hash': '', + 'host': 'foo.com', + 'hostname': 'foo.com', + 'href': 'https://foo.com/a?b=c', + 'origin': 'https://foo.com', + 'pathname': '/a', + 'port': '', + 'protocol': 'https:', + 'search': '?b=c', + }); + expect(context.canonicalUrl).to.equal('https://bar.com'); expect(context.pageViewId).to.equal('1'); expect(context.sentinel).to.equal('1-291921'); expect(context.startTime).to.equal(0); @@ -77,8 +87,18 @@ describe('3p ampcontext.js', () => { win.name = generateSerializedAttributesA4A(); const context = new AmpContext(win); expect(context).to.be.ok; - expect(context.location).to.equal('foo.com'); - expect(context.canonicalUrl).to.equal('foo.com'); + expect(context.location).to.deep.equal({ + 'hash': '', + 'host': 'foo.com', + 'hostname': 'foo.com', + 'href': 'https://foo.com/a?b=c', + 'origin': 'https://foo.com', + 'pathname': '/a', + 'port': '', + 'protocol': 'https:', + 'search': '?b=c', + }); + expect(context.canonicalUrl).to.equal('https://bar.com'); expect(context.pageViewId).to.equal('1'); expect(context.sentinel).to.equal('1-291921'); expect(context.startTime).to.equal(0); @@ -89,8 +109,18 @@ describe('3p ampcontext.js', () => { win.AMP_CONTEXT_DATA = generateAttributes(); const context = new AmpContext(win); expect(context).to.be.ok; - expect(context.location).to.equal('foo.com'); - expect(context.canonicalUrl).to.equal('foo.com'); + expect(context.location).to.deep.equal({ + 'hash': '', + 'host': 'foo.com', + 'hostname': 'foo.com', + 'href': 'https://foo.com/a?b=c', + 'origin': 'https://foo.com', + 'pathname': '/a', + 'port': '', + 'protocol': 'https:', + 'search': '?b=c', + }); + expect(context.canonicalUrl).to.equal('https://bar.com'); expect(context.pageViewId).to.equal('1'); expect(context.sentinel).to.equal('1-291921'); expect(context.startTime).to.equal(0); @@ -323,8 +353,10 @@ function generateAttributes(opt_sentinel) { name.attributes = {}; const sentinel = opt_sentinel || '1-291921'; name.attributes._context = { - location: 'foo.com', - canonicalUrl: 'foo.com', + location: { + href: 'https://foo.com/a?b=c', + }, + canonicalUrl: 'https://bar.com', pageViewId: '1', sentinel, startTime: 0, @@ -342,8 +374,10 @@ function generateAttributesA4A(opt_sentinel) { const attributes = {}; const sentinel = opt_sentinel || '1-291921'; attributes._context = { - location: 'foo.com', - canonicalUrl: 'foo.com', + location: { + href: 'https://foo.com/a?b=c', + }, + canonicalUrl: 'https://bar.com', pageViewId: '1', sentinel, startTime: 0, @@ -358,8 +392,10 @@ function generateIncorrectAttributes() { const name = {}; name.attributes = {}; name.attributes.wrong = { - location: 'foo.com', - canonicalUrl: 'foo.com', + location: { + href: 'https://foo.com/a?b=c', + }, + canonicalUrl: 'https://foo.com', pageViewId: '1', sentinel: '1-291921', startTime: 0,