-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
109 lines (85 loc) · 3.46 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import threading
from pyppeteer import launch
from quart import Quart, Response, request
from async_generator import asynccontextmanager
# those imports are for testing purposes:
import asyncio
import unittest
import multiprocessing
import time
import hashlib
import requests
app = Quart(__name__)
def random_string():
return hashlib.md5(str(time.time()).encode('utf8')).hexdigest()
SHUTDOWN_CODE = random_string()
# this is for testing purposes only - we need a way to turn off the server
# once tests are done
@app.route('/' + SHUTDOWN_CODE, methods=['POST', 'GET'])
def shutdown():
asyncio.get_event_loop().stop()
return 'Server shutting down...'
@asynccontextmanager
async def visit_page(url):
# this lets us work without CAP_SYS_ADMIN:
options = {'args': ['--no-sandbox']}
# HACK: we're disabling signals because they fail in system tests
if threading.currentThread() != threading._main_thread:
options.update({'handleSIGINT': False, 'handleSIGHUP': False,
'handleSIGTERM': False})
browser = await launch(**options)
try:
page = await browser.newPage()
await page.goto(url)
yield page
finally:
await browser.close()
@app.route('/html2pdf', methods=['POST'])
async def html2pdf():
call_args = await request.form
url = call_args.pop('url')
async with visit_page(url) as page:
pdf = await page.pdf(**call_args)
return Response(pdf, mimetype='application/pdf')
@app.route('/html2img', methods=['POST'])
async def html2img():
call_args = await request.form
url = call_args.pop('url')
img_format = call_args.get('type', 'png')
async with visit_page(url) as page:
pdf = await page.screenshot(**call_args)
return Response(pdf, mimetype=('image/' + img_format))
class SystemTest(unittest.TestCase):
def setUp(self):
self.server_thread = multiprocessing.Process(target=lambda: app.run())
self.server_thread.start()
time.sleep(1.0)
def tearDown(self):
requests.post(self.get_server_url() + '/' + SHUTDOWN_CODE, {})
self.server_thread.terminate()
self.server_thread.join()
def get_server_url(self):
return 'http://localhost:5000'
def test_server_renders_about_blank_pdf(self):
args = {'url': 'about:blank'}
response = requests.post(self.get_server_url() + '/html2pdf', args)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers['content-type'], 'application/pdf')
expected = b'%PDF'
self.assertEqual(response.content[:len(expected)], expected)
def test_server_renders_about_blank_png(self):
args = {'url': 'about:blank'}
response = requests.post(self.get_server_url() + '/html2img', args)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers['content-type'], 'image/png')
expected = b'\x89PNG\r\n\x1a\n\x00'
self.assertEqual(response.content[:len(expected)], expected)
def test_server_renders_about_blank_jpg(self):
args = {'url': 'about:blank', 'type': 'jpeg'}
response = requests.post(self.get_server_url() + '/html2img', args)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers['content-type'], 'image/jpeg')
expected = b'\xff\xd8\xff\xe0\x00\x10JFIF'
self.assertEqual(response.content[:len(expected)], expected)