Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding TemplateValidator. * Renaming test-amp-ad-templates to test-amp-ad-template-helper. * Stash * Adding tests. * Moved variables tocloser to where they're actually used. * NON_AMP case fix. * lint * Header renamed. * Actually renamed header this time. * Supporting two header names + whitelisted for presubmit. * Undoing lint fix to unrelated file.
- Loading branch information
Showing
10 changed files
with
357 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/** | ||
* Copyright 2018 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { | ||
AdResponseType, | ||
Validator, | ||
ValidatorResult, | ||
} from './amp-ad-type-defs'; | ||
import {AmpAdTemplateHelper} from '../../amp-a4a/0.1/amp-ad-template-helper'; | ||
import {Services} from '../../../src/services'; | ||
import {getAmpAdMetadata} from './amp-ad-utils'; | ||
import {pushIfNotExist} from '../../../src/utils/array'; | ||
import {tryParseJson} from '../../../src/json'; | ||
import {utf8Decode} from '../../../src/utils/bytes'; | ||
|
||
/** @const {string} */ | ||
export const AMP_TEMPLATED_CREATIVE_HEADER_NAME = 'AMP-Ad-Template-Extension'; | ||
export const DEPRECATED_AMP_TEMPLATED_CREATIVE_HEADER_NAME = | ||
'AMP-template-amp-creative'; | ||
|
||
/** {?AmpAdTemplateHelper} */ | ||
let ampAdTemplateHelper; | ||
|
||
/** | ||
* Returns the global template helper. | ||
* @param {!Window} win | ||
* @return {!AmpAdTemplateHelper} | ||
* @visibleForTesting | ||
*/ | ||
export function getAmpAdTemplateHelper(win) { | ||
return ampAdTemplateHelper || | ||
(ampAdTemplateHelper = new AmpAdTemplateHelper(win)); | ||
} | ||
|
||
/** | ||
* Validator for Template ads. | ||
*/ | ||
export class TemplateValidator extends Validator { | ||
/** @override */ | ||
validate(context, unvalidatedBytes, headers) { | ||
|
||
const body = utf8Decode(/** @type {!ArrayBuffer} */ (unvalidatedBytes)); | ||
|
||
// If we're missing the relevant header, or headers altogether, we cannot | ||
// proceed. In this case, we return a NON_AMP response, since we cannot | ||
// ensure this template will be valid AMP. We will pass the body of the | ||
// response as the creative, and downstream renderers may attempt to render | ||
// it as a non-AMP creative within a cross-domain iframe. | ||
if (!headers || | ||
(headers.get(AMP_TEMPLATED_CREATIVE_HEADER_NAME) !== 'amp-mustache' && | ||
headers.get(DEPRECATED_AMP_TEMPLATED_CREATIVE_HEADER_NAME) !== | ||
'amp-mustache')) { | ||
return Promise.resolve( | ||
/** @type {!./amp-ad-type-defs.ValidatorOutput} */ ({ | ||
creativeData: { | ||
creative: body, | ||
}, | ||
adResponseType: AdResponseType.TEMPLATE, | ||
type: ValidatorResult.NON_AMP, | ||
})); | ||
} | ||
|
||
const parsedResponseBody = | ||
/** @type {!./amp-ad-type-defs.AmpTemplateCreativeDef} */ ( | ||
tryParseJson(body) || {}); | ||
return getAmpAdTemplateHelper() | ||
.fetch(parsedResponseBody.templateUrl) | ||
.then(template => { | ||
const creativeMetadata = getAmpAdMetadata(template); | ||
if (parsedResponseBody.analytics) { | ||
pushIfNotExist( | ||
creativeMetadata['customElementExtensions'], 'amp-analytics'); | ||
} | ||
pushIfNotExist( | ||
creativeMetadata['customElementExtensions'], 'amp-mustache'); | ||
|
||
const extensions = Services.extensionsFor(context.win); | ||
creativeMetadata.customElementExtensions.forEach( | ||
extensionId => extensions./*OK*/preloadExtension(extensionId)); | ||
// TODO(levitzky) Add preload logic for fonts / images. | ||
return Promise.resolve( | ||
/** @type {!./amp-ad-type-defs.ValidatorOutput} */ ({ | ||
creativeData: { | ||
templateData: parsedResponseBody, | ||
creativeMetadata, | ||
}, | ||
adResponseType: AdResponseType.TEMPLATE, | ||
type: ValidatorResult.AMP, | ||
})); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/** | ||
* Copyright 2018 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as sinon from 'sinon'; | ||
import { | ||
AMP_TEMPLATED_CREATIVE_HEADER_NAME, | ||
DEPRECATED_AMP_TEMPLATED_CREATIVE_HEADER_NAME, | ||
TemplateValidator, | ||
getAmpAdTemplateHelper, | ||
} from '../template-validator'; | ||
import {AdResponseType, ValidatorResult} from '../amp-ad-type-defs'; | ||
import {data} from './testdata/valid_css_at_rules_amp.reserialized'; | ||
import {utf8Encode} from '../../../../src/utils/bytes'; | ||
|
||
const realWinConfig = { | ||
amp: {}, | ||
ampAdCss: true, | ||
allowExternalResources: true, | ||
}; | ||
|
||
describes.realWin('TemplateValidator', realWinConfig, env => { | ||
|
||
const templateUrl = 'https://adnetwork.com/amp-template.html'; | ||
const headers = { | ||
get: name => { | ||
if (name == AMP_TEMPLATED_CREATIVE_HEADER_NAME) { | ||
return 'amp-mustache'; | ||
} | ||
}, | ||
}; | ||
let validator; | ||
|
||
beforeEach(() => { | ||
validator = new TemplateValidator(); | ||
}); | ||
|
||
describe('AMP Result', () => { | ||
|
||
let sandbox; | ||
let validatorPromise; | ||
|
||
beforeEach(() => { | ||
sandbox = sinon.sandbox.create(); | ||
sandbox.stub(getAmpAdTemplateHelper(env.win), 'fetch').callsFake(url => { | ||
expect(url).to.equal(templateUrl); | ||
return Promise.resolve(data.adTemplate); | ||
}); | ||
|
||
validatorPromise = validator.validate({win: env.win}, | ||
utf8Encode(JSON.stringify({ | ||
templateUrl, | ||
data: {url: 'https://buy.com/buy-1'}, | ||
analytics: {foo: 'bar'}, | ||
})), headers); | ||
}); | ||
|
||
afterEach(() => sandbox.restore()); | ||
|
||
it('should have AMP validator result', () => { | ||
return validatorPromise.then(validatorOutput => { | ||
expect(validatorOutput).to.be.ok; | ||
expect(validatorOutput.type).to.equal(ValidatorResult.AMP); | ||
}); | ||
}); | ||
|
||
it('should have AMP validator result w/ deprecated header name', () => { | ||
validator.validate({win: env.win}, | ||
utf8Encode(JSON.stringify({ | ||
templateUrl, | ||
data: {url: 'https://buy.com/buy-1'}, | ||
analytics: {foo: 'bar'}, | ||
})), { | ||
get: name => { | ||
if (name == DEPRECATED_AMP_TEMPLATED_CREATIVE_HEADER_NAME) { | ||
return 'amp-mustache'; | ||
} | ||
}, | ||
}).then(validatorOutput => { | ||
expect(validatorOutput).to.be.ok; | ||
expect(validatorOutput.type).to.equal(ValidatorResult.AMP); | ||
}); | ||
}); | ||
|
||
it('should have TEMPLATE ad response type', () => { | ||
return validatorPromise.then(validatorOutput => { | ||
expect(validatorOutput).to.be.ok; | ||
expect(validatorOutput.adResponseType).to.equal( | ||
AdResponseType.TEMPLATE); | ||
}); | ||
}); | ||
|
||
it('should have creativeData with minified creative in metadata', () => { | ||
return validatorPromise.then(validatorOutput => { | ||
expect(validatorOutput).to.be.ok; | ||
expect(validatorOutput.creativeData).to.be.ok; | ||
const {creativeMetadata} = validatorOutput.creativeData; | ||
expect(creativeMetadata.minifiedCreative) | ||
.to.equal(data.minifiedTemplateCreative); | ||
}); | ||
}); | ||
|
||
it('should have amp-analytics and mustache in customElementExtensions', | ||
() => { | ||
return validatorPromise.then(validatorOutput => { | ||
expect(validatorOutput).to.be.ok; | ||
expect(validatorOutput.creativeData).to.be.ok; | ||
const {creativeMetadata} = validatorOutput.creativeData; | ||
expect(creativeMetadata.customElementExtensions) | ||
.to.deep.equal(['amp-analytics', 'amp-mustache']); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Non-AMP Result', () => { | ||
it('should have NON_AMP validator result due to lack of headers', () => { | ||
return validator.validate({win: env.win}, | ||
utf8Encode(JSON.stringify({ | ||
templateUrl, | ||
data: {url: 'https://buy.com/buy-1'}, | ||
analytics: {foo: 'bar'}, | ||
}))).then(validatorOutput => { | ||
expect(validatorOutput).to.be.ok; | ||
expect(validatorOutput.type).to.equal(ValidatorResult.NON_AMP); | ||
}); | ||
}); | ||
|
||
it('should have NON_AMP validator result due to lack of mustache header', | ||
() => { | ||
return validator.validate({win: env.win}, | ||
utf8Encode(JSON.stringify({ | ||
templateUrl, | ||
data: {url: 'https://buy.com/buy-1'}, | ||
analytics: {foo: 'bar'}, | ||
})), | ||
{ | ||
get: () => null, | ||
}).then(validatorOutput => { | ||
expect(validatorOutput).to.be.ok; | ||
expect(validatorOutput.type).to.equal(ValidatorResult.NON_AMP); | ||
}); | ||
}); | ||
|
||
it('should have TEMPLATE ad response type', () => { | ||
return validator.validate({win: env.win}, | ||
utf8Encode(JSON.stringify({ | ||
templateUrl, | ||
data: {url: 'https://buy.com/buy-1'}, | ||
analytics: {foo: 'bar'}, | ||
}))).then(validatorOutput => { | ||
expect(validatorOutput).to.be.ok; | ||
expect(validatorOutput.adResponseType) | ||
.to.equal(AdResponseType.TEMPLATE); | ||
}); | ||
}); | ||
|
||
it('should have the response body as the creative in creativeData', () => { | ||
return validator.validate({win: env.win}, | ||
utf8Encode(JSON.stringify({templateUrl})), | ||
{ | ||
get: () => null, | ||
}).then(validatorOutput => { | ||
expect(validatorOutput).to.be.ok; | ||
expect(validatorOutput.creativeData).to.be.ok; | ||
const {creativeData} = validatorOutput; | ||
expect(creativeData).to.be.ok; | ||
expect(creativeData.creative).to.deep.equal( | ||
'{"templateUrl":"https://adnetwork.com/amp-template.html"}'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
Oops, something went wrong.