def test_read_write(self): record_type = FCGI_DATA request_id = randint(1, 65535) data = binary_data() record = Record(record_type, data, request_id) conn = Connection(self.sock) conn.write_record(record) self.sock.flip() record = conn.read_record() assert record.type == record_type assert record.content == data assert record.request_id == request_id assert conn.read_record() is None
def test_partial_read(self): conn = Connection(self.sock) data = binary_data(FCGI_RECORD_HEADER_LEN - 1) self.sock.sendall(data) self.sock.flip() self.assertRaises(PartialRead, conn.read_record)
def _handle_request(self, app): sock = MockSocket() conn = Connection(sock) request = Request(conn, 1, FCGI_RESPONDER) handler = self.handler_class(app) handler(request) sock.flip() stdout = b''.join(record.content for record in conn if record.type == FCGI_STDOUT) return stdout.split(b'\r\n\r\n', 1)
def setUp(self): self.sock = MockSocket() self.conn = Connection(self.sock)
class StreamTestsBase(object): def setUp(self): self.sock = MockSocket() self.conn = Connection(self.sock) def tearDown(self): del self.conn del self.sock def stream(self, conn=None, request_id=None): if conn is None: conn = self.conn if request_id is None: request_id = randint(1, 65535) return self.stream_class(conn, request_id) def test_constructor(self): conn = self.conn self.assertRaises(TypeError, self.stream_class) self.assertRaises(TypeError, self.stream_class, conn) stream = self.stream_class(conn, 333) assert stream.conn is conn assert stream.request_id == 333 assert stream.record_type == self.stream_class.record_type assert not stream.closed def test_write(self): stream = self.stream() data = [binary_data(1024, 1) for _ in range(13)] map(stream.write, data) self.sock.flip() for chunk, record in zip(data, self.conn): assert record.type == stream.record_type assert record.request_id == stream.request_id assert record.content == chunk assert self.conn.read_record() is None def test_long_write(self): stream = self.stream() data = binary_data(FCGI_MAX_CONTENT_LEN * 3 + 13713) stream.write(data) self.sock.flip() received = [] for record in self.conn: assert record.type == stream.record_type assert record.request_id == stream.request_id received.append(record.content) assert ''.join(received) == ''.join(data) def test_long_writelines(self): stream = self.stream() data = [binary_data(37137) for _ in range(3)] stream.writelines(data) self.sock.flip() received = [] for record in self.conn: assert record.type == stream.record_type assert record.request_id == stream.request_id received.append(record.content) assert ''.join(received) == ''.join(data) def test_empty_write(self): conn = self.conn # calling this would raise AttributeError conn.write_record = None stream = self.stream(conn=conn) stream.write('') stream.flush() stream.writelines('' for _ in range(13)) def test_close(self): stream = self.stream() # should send EOF record stream.close() # should fail since stream was closed self.assertRaises(IOError, stream.write, '') self.assertRaises(IOError, stream.writelines, (text_data(137) for _ in range(3))) self.sock.flip() # should receive EOF record record = self.conn.read_record() assert record.type == stream.record_type assert record.content == '' assert record.request_id == stream.request_id assert self.conn.read_record() is None
def setUp(self): self.sock = MockSocket() self.conn = Connection(self.sock) self.stream = InputStream()
def test_read_write_long_content(self): data = binary_data(FCGI_MAX_CONTENT_LEN + 1) conn = Connection(self.sock) with self.assertRaises(ValueError): conn.write_record(Record(FCGI_STDERR, data, 1))