forget-cancellare-vecchi-toot/test/test_libbrotli.py

128 lines
3.0 KiB
Python
Raw Normal View History

2017-09-08 23:19:59 +02:00
import pytest
2017-09-20 23:02:36 +02:00
import libforget.brotli
2017-09-08 23:19:59 +02:00
BEE_SCRIPT = bytes("According to all known laws of aviation,", 'UTF-8')
TIMEOUT_TARGET = 0.2
@pytest.fixture
def app():
2017-09-08 23:19:59 +02:00
from flask import Flask
app_ = Flask(__name__)
app_.config['REDIS_URI'] = 'redis://localhost:6379'
2017-09-09 00:06:09 +02:00
app_.debug = True
2017-09-08 23:19:59 +02:00
@app_.route('/')
2017-09-24 23:54:03 +02:00
def hello(): # pylint: disable=W0612
return 'Hello, world!'
2017-09-08 23:19:59 +02:00
with app_.app_context():
yield app_
@pytest.fixture
def br_app(app):
2017-09-20 23:02:36 +02:00
libforget.brotli.brotli(app, timeout=TIMEOUT_TARGET)
2017-09-08 23:19:59 +02:00
return app
@pytest.fixture
def br_client(br_app):
return br_app.test_client()
def test_brotli_static_passthru(br_client):
resp = br_client.get('/static/bee.txt')
assert BEE_SCRIPT in resp.data
def test_brotli_static_gzip(br_client):
from gzip import decompress
gzip_resp = br_client.get(
'/static/bee.txt',
headers=[('accept-encoding', 'gzip')])
assert gzip_resp.headers.get('content-encoding') == 'gzip'
assert BEE_SCRIPT in decompress(gzip_resp.data)
def test_brotli_static_br(br_client):
from brotli import decompress
br_resp = br_client.get(
'/static/bee.txt',
headers=[('accept-encoding', 'gzip, br')])
assert br_resp.headers.get('content-encoding') == 'br'
assert BEE_SCRIPT in decompress(br_resp.data)
def test_brotli_dynamic(br_client):
from brotli import decompress
resp = br_client.get(
'/',
headers=[('accept-encoding', 'gzip, br')])
2018-11-30 03:25:24 +01:00
assert resp.headers.get('brotli-cache') == 'MISS'
2017-09-08 23:19:59 +02:00
assert resp.headers.get('content-encoding') == 'br'
assert b"Hello, world!" in decompress(resp.data)
def test_brotli_dynamic_cache(br_client):
from brotli import decompress
from time import sleep
br_client.get(
'/',
headers=[('accept-encoding', 'gzip, br')])
sleep(0.5)
resp = br_client.get(
'/',
headers=[('accept-encoding', 'gzip, br')])
2018-11-30 03:25:24 +01:00
assert resp.headers.get('brotli-cache') == 'HIT'
2017-09-08 23:19:59 +02:00
assert resp.headers.get('content-encoding') == 'br'
assert b"Hello, world!" in decompress(resp.data)
def test_brotli_dynamic_timeout(app):
2017-09-09 00:06:09 +02:00
from secrets import token_urlsafe
libforget.brotli.brotli(app, timeout=0.001)
2017-09-09 00:06:09 +02:00
@app.route('/hard_to_compress')
2017-09-24 23:54:03 +02:00
def hard_to_compress(): # pylint: disable=W0612
return token_urlsafe(2**16)
2017-09-08 23:19:59 +02:00
client = app.test_client()
resp = client.get(
2017-09-09 00:06:09 +02:00
'/hard_to_compress',
2017-09-08 23:19:59 +02:00
headers=[('accept-encoding', 'gzip, br')])
2018-11-30 03:25:24 +01:00
assert resp.headers.get('brotli-cache') == 'TIMEOUT'
2017-09-08 23:19:59 +02:00
assert resp.headers.get('content-encoding') != 'br'
def test_brotli_dynamic_expire(app):
from time import sleep
2017-09-20 23:02:36 +02:00
libforget.brotli.brotli(app, expire=0.1)
2017-09-08 23:19:59 +02:00
client = app.test_client()
client.get(
'/',
headers=[('accept-encoding', 'gzip, br')])
sleep(1)
resp = client.get(
'/',
headers=[('accept-encoding', 'gzip, br')])
2018-11-30 03:25:24 +01:00
assert resp.headers.get('brotli-cache') != 'HIT'