From f72e31c116c7e2e4edaa96f350fd090263c74dfe Mon Sep 17 00:00:00 2001 From: Boxiao Cao <9083193+antiphoton@users.noreply.github.com> Date: Thu, 22 Apr 2021 11:11:20 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8Add=20a=20hash=20parameter=20to=20inte?= =?UTF-8?q?grate=20WebAssembly=20Validator=20(#33941)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add hash parameter `#validate=wasm` * Lint --- src/validator-integration.js | 12 ++++++---- test/unit/test-validator-integration.js | 30 ++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/validator-integration.js b/src/validator-integration.js index 65b08d5e7aed..ec22fcb6f576 100644 --- a/src/validator-integration.js +++ b/src/validator-integration.js @@ -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); }); diff --git a/test/unit/test-validator-integration.js b/test/unit/test-validator-integration.js index 331238558d56..2590dda5723d 100644 --- a/test/unit/test-validator-integration.js +++ b/test/unit/test-validator-integration.js @@ -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'); }); @@ -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', () => {