summaryrefslogtreecommitdiff
path: root/tests/test_gzipper.py
blob: 8ad5ec3f93474194710beefc3c2d7cd79daf6250 (plain)
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
from paste.fixture import TestApp
from paste.gzipper import middleware
import gzip
import six


def simple_app(environ, start_response):
    start_response('200 OK', [('content-type', 'text/plain')])
    return [b'this is a test']


def simple_app_nothing(environ, start_response):
    start_response('200 OK', [('content-type', 'text/plain')])
    return [b'']


def simple_app_generator(environ, start_response):
    start_response('200 OK', [('content-type', 'text/plain')])
    yield b'this is a test'


def simple_app_generator_nothing(environ, start_response):
    start_response('200 OK', [('content-type', 'text/plain')])
    yield b''


def test_gzip():
    wsgi_app = middleware(simple_app)
    app = TestApp(wsgi_app)
    res = app.get(
        '/', extra_environ=dict(HTTP_ACCEPT_ENCODING='gzip'))
    assert int(res.header('content-length')) == len(res.body)
    assert res.body != b'this is a test'
    actual = gzip.GzipFile(fileobj=six.BytesIO(res.body)).read()
    assert actual == b'this is a test'


def test_gzip_nothing():
    wsgi_app = middleware(simple_app_nothing)
    app = TestApp(wsgi_app)
    res = app.get(
        '/', extra_environ=dict(HTTP_ACCEPT_ENCODING='gzip'))
    assert int(res.header('content-length')) == len(res.body)
    assert res.body != b'this is a test'
    actual = gzip.GzipFile(fileobj=six.BytesIO(res.body)).read()
    assert actual == b''


def test_gzip_generator():
    wsgi_app = middleware(simple_app_generator)
    app = TestApp(wsgi_app)
    res = app.get(
        '/', extra_environ=dict(HTTP_ACCEPT_ENCODING='gzip'))
    assert int(res.header('content-length')) == len(res.body)
    assert res.body != b'this is a test'
    actual = gzip.GzipFile(fileobj=six.BytesIO(res.body)).read()
    assert actual == b'this is a test'


def test_gzip_generator_nothing():
    wsgi_app = middleware(simple_app_generator_nothing)
    app = TestApp(wsgi_app)
    res = app.get(
        '/', extra_environ=dict(HTTP_ACCEPT_ENCODING='gzip'))
    assert int(res.header('content-length')) == len(res.body)
    assert res.body != b'this is a test'
    actual = gzip.GzipFile(fileobj=six.BytesIO(res.body)).read()
    assert actual == b''