|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const { execSync } = require('child_process'); |
| 4 | +const got = require('got'); |
| 5 | + |
| 6 | +const fixturePath = path.join(__dirname, 'fixtures'); |
| 7 | + |
| 8 | +const client = got.extend({ |
| 9 | + baseUrl: 'http://localhost:3000', |
| 10 | + query: { accessKey: process.env.RENDERER_ACCESS_KEY } |
| 11 | +}); |
| 12 | + |
| 13 | +function recordFailure(name, body) { |
| 14 | + fs.writeFileSync(`./${name}`, body); |
| 15 | + execSync(`curl --upload-file ./${name} https://transfer.sh/${name}`, { |
| 16 | + stdio: 'inherit' |
| 17 | + }); |
| 18 | +} |
| 19 | + |
| 20 | +beforeEach(() => { |
| 21 | + jest.setTimeout(10000); |
| 22 | +}); |
| 23 | + |
| 24 | +describe('integration', () => { |
| 25 | + describe('GET /stats', () => { |
| 26 | + it('should disallow access without key', async () => { |
| 27 | + expect.assertions(1); |
| 28 | + try { |
| 29 | + await got.get('http://localhost:3000/stats'); |
| 30 | + } catch (error) { |
| 31 | + expect(error.response.statusCode).toEqual(403); |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + it('should print empty stats', async () => { |
| 36 | + const { body } = await client.get('/stats', { json: true }); |
| 37 | + |
| 38 | + expect(body).toEqual({ |
| 39 | + concurrency: 1, |
| 40 | + queue_length: 0, |
| 41 | + workersList: [] |
| 42 | + }); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('GET /png', () => { |
| 47 | + it('should render valid png from fixtures/example.html', async () => { |
| 48 | + const { body } = await client.get('/png', { |
| 49 | + encoding: null, |
| 50 | + query: { url: 'https://example.com/' } |
| 51 | + }); |
| 52 | + |
| 53 | + const examplePngPath = path.join(fixturePath, 'example.png'); |
| 54 | + const fixture = fs.readFileSync(examplePngPath); |
| 55 | + if (body.compare(fixture) === 0) return; |
| 56 | + |
| 57 | + recordFailure('example_failed.png', body); |
| 58 | + throw new Error(`${examplePngPath} does not match rendered screenshot`); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('GET /pdf', () => { |
| 63 | + it('should render valid pdf from fixtures/example.html', async () => { |
| 64 | + const { body } = await client.get('/pdf', { |
| 65 | + encoding: null, |
| 66 | + query: { url: 'https://example.com/' } |
| 67 | + }); |
| 68 | + |
| 69 | + const examplePdfPath = path.join(fixturePath, 'example.pdf'); |
| 70 | + const fixture = fs.readFileSync(examplePdfPath); |
| 71 | + if (body.slice(150).compare(fixture.slice(150)) === 0) return; // Slice out ModDate |
| 72 | + |
| 73 | + recordFailure('example_failed.pdf', body); |
| 74 | + throw new Error(`${examplePdfPath} does not match rendered pdf`); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('POST /pdf', () => { |
| 79 | + it('should render valid pdf from POSTED html in fixtures/example.html', async () => { |
| 80 | + const exampleHtmlPath = path.join(fixturePath, 'example.html'); |
| 81 | + const exampleHtml = fs.readFileSync(exampleHtmlPath, 'utf-8'); |
| 82 | + |
| 83 | + const { body } = await client.post('/pdf', { |
| 84 | + body: exampleHtml, |
| 85 | + encoding: null, |
| 86 | + query: { url: 'https://example.com/' } |
| 87 | + }); |
| 88 | + |
| 89 | + const examplePdfPath = path.join(fixturePath, 'example.pdf'); |
| 90 | + const fixture = fs.readFileSync(examplePdfPath); |
| 91 | + |
| 92 | + if (body.slice(150).compare(fixture.slice(150)) === 0) return; // Slice out ModDate |
| 93 | + recordFailure('example_failed_post.pdf', body); |
| 94 | + throw new Error(`${examplePdfPath} does not match rendered pdf`); |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
0 commit comments