A class containing lots of helper methods and state to build frames. This allows test cases to easily build correct HTTP/2 frames to feed to hyper-h2.
| 29 | |
| 30 | |
| 31 | class FrameFactory: |
| 32 | """ |
| 33 | A class containing lots of helper methods and state to build frames. This |
| 34 | allows test cases to easily build correct HTTP/2 frames to feed to |
| 35 | hyper-h2. |
| 36 | """ |
| 37 | |
| 38 | def __init__(self): |
| 39 | self.encoder = Encoder() |
| 40 | |
| 41 | def refresh_encoder(self): |
| 42 | self.encoder = Encoder() |
| 43 | |
| 44 | def preamble(self): |
| 45 | return b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" |
| 46 | |
| 47 | def build_headers_frame(self, headers, flags=[], stream_id=1, **priority_kwargs): |
| 48 | """ |
| 49 | Builds a single valid headers frame out of the contained headers. |
| 50 | """ |
| 51 | f = HeadersFrame(stream_id) |
| 52 | f.data = self.encoder.encode(headers) |
| 53 | f.flags.add("END_HEADERS") |
| 54 | for flag in flags: |
| 55 | f.flags.add(flag) |
| 56 | |
| 57 | for k, v in priority_kwargs.items(): |
| 58 | setattr(f, k, v) |
| 59 | |
| 60 | return f |
| 61 | |
| 62 | def build_continuation_frame(self, header_block, flags=[], stream_id=1): |
| 63 | """ |
| 64 | Builds a single continuation frame out of the binary header block. |
| 65 | """ |
| 66 | f = ContinuationFrame(stream_id) |
| 67 | f.data = header_block |
| 68 | f.flags = set(flags) |
| 69 | |
| 70 | return f |
| 71 | |
| 72 | def build_data_frame(self, data, flags=None, stream_id=1, padding_len=0): |
| 73 | """ |
| 74 | Builds a single data frame out of a chunk of data. |
| 75 | """ |
| 76 | flags = set(flags) if flags is not None else set() |
| 77 | f = DataFrame(stream_id) |
| 78 | f.data = data |
| 79 | f.flags = flags |
| 80 | |
| 81 | if padding_len: |
| 82 | flags.add("PADDED") |
| 83 | f.pad_length = padding_len |
| 84 | |
| 85 | return f |
| 86 | |
| 87 | def build_settings_frame(self, settings, ack=False): |
| 88 | """ |
no outgoing calls
searching dependent graphs…