forked from python-hyper/hyper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
59 lines (48 loc) · 1.89 KB
/
tasks.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
import json
import os
from binascii import hexlify
from invoke import task
from hyper.http20.hpack import Encoder
@task
def hpack():
"""
This task generates HPACK test data suitable for use with
https://github.com/http2jp/hpack-test-case
The current format defines a JSON object with three keys: 'draft',
'description' and 'cases'.
The cases key has as its value a list of objects, with each object
representing a set of headers and the output from the encoder. The object
has the following keys:
- 'header_table_size': the size of the header table used.
- 'headers': A list of the headers as JSON objects.
- 'wire': The output from the encoder in hexadecimal.
"""
# A generator that contains the paths to all the raw data files and their
# names.
raw_story_files = (
(os.path.join('test_fixtures/raw-data', name), name)
for name in os.listdir('test_fixtures/raw-data')
)
# For each file, build our output.
for source, outname in raw_story_files:
with open(source, 'rb') as f:
indata = json.loads(f.read())
# Prepare the output and the encoder.
output = {
'description': 'Encoded by hyper. See github.com/Lukasa/hyper for more information.',
'cases': []
}
e = Encoder()
for case in indata['cases']:
outcase = {'header_table_size': e.header_table_size}
outcase['headers'] = case['headers']
headers = []
for header in case['headers']:
key = header.keys()[0]
header = (key, header[key])
headers.append(header)
outcase['wire'] = hexlify(e.encode(headers))
output['cases'].append(outcase)
with open(outname, 'wb') as f:
f.write(json.dumps(output, sort_keys=True,
indent=2, separators=(',', ': ')))