Skip to content

Commit

Permalink
A4A and 3P safari tests fixes (ampproject#6161)
Browse files Browse the repository at this point in the history
  • Loading branch information
aghassemi authored Nov 14, 2016
1 parent 37bdea8 commit 5e96ade
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
15 changes: 10 additions & 5 deletions extensions/amp-a4a/0.1/test/test-a4a-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
data as validCSSAmp,
} from './testdata/valid_css_at_rules_amp.reserialized';
import {installDocService} from '../../../../src/service/ampdoc-impl';
import {FetchResponseHeaders} from '../../../../src/service/xhr-impl';
import {adConfig} from '../../../../ads/_config';
import {a4aRegistry} from '../../../../ads/_a4a-config';
import {
Expand Down Expand Up @@ -68,6 +69,7 @@ describe('integration test: a4a', () => {
let fixture;
let mockResponse;
let a4aElement;
let headers;
beforeEach(() => {
sandbox = sinon.sandbox.create();
xhrMock = sandbox.stub(Xhr.prototype, 'fetch');
Expand All @@ -76,9 +78,14 @@ describe('integration test: a4a', () => {
return utf8Encode(validCSSAmp.reserialized);
},
bodyUsed: false,
headers: new Headers(),
headers: new FetchResponseHeaders({
getResponseHeader(name) {
return headers[name];
},
}),
};
mockResponse.headers.append(SIGNATURE_HEADER, validCSSAmp.signature);
headers = {};
headers[SIGNATURE_HEADER] = validCSSAmp.signature;
xhrMock.withArgs(TEST_URL, {
mode: 'cors',
method: 'GET',
Expand Down Expand Up @@ -113,7 +120,7 @@ describe('integration test: a4a', () => {
});

it('should fall back to 3p when no signature is present', () => {
mockResponse.headers.delete(SIGNATURE_HEADER);
delete headers[SIGNATURE_HEADER];
return fixture.addElement(a4aElement).then(unusedElement => {
expectRenderedInXDomainIframe(a4aElement, TEST_URL);
});
Expand Down Expand Up @@ -187,5 +194,3 @@ describe('integration test: a4a', () => {
// to whom.
it('should propagate errors out and report them to upstream error log');
});


27 changes: 17 additions & 10 deletions extensions/amp-a4a/0.1/test/test-amp-a4a.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from './testdata/valid_css_at_rules_amp.reserialized';
import {data as testFragments} from './testdata/test_fragments';
import {installDocService} from '../../../../src/service/ampdoc-impl';
import {FetchResponseHeaders} from '../../../../src/service/xhr-impl';
import {base64UrlDecodeToBytes} from '../../../../src/utils/base64';
import {utf8Encode} from '../../../../src/utils/bytes';
import {resetScheduledElementForTesting} from '../../../../src/custom-element';
Expand Down Expand Up @@ -71,6 +72,7 @@ describe('amp-a4a', () => {
let getSigningServiceNamesMock;
let viewerWhenVisibleMock;
let mockResponse;
let headers;

beforeEach(() => {
sandbox = sinon.sandbox.create();
Expand All @@ -90,10 +92,15 @@ describe('amp-a4a', () => {
return utf8Encode(validCSSAmp.reserialized);
},
bodyUsed: false,
headers: new Headers(),
headers: new FetchResponseHeaders({
getResponseHeader(name) {
return headers[name];
},
}),
catch: callback => callback(),
};
mockResponse.headers.append(SIGNATURE_HEADER, validCSSAmp.signature);
headers = {};
headers[SIGNATURE_HEADER] = validCSSAmp.signature;
});

afterEach(() => {
Expand Down Expand Up @@ -160,9 +167,9 @@ describe('amp-a4a', () => {
it('for SafeFrame rendering case', () => {
verifyNonAMPRender(a4a);
// Make sure there's no signature, so that we go down the 3p iframe path.
mockResponse.headers.delete(SIGNATURE_HEADER);
delete headers[SIGNATURE_HEADER];
// If rendering type is safeframe, we SHOULD attach a SafeFrame.
mockResponse.headers.append(RENDERING_TYPE_HEADER, 'safeframe');
headers[RENDERING_TYPE_HEADER] = 'safeframe';
fixture.doc.body.appendChild(a4aElement);
a4a.onLayoutMeasure();
return a4a.layoutCallback().then(() => {
Expand All @@ -178,7 +185,7 @@ describe('amp-a4a', () => {
it('for cached content iframe rendering case', () => {
verifyNonAMPRender(a4a);
// Make sure there's no signature, so that we go down the 3p iframe path.
mockResponse.headers.delete(SIGNATURE_HEADER);
delete headers[SIGNATURE_HEADER];
fixture.doc.body.appendChild(a4aElement);
a4a.onLayoutMeasure();
return a4a.layoutCallback().then(() => {
Expand Down Expand Up @@ -212,9 +219,9 @@ describe('amp-a4a', () => {

it('should attach a SafeFrame when header is set', () => {
// Make sure there's no signature, so that we go down the 3p iframe path.
mockResponse.headers.delete(SIGNATURE_HEADER);
delete headers[SIGNATURE_HEADER];
// If rendering type is safeframe, we SHOULD attach a SafeFrame.
mockResponse.headers.append(RENDERING_TYPE_HEADER, 'safeframe');
headers[RENDERING_TYPE_HEADER] = 'safeframe';
xhrMock.withArgs(TEST_URL, {
mode: 'cors',
method: 'GET',
Expand Down Expand Up @@ -246,10 +253,10 @@ describe('amp-a4a', () => {
['', 'client_cache', 'some_random_thing'].forEach(headerVal => {
it(`should not attach a SafeFrame when header is ${headerVal}`, () => {
// Make sure there's no signature, so that we go down the 3p iframe path.
mockResponse.headers.delete(SIGNATURE_HEADER);
delete headers[SIGNATURE_HEADER];
// If rendering type is anything but safeframe, we SHOULD NOT attach a
// SafeFrame.
mockResponse.headers.append(RENDERING_TYPE_HEADER, headerVal);
headers[RENDERING_TYPE_HEADER] = headerVal;
xhrMock.withArgs(TEST_URL, {
mode: 'cors',
method: 'GET',
Expand Down Expand Up @@ -284,7 +291,7 @@ describe('amp-a4a', () => {
it('should not use SafeFrame if creative is A4A', () => {
// Set safeframe header, but it should be ignored when a signature
// exists and validates.
mockResponse.headers.append(RENDERING_TYPE_HEADER, 'safeframe');
headers[RENDERING_TYPE_HEADER] = 'safeframe';
xhrMock.withArgs(TEST_URL, {
mode: 'cors',
method: 'GET',
Expand Down
11 changes: 10 additions & 1 deletion src/service/xhr-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,9 @@ export class FetchResponse {

/**
* Provides access to the response headers as defined in the Fetch API.
* @private Visible for testing.
*/
class FetchResponseHeaders {
export class FetchResponseHeaders {
/**
* @param {!XMLHttpRequest|!XDomainRequest} xhr
*/
Expand All @@ -534,6 +535,14 @@ class FetchResponseHeaders {
get(name) {
return this.xhr_.getResponseHeader(name);
}

/**
* @param {string} name
* @return {boolean}
*/
has(name) {
return this.xhr_.getResponseHeader(name) != null;
}
}


Expand Down
9 changes: 8 additions & 1 deletion test/functional/test-3p-frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,14 @@ describe('3p-frame', () => {
const srcParts = src.split('#');
expect(srcParts[0]).to.equal(
'http://ads.localhost:9876/dist.3p/current/frame.max.html');
expect(JSON.parse(srcParts[1])).to.deep.equal(JSON.parse(fragment));
const expectedFragment = JSON.parse(srcParts[1]);
const parsedFragment = JSON.parse(fragment);
// Since DOM fingerprint changes between browsers and documents, to have
// stable tests, we can only verify its existence.
expect(expectedFragment._context.domFingerprint).to.exist;
delete expectedFragment._context.domFingerprint;
delete parsedFragment._context.domFingerprint;
expect(expectedFragment).to.deep.equal(parsedFragment);

// Switch to same origin for inner tests.
iframe.src = '/dist.3p/current/frame.max.html#' + fragment;
Expand Down

0 comments on commit 5e96ade

Please sign in to comment.