def test_can_read_multiple_frames_from_streams(self): out_frames = [] in_frames = [] def send_cb(frame): out_frames.append(frame) def recv_cb(s): def inner(): s.receive_frame(in_frames.pop(0)) return inner s = Stream(1, send_cb, None, None, None) s._recv_cb = recv_cb(s) s.state = STATE_HALF_CLOSED_LOCAL # Provide two data frames to read. f = DataFrame(1) f.data = b'hi there!' in_frames.append(f) f = DataFrame(1) f.data = b'hi there again!' f.flags.add('END_STREAM') in_frames.append(f) data = s._read() assert data == b'hi there!hi there again!' assert len(out_frames) == 1 assert isinstance(out_frames[0], WindowUpdateFrame) assert out_frames[0].window_increment == len(b'hi there!')
def test_data_frame_serializes_properly(self): f = DataFrame(1) f.flags = set(['END_STREAM']) f.data = b'testdata' s = f.serialize() assert s == b'\x00\x08\x00\x01\x00\x00\x00\x01testdata'
def test_stream_reading_works(self): out_frames = [] in_frames = [] def send_cb(frame): out_frames.append(frame) def recv_cb(s): def inner(): s.receive_frame(in_frames.pop(0)) return inner s = Stream(1, send_cb, None, None, None) s._recv_cb = recv_cb(s) s.state = STATE_HALF_CLOSED_LOCAL # Provide a data frame to read. f = DataFrame(1) f.data = b'hi there!' f.flags.add('END_STREAM') in_frames.append(f) data = s._read() assert data == b'hi there!' assert len(out_frames) == 0
def socket_handler(listener): sock = listener.accept()[0] # Do the handshake: conn header, settings, send settings, recv ack. receive_preamble(sock) # Now expect some data. One headers frame and one data frame. data.append(sock.recv(65535)) data.append(sock.recv(65535)) # Respond! h = HeadersFrame(1) h.data = self.get_encoder().encode({ ':status': 200, 'Content-Type': 'not/real', 'Content-Length': 20 }) h.flags.add('END_HEADERS') sock.send(h.serialize()) d = DataFrame(1) d.data = b'1234567890' * 2 d.flags.add('END_STREAM') sock.send(d.serialize()) send_event.set() sock.close()
def test_data_frame_serializes_properly(self): f = DataFrame(1) f.flags = set(["END_STREAM"]) f.data = b"testdata" s = f.serialize() assert s == b"\x00\x08\x00\x01\x00\x00\x00\x01testdata"
def socket_handler(listener): sock = listener.accept()[0] # Do the handshake: conn header, settings, send settings, recv ack. sock.recv(65535) sock.recv(65535) sock.send(SettingsFrame(0).serialize()) sock.recv(65535) # Now expect some data. One headers frame and one data frame. data.append(sock.recv(65535)) data.append(sock.recv(65535)) # Respond! h = HeadersFrame(1) h.data = self.get_encoder().encode({':status': 200, 'Content-Type': 'not/real', 'Content-Length': 20}) h.flags.add('END_HEADERS') sock.send(h.serialize()) d = DataFrame(1) d.data = b'1234567890' * 2 d.flags.add('END_STREAM') sock.send(d.serialize()) send_event.set() sock.close()
def socket_handler(listener): sock = listener.accept()[0] e = Encoder() e.huffman_coder = HuffmanEncoder(REQUEST_CODES, REQUEST_CODES_LENGTH) # We get two messages for the connection open and then a HEADERS # frame. receive_preamble(sock) # Now, send the headers for the response. This response has no body. f = build_headers_frame([(':status', '200'), ('content-length', '0')], e) f.stream_id = 1 sock.send(f.serialize()) # Also send a data frame. f = DataFrame(1) f.data = b'have some data' sock.send(f.serialize()) # Now, send a headers frame again, containing trailing headers. f = build_headers_frame([('trailing', 'sure'), (':res', 'no')], e) f.flags.add('END_STREAM') f.stream_id = 1 sock.send(f.serialize()) # Wait for the message from the main thread. recv_event.wait() sock.close()
def test_partial_reads_from_streams(self): out_frames = [] in_frames = [] def send_cb(frame): out_frames.append(frame) def recv_cb(s): def inner(): s.receive_frame(in_frames.pop(0)) return inner s = Stream(1, send_cb, None, None, None, None, FlowControlManager(65535)) s._recv_cb = recv_cb(s) s.state = STATE_HALF_CLOSED_LOCAL # Provide two data frames to read. f = DataFrame(1) f.data = b'hi there!' in_frames.append(f) f = DataFrame(1) f.data = b'hi there again!' f.flags.add('END_STREAM') in_frames.append(f) # We'll get the entire first frame. data = s._read(4) assert data == b'hi there!' assert len(out_frames) == 1 # Now we'll get the entire of the second frame. data = s._read(4) assert data == b'hi there again!' assert len(out_frames) == 1 assert s.state == STATE_CLOSED
def test_partial_reads_from_streams(self): out_frames = [] in_frames = [] def send_cb(frame): out_frames.append(frame) def recv_cb(s): def inner(): s.receive_frame(in_frames.pop(0)) return inner s = Stream(1, send_cb, None, None, None, None, FlowControlManager(65535)) s._recv_cb = recv_cb(s) s.state = STATE_HALF_CLOSED_LOCAL # Provide two data frames to read. f = DataFrame(1) f.data = b"hi there!" in_frames.append(f) f = DataFrame(1) f.data = b"hi there again!" f.flags.add("END_STREAM") in_frames.append(f) # We'll get the entire first frame. data = s._read(4) assert data == b"hi there!" assert len(out_frames) == 1 # Now we'll get the entire of the second frame. data = s._read(4) assert data == b"hi there again!" assert len(out_frames) == 1 assert s.state == STATE_CLOSED