Skip to content

Commit

Permalink
✨Add a hash parameter to integrate WebAssembly Validator (#33941)
Browse files Browse the repository at this point in the history
* Add hash parameter `#validate=wasm`

* Lint
  • Loading branch information
antiphoton committed Apr 22, 2021
1 parent 5c5a5cf commit f72e31c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/validator-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,20 @@ export function maybeValidate(win) {
// Should only happen in tests.
return;
}
let validator = false;
let validatorUrl = null;
if (getMode().development) {
const hash = parseQueryString(
win.location['originalHash'] || win.location.hash
);
validator = hash['validate'] !== '0';
if (hash['validate'] === 'wasm') {
validatorUrl = `${urls.cdn}/v0/validator_wasm.js`;
} else if (hash['validate'] !== '0') {
validatorUrl = `${urls.cdn}/v0/validator.js`;
}
}

if (validator) {
loadScript(win.document, `${urls.cdn}/v0/validator.js`).then(() => {
if (validatorUrl) {
loadScript(win.document, validatorUrl).then(() => {
/* global amp: false */
amp.validator.validateUrlAndLog(filename, win.document);
});
Expand Down
30 changes: 29 additions & 1 deletion test/unit/test-validator-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ describes.fakeWin('validator-integration', {}, (env) => {
describe('maybeValidate', () => {
beforeEach(() => {
win = env.win;
loadScriptStub = env.sandbox.stub(eventHelper, 'loadPromise');
loadScriptStub = env.sandbox
.stub(eventHelper, 'loadPromise')
.returns(Promise.resolve());
modeStub = env.sandbox.stub(mode, 'getMode');
});

Expand All @@ -51,6 +53,32 @@ describes.fakeWin('validator-integration', {}, (env) => {
maybeValidate(win);
expect(loadScriptStub).to.have.been.called;
});

it('should load JavaScript validator by default', () => {
modeStub.returns({development: true, test: true});
win.location = 'https://www.example.com/#development=1';
maybeValidate(win);
expect(loadScriptStub).to.have.been.calledWith(
env.sandbox.match(
(el) =>
el.getAttribute('src') ===
'https://cdn.ampproject.org/v0/validator.js'
)
);
});

it('should load WebAssembly validator when specified', () => {
modeStub.returns({development: true, test: true});
win.location = 'https://www.example.com/#development=1&validate=wasm';
maybeValidate(win);
expect(loadScriptStub).to.have.been.calledWith(
env.sandbox.match(
(el) =>
el.getAttribute('src') ===
'https://cdn.ampproject.org/v0/validator_wasm.js'
)
);
});
});

describe('loadScript', () => {
Expand Down

0 comments on commit f72e31c

Please sign in to comment.