def test_flush_no_body(self, mock_requests): url = 'http://localhost:8080/frugal' tr = THttpTransport(url) tr.flush() self.assertFalse(mock_requests.post.called)
def test_flush_oneway(self, mock_requests): url = 'http://localhost:8080/frugal' resp = Mock(status_code=200) buff = bytearray(4) pack_into('!I', buff, 0, 0) resp.content = b64encode(buff) mock_requests.post.return_value = resp tr = THttpTransport(url) data = b'helloworld' buff = bytearray(4) pack_into('!I', buff, 0, len(data)) encoded_frame = b64encode(buff + data) tr.write(data) tr.flush() mock_requests.post.assert_called_once_with( url, data=encoded_frame, timeout=None, headers={'Content-Length': '20', 'Content-Type': 'application/x-frugal', 'Content-Transfer-Encoding': 'base64', 'User-Agent': 'Python/TBaseHttpTransport'}) resp = tr.read(10) self.assertEqual(b'', resp)
def test_write_limit_exceeded(self, mock_requests): url = 'http://localhost:8080/frugal' resp = Mock(status_code=200) buff = bytearray(4) pack_into('!I', buff, 0, 0) resp.content = b64encode(buff) mock_requests.post.return_value = resp tr = THttpTransport(url, request_capacity=5) data = b'helloworld' with self.assertRaises(TTransportException) as cm: tr.write(data) self.assertEqual(TTransportExceptionType.REQUEST_TOO_LARGE, cm.exception.type) self.assertFalse(mock_requests.post.called)
def main(): logging.info("Starting...") # Declare the protocol stack used for serialization. # Protocol stacks must match between clients and servers. prot_factory = FProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()) # Create an HTTP transport for the server URL transport = THttpTransport(URL) transport.open() # Using the configured transport and protocol, create a client # to talk to the music store service. store_client = FStoreClient(FServiceProvider(transport, prot_factory)) album = store_client.buyAlbum(FContext(), str(uuid.uuid4()), "ACT-12345") root.info("Bought an album %s\n", album) store_client.enterAlbumGiveaway(FContext(), "*****@*****.**", "Kevin") # Close the transport transport.close()
def main(): parser = argparse.ArgumentParser(description="Run a vanilla python client") parser.add_argument('--port', dest='port', default='9090') parser.add_argument('--protocol', dest='protocol_type', default="binary", choices="binary, compact, json") parser.add_argument('--transport', dest='transport_type', default="http", choices="http") args = parser.parse_args() protocol_factory = get_protocol_factory(args.protocol_type) if args.transport_type == "http": transport = THttpTransport("http://localhost:" + str(args.port)) else: print("Unknown transport type: {}".format(args.transport_type)) sys.exit(1) transport.open() ctx = FContext("test") client = FrugalTestClient(FServiceProvider(transport, protocol_factory), client_middleware) # Scope generation is not currently supported with vanilla python # TODO: Add Pub/Sub test once scopes are supported test_rpc(client, ctx) global middleware_called if not middleware_called: print("Client middleware never invoked") exit(1) transport.close()
def test_request_timeout(self, mock_requests): url = 'http://localhost:8080/frugal' headers = {'foo': 'bar'} resp = Mock(status_code=200) response = b'response' buff = bytearray(4) pack_into('!I', buff, 0, len(response)) resp.content = b64encode(buff + response) mock_requests.post.return_value = resp def get_headers(): return {'baz': 'qux'} tr = THttpTransport(url, headers=headers, get_headers=get_headers, response_capacity=500) tr.open() self.assertTrue(tr.isOpen()) data = b'helloworld' buff = bytearray(4) pack_into('!I', buff, 0, len(data)) encoded_frame = b64encode(buff + data) tr.set_timeout(5000) tr.write(data) tr.flush() mock_requests.post.assert_called_once_with( url, data=encoded_frame, timeout=5, headers={'foo': 'bar', 'baz': 'qux', 'Content-Length': '20', 'Content-Type': 'application/x-frugal', 'Content-Transfer-Encoding': 'base64', 'User-Agent': 'Python/TBaseHttpTransport', 'x-frugal-payload-limit': '500'}) resp = tr.read(len(response)) self.assertEqual(response, resp) tr.close() self.assertTrue(tr.isOpen()) # open/close are no-ops