From d8077f567890c4fb05b57ee526d0ad919b8578b4 Mon Sep 17 00:00:00 2001 From: Justin Holdstock Date: Thu, 25 Oct 2018 15:40:13 -0600 Subject: [PATCH] Chore: Add test to check PDFjs headers to avoid preflight (#855) --- .../doc/__tests__/DocBaseViewer-test.js | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/lib/viewers/doc/__tests__/DocBaseViewer-test.js b/src/lib/viewers/doc/__tests__/DocBaseViewer-test.js index 9c27947f2..6265fb1cc 100644 --- a/src/lib/viewers/doc/__tests__/DocBaseViewer-test.js +++ b/src/lib/viewers/doc/__tests__/DocBaseViewer-test.js @@ -39,6 +39,18 @@ let docBase; let containerEl; let stubs = {}; +const STANDARD_HEADERS = [ + 'Accept', + 'Accept-Language', + 'Content-Language', + 'Content-Type', + 'DPR', + 'Downlink', + 'Save-Data', + 'Viewport-Width', + 'Width' +]; + describe('src/lib/viewers/doc/DocBaseViewer', () => { const setupFunc = BaseViewer.prototype.setup; @@ -932,6 +944,29 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => { }); }); + it('should avoid preflight requests by not adding non-standard headers', (done) => { + docBase.options.location = { + locale: 'en-US' + }; + // Excluding IOS for If-None-Match cache busting + sandbox.stub(Browser, 'isIOS').returns(false); + sandbox.stub(PDFJS, 'getDocument').callsFake((docInitParams) => { + return new Promise(() => { + const { httpHeaders = {} } = docInitParams; + const headerKeys = Object.keys(httpHeaders); + + const containsNonStandardHeader = headerKeys.some((header) => { + return !STANDARD_HEADERS.includes(header); + }); + + expect(containsNonStandardHeader).to.be.false; + done(); + }); + }); + + return docBase.initViewer(''); + }); + it('should append encoding query parameter for gzip content when range requests are disabled', () => { // en-US allows for disabled range requests docBase.options.location = {