New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds TemplateValidator #14584
Merged
Merged
Adds TemplateValidator #14584
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
148978a
Adding TemplateValidator.
glevitzky a123e46
Renaming test-amp-ad-templates to test-amp-ad-template-helper.
glevitzky 59a6368
Stash
glevitzky f7534ac
Adding tests.
glevitzky faee6bb
Moved variables tocloser to where they're actually used.
glevitzky a600a3b
NON_AMP case fix.
glevitzky 23e9c0a
lint
glevitzky 422a42c
Header renamed.
glevitzky 17cfabc
Actually renamed header this time.
glevitzky 1072414
Supporting two header names + whitelisted for presubmit.
glevitzky ade946b
Undoing lint fix to unrelated file.
glevitzky File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I mentioned, can we accept both headers for now? So later we can easily migrate AdZerk over.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Sorry about the confusion.