def __init__(self, schema_validate=False, chunk_size=1024, *args, **kwargs): """Data service client constructor. schema_validate -- validate XML transfer document if True chunk_size -- buffer size for get file streaming """ super(DataClient, self).__init__(*args, **kwargs) self.transfer_reader = TransferReader(validate=schema_validate) self.transfer_writer = TransferWriter() self.chunk_size = chunk_size # Specific base_url for data webservice, and a separate base URL # that will be used for HEAD requests (either */data/auth or # */data/pub). self.base_url = self.base_url + '/data' self.head_base_url = self.base_url if self.basic_auth is not None: self.base_url = self.base_url + '/auth' self.head_base_url = self.head_base_url + '/auth' else: self.head_base_url = self.head_base_url + '/pub' self.base_url = self.base_url + '/transfer'
def test_roundtrip_get(self): tran = Transfer(test_target_good, test_dir_get, protocols=[ Protocol(DIRECTION_PROTOCOL_MAP['pullFromVoSpace'], endpoint='http://somewhere') ], properties={ 'LENGTH': '1234', 'uri=ivo://ivoa.net/vospace/core#quota': '100' }, version=VOSPACE_21) xml_str = TransferWriter().write(tran) tran2 = TransferReader(validate=True).read(xml_str) self.assertEqual(tran.target, tran2.target, 'Wrong target.') self.assertEqual(tran.direction, tran2.direction, 'Wrong direction.') self.assertEqual(tran.properties, tran2.properties, 'Wrong properties.') self.assertEqual(len(tran.protocols), len(tran2.protocols), 'Wrong number of protocols.') for i in range(len(tran.protocols)): p1 = tran.protocols[i] p2 = tran2.protocols[i] self.assertEqual(p1.uri, p1.uri, 'Wrong uri, protocol %i' % i) self.assertEqual(p1.endpoint, p1.endpoint, 'Wrong endpoint, protocol %i' % i)
def test_validation(self): # VOSPACE_20 tran = Transfer(test_target_good, test_dir_put, version=VOSPACE_20) xml_str = TransferWriter().write(tran) tran2 = TransferReader(validate=True).read(xml_str) # VOSPACE_21 tran = Transfer(test_target_good, test_dir_put, properties={'LENGTH': '1234'}, version=VOSPACE_21) xml_str = TransferWriter().write(tran) # introduce an error that schema validation should catch xml = etree.fromstring(xml_str) junk = etree.SubElement(xml, 'junk') xml_str2 = etree.tostring(xml, encoding='UTF-8', pretty_print=True) # should not raise exception because validation turned off by default tran2 = TransferReader().read(xml_str2) # should now raise exception with validation turned on with self.assertRaises(etree.DocumentInvalid): tran2 = TransferReader(validate=True).read(xml_str2)
def test_roundtrip_put(self): tran = Transfer(test_target_good, test_dir_put, properties={'LENGTH': '1234'}, version=VOSPACE_21) xml_str = TransferWriter().write(tran) tran2 = TransferReader(validate=True).read(xml_str) self.assertEqual(tran.target, tran2.target, 'Wrong target.') self.assertEqual(tran.direction, tran2.direction, 'Wrong direction.') self.assertEqual(tran.properties, tran2.properties, 'Wrong properties.') self.assertEqual(len(tran.protocols), len(tran2.protocols), 'Wrong number of protocols.') for i in range(len(tran.protocols)): p1 = tran.protocols[i] p2 = tran2.protocols[i] self.assertEqual(p1.uri, p1.uri, 'Wrong uri, protocol %i' % i) self.assertEqual(p1.endpoint, p1.endpoint, 'Wrong endpoint, protocol %i' % i)