From ceafa6548de254812be785a775dd6bb5a3d7f2d3 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Thu, 30 Sep 2021 17:50:13 +0200 Subject: [PATCH] test: add test for downloading PDF files (#9235) --- tests/assets/empty.pdf | Bin 0 -> 857 bytes tests/download.spec.ts | 62 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 tests/assets/empty.pdf diff --git a/tests/assets/empty.pdf b/tests/assets/empty.pdf new file mode 100644 index 0000000000000000000000000000000000000000..ed845a63727e0a03b754a73e0f050e49d4e0f0a2 GIT binary patch literal 857 zcmaJ=L2DC17?sva8IoHR_4Ek_TdU4)Hk)>(gpy5Cf{BJrZ?VvIcapAVcXW0Fu^=dd z2dRHR^dN};K%rN`+=1MML!QK`;4Eo_Bp-sX4zQ+)3-3wfbwGdhb{4gR1JSw}A!9w|Z zPRr?$eWUBVlA4etM+QY;b{`AUM3Nby|I2`x2%-sB>5dkHC@kg7pXqB2_T zp@uT^xX%c!v#2Y#!7__Qh$T7-$>7z2k^69Rc5W6vf3Xi=w$CqK{3cF|C;o1|k-qPCH%F*p#KZfM!(^h;XwVz3)>-%fN zw-4rKE}VFM`P^?Mf4tJzNG{R1@$fza+H|`p&0#U`3y;NsVPmjfi%DpYGNl`AAc59G zB$mdrJ)UcugjNP}y(LxDSh{Y2ipnYCz<`+_xFtS-wuWFq&%v^l!xEcEl0`BGm6$_l zONzcHo30pv8n$-y2j5Nm~ljWwtBb9`CAU zy6AXnm4RR#qQNrJCIbqlX_(M~38rI_8W2q|!#s}ZCOVwr=@#zmDa_1cYSWmtl3-G} rL5~S_6k5R!Lv_^QT!Qk30oI}p2X$O9coIuj$dQXe-Xz&< { it.beforeEach(async ({ server }) => { @@ -580,3 +581,64 @@ it.describe('download event', () => { await page.close(); }); }); + +it('should be able to download a PDF file', async ({ browser, server, asset }) => { + const page = await browser.newPage({ acceptDownloads: true }); + await page.goto(server.EMPTY_PAGE); + await page.setContent(` + download + `); + const [download] = await Promise.all([ + page.waitForEvent('download'), + page.click('a'), + ]); + await assertDownloadToPDF(download, asset('empty.pdf')); + await page.close(); +}); + +it('should be able to download a inline PDF file', async ({ browser, server, asset }) => { + it.fixme(); + const page = await browser.newPage({ acceptDownloads: true }); + await page.goto(server.EMPTY_PAGE); + await page.route('**/empty.pdf', async route => { + const response = await page.context()._request.get(route.request()); + await route.fulfill({ + response, + headers: { + ...response.headers(), + 'Content-Disposition': 'attachment', + } + }); + }); + await page.setContent(` + open + `); + const [download] = await Promise.all([ + page.waitForEvent('download'), + page.click('a'), + ]); + await assertDownloadToPDF(download, asset('empty.pdf')); + await page.close(); +}); + +async function assertDownloadToPDF(download: Download, filePath: string) { + expect(download.suggestedFilename()).toBe(path.basename(filePath)); + const stream = await download.createReadStream(); + const data = await new Promise((fulfill, reject) => { + const bufs = []; + stream.on('data', d => bufs.push(d)); + stream.on('error', reject); + stream.on('end', () => fulfill(Buffer.concat(bufs))); + }); + expect(download.url().endsWith('/' + path.basename(filePath))).toBeTruthy(); + const expectedPrefix = '%PDF'; + for (let i = 0; i < expectedPrefix.length; i++) + expect(data[i]).toBe(expectedPrefix.charCodeAt(i)); + assertBuffer(data, fs.readFileSync(filePath)); +} + +async function assertBuffer(expected: Buffer, actual: Buffer) { + expect(expected.byteLength).toBe(actual.byteLength); + for (let i = 0; i < expected.byteLength; i++) + expect(expected[i]).toBe(actual[i]); +}