Debian Codegraph @

testing/web-platform/tests/tools/third_party/hpack/tasks.py

raw
1 # -*- coding: utf-8 -*-
2 import json
3 import os
4
5 from binascii import hexlify
6 from invoke import task
7 from hyper.http20.hpack import Encoder
8
9
10 @task
11 def hpack():
12 """
13 This task generates HPACK test data suitable for use with
14 https://github.com/http2jp/hpack-test-case
15
16 The current format defines a JSON object with three keys: 'draft',
17 'description' and 'cases'.
18
19 The cases key has as its value a list of objects, with each object
20 representing a set of headers and the output from the encoder. The object
21 has the following keys:
22
23 - 'header_table_size': the size of the header table used.
24 - 'headers': A list of the headers as JSON objects.
25 - 'wire': The output from the encoder in hexadecimal.
26 """
27 # A generator that contains the paths to all the raw data files and their
28 # names.
29 raw_story_files = (
30 (os.path.join('test/test_fixtures/raw-data', name), name)
31 for name in os.listdir('test/test_fixtures/raw-data')
32 )
33
34 # For each file, build our output.
35 for source, outname in raw_story_files:
36 with open(source, 'rb') as f:
37 indata = json.load(f)
38
39 # Prepare the output and the encoder.
40 output = {
41 'description': 'Encoded by hyper. See github.com/Lukasa/hyper for more information.',
42 'cases': []
43 }
44 e = Encoder()
45
46 for case in indata['cases']:
47 outcase = {
48 'header_table_size': e.header_table_size,
49 'headers': case['headers'],
50 }
51 headers = []
52
53 for header in case['headers']:
54 key = header.keys()[0]
55 header = (key, header[key])
56 headers.append(header)
57
58 outcase['wire'] = hexlify(e.encode(headers))
59 output['cases'].append(outcase)
60
61 with open(outname, 'wb') as f:
62 f.write(json.dumps(output, sort_keys=True,
63 indent=2, separators=(',', ': ')))