def test_send_success(self): transport = CertificationTransport(self.valid_url, self.valid_option_string) requests.post.return_value = MagicMock(name='response') requests.post.return_value.status_code = 200 requests.post.return_value.text = '{"id": 768}' result = transport.send(self.sample_xml) self.assertTrue(result)
def test_valid_url_cant_connect(self): transport = CertificationTransport(self.unreachable_url, self.valid_option_string) dummy_data = BytesIO(b"some data to send") requests.post.side_effect = ConnectionError with self.assertRaises(ConnectionError): result = transport.send(dummy_data) self.assertIsNotNone(result) requests.post.assert_called_with( self.unreachable_url, files={'data': dummy_data}, headers={'X_HARDWARE_ID': self.valid_secure_id}, proxies=None)
def test_invalid_url(self): transport = CertificationTransport(self.invalid_url, self.valid_option_string) dummy_data = BytesIO(b"some data to send") requests.post.side_effect = InvalidSchema with self.assertRaises(InvalidSchema): result = transport.send(dummy_data) self.assertIsNotNone(result) requests.post.assert_called_with( self.invalid_url, files={'data': dummy_data}, headers={'X_HARDWARE_ID': self.valid_secure_id}, proxies=None)
def test_valid_url_cant_connect(self): transport = CertificationTransport(self.unreachable_url, self.valid_option_string) dummy_data = BytesIO(b"some data to send") requests.post.side_effect = ConnectionError with self.assertRaises(ConnectionError): result = transport.send(dummy_data) self.assertIsNotNone(result) requests.post.assert_called_with(self.unreachable_url, files={'data': dummy_data}, headers={'X_HARDWARE_ID': self.valid_secure_id}, proxies=None)
def test_invalid_url(self): transport = CertificationTransport(self.invalid_url, self.valid_option_string) dummy_data = BytesIO(b"some data to send") requests.post.side_effect = InvalidSchema with self.assertRaises(InvalidSchema): result = transport.send(dummy_data) self.assertIsNotNone(result) requests.post.assert_called_with(self.invalid_url, files={'data': dummy_data}, headers={'X_HARDWARE_ID': self.valid_secure_id}, proxies=None)
def test_send_failure(self): transport = CertificationTransport(self.valid_url, self.valid_option_string) requests.post.return_value = MagicMock(name='response') requests.post.return_value.status_code = 412 requests.post.return_value.text = 'Some error' #Oops, raise_for_status doesn't get fooled by my mocking, #so I have to mock *that* method as well.. requests.post.return_value.raise_for_status = MagicMock( side_effect=HTTPError) with self.assertRaises(HTTPError): result = transport.send(self.sample_xml) self.assertIsNotNone(result)
def _submit_results(self): print("Submitting results to {0} for secure_id {1}".format( self.config.c3_url, self.config.secure_id)) options_string = "secure_id={0}".format(self.config.secure_id) # Create the transport object try: transport = CertificationTransport( self.config.c3_url, options_string, self.config) except InvalidSecureIDError as exc: print(exc) return False # Prepare the data for submission data = self.exporter.get_session_data_subset(self.session) with tempfile.NamedTemporaryFile(mode='w+b') as stream: # Dump the data to the temporary file self.exporter.dump(data, stream) # Flush and rewind stream.flush() stream.seek(0) try: # Send the data, reading from the temporary file result = transport.send(stream) if 'url' in result: print("Successfully sent, submission status at {0}".format( result['url'])) else: print("Successfully sent, server response: {0}".format( result)) except InvalidSchema as exc: print("Invalid destination URL: {0}".format(exc)) except ConnectionError as exc: print("Unable to connect to destination URL: {0}".format(exc)) except HTTPError as exc: print(("Server returned an error when " "receiving or processing: {0}").format(exc)) except IOError as exc: print("Problem reading a file: {0}".format(exc))
def _submit_results(self): print("Submitting results to {0} for secure_id {1}".format( self.config.c3_url, self.config.secure_id)) options_string = "secure_id={0}".format(self.config.secure_id) # Create the transport object try: transport = CertificationTransport(self.config.c3_url, options_string, self.config) except InvalidSecureIDError as exc: print(exc) return False # Prepare the data for submission data = self.exporter.get_session_data_subset(self.session) with tempfile.NamedTemporaryFile(mode='w+b') as stream: # Dump the data to the temporary file self.exporter.dump(data, stream) # Flush and rewind stream.flush() stream.seek(0) try: # Send the data, reading from the temporary file result = transport.send(stream) if 'url' in result: print("Successfully sent, submission status at {0}".format( result['url'])) else: print("Successfully sent, server response: {0}".format( result)) except InvalidSchema as exc: print("Invalid destination URL: {0}".format(exc)) except ConnectionError as exc: print("Unable to connect to destination URL: {0}".format(exc)) except HTTPError as exc: print(("Server returned an error when " "receiving or processing: {0}").format(exc)) except IOError as exc: print("Problem reading a file: {0}".format(exc))
def proxy_test(self, environment, proxies): test_environment = environment test_proxies = proxies test_config = PlainBoxConfig() test_config.environment = test_environment transport = CertificationTransport(self.valid_url, self.valid_option_string, config=test_config) dummy_data = BytesIO(b"some data to send") requests.post.return_value = MagicMock(name='response') requests.post.return_value.status_code = 200 requests.post.return_value.text = '{"id": 768}' result = transport.send(dummy_data) self.assertTrue(result) requests.post.assert_called_with( self.valid_url, files={'data': dummy_data}, headers={'X_HARDWARE_ID': self.valid_secure_id}, proxies=test_proxies)
def proxy_test(self, environment, proxies): test_environment = environment test_proxies = proxies test_config = PlainBoxConfig() test_config.environment = test_environment transport = CertificationTransport(self.valid_url, self.valid_option_string, config=test_config) dummy_data = BytesIO(b"some data to send") requests.post.return_value = MagicMock(name='response') requests.post.return_value.status_code = 200 requests.post.return_value.text = '{"id": 768}' result = transport.send(dummy_data) self.assertTrue(result) requests.post.assert_called_with(self.valid_url, files={'data': dummy_data}, headers={'X_HARDWARE_ID': self.valid_secure_id}, proxies=test_proxies)