-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
94 lines (55 loc) 路 2.41 KB
/
Copy pathtest.py
File metadata and controls
94 lines (55 loc) 路 2.41 KB
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
import gzip
import zlib
import brotli
from unittest import TestCase
from flask import Flask
from flask.testing import FlaskClient
from flask_zipper import *
def decode_response_data_with_brotli(response):
return brotli.decompress(response.data).decode()
def decode_response_data_with_deflate(response):
return zlib.decompress(response.data).decode()
def decode_response_data_with_gzip(response):
return gzip.decompress(response.data).decode()
RESPONSE_MESSAGE = 'hello'
def get_test_client_of_decorated_view_function_registered_flask_app(decorator_func) -> FlaskClient:
def view_func():
return RESPONSE_MESSAGE
decorated_view_func = decorator_func(view_func)
app = Flask(__name__)
Zipper(app)
app.add_url_rule('/', view_func=decorated_view_func)
return app.test_client()
def _test_encoded_response(client, content_encoding_string, decoder_function):
resp = client.get('/', headers={'Accept-Encoding': content_encoding_string})
assert resp.status_code == 200
assert resp.headers['Content-Encoding'] == content_encoding_string
assert resp.headers['Vary'] == 'Accept-Encoding'
assert decoder_function(resp) == RESPONSE_MESSAGE
def _test_content_encoding_header_missing(client):
resp = client.get('/')
assert resp.status_code == 200
assert 'Content-Encoding' not in resp.headers
assert resp.data.decode() == RESPONSE_MESSAGE
def _test_content_encoding_value_missing(client):
resp = client.get('/', headers={'Accept-Encoding': 'foo'})
assert resp.status_code == 200
assert 'Content-Encoding' not in resp.headers
assert resp.data.decode() == RESPONSE_MESSAGE
CONTENT_ENCODING_STRING_DECORATOR_DECODER_MAPPING = {
'br': (encode_brotli, decode_response_data_with_brotli),
'deflate': (encode_deflate, decode_response_data_with_deflate),
'gzip': (encode_gzip, decode_response_data_with_gzip)
}
def _test_all(content_encoding_string):
target_func, decoder_function = CONTENT_ENCODING_STRING_DECORATOR_DECODER_MAPPING[content_encoding_string]
client = get_test_client_of_decorated_view_function_registered_flask_app(target_func)
_test_encoded_response(client, content_encoding_string, decoder_function)
_test_content_encoding_header_missing(client)
_test_content_encoding_value_missing(client)
def test_brotli():
_test_all('br')
def test_deflate():
_test_all('deflate')
def test_gzip():
_test_all('gzip')