def call(self, module, method, wrapped, instance, args, kwargs): if 'method' in kwargs: method = kwargs['method'] else: method = args[0] if 'url' in kwargs: url = kwargs['url'] else: url = args[1] signature = method.upper() if url: parsed_url = urlparse.urlparse(url) host = parsed_url.hostname or " " signature += " " + host if parsed_url.port and \ default_ports.get(parsed_url.scheme) != parsed_url.port: signature += ":" + str(parsed_url.port) with trace(signature, "ext.http.requests", {'url': url}, leaf=True): return wrapped(*args, **kwargs)
def test_generic_error(self, mock_urlopen): url, status, message, body = ('http://localhost:9999', 418, "I'm a teapot", 'Nothing') transport = HTTPTransport(urlparse.urlparse(url)) mock_urlopen.side_effect = Exception('Oopsie') with pytest.raises(TransportException) as exc_info: transport.send('x', {}) assert 'Oopsie' in str(exc_info.value)
def test_generic_error(self, mock_urlopen): url, status, message, body = ( 'http://localhost:9999', 418, "I'm a teapot", 'Nothing' ) transport = HTTPTransport(urlparse.urlparse(url)) mock_urlopen.side_effect = Exception('Oopsie') with pytest.raises(TransportException) as exc_info: transport.send('x', {}) assert 'Oopsie' in str(exc_info.value)
def test_send(self, mock_urlopen): transport = HTTPTransport(urlparse.urlparse('http://localhost:9999')) mock_response = mock.Mock( info=lambda: {'Location': 'http://example.com/foo'} ) mock_urlopen.return_value = mock_response url = transport.send('x', {}) assert url == 'http://example.com/foo' assert mock_response.close.call_count == 1
def get_host_from_url(url): parsed_url = urlparse.urlparse(url) host = parsed_url.hostname or " " if (parsed_url.port and default_ports.get(parsed_url.scheme) != parsed_url.port): host += ":" + str(parsed_url.port) return host
def get_host_from_url(url): parsed_url = urlparse.urlparse(url) host = parsed_url.hostname or " " if ( parsed_url.port and default_ports.get(parsed_url.scheme) != parsed_url.port ): host += ":" + str(parsed_url.port) return host
def test_http_error(self, mock_urlopen): url, status, message, body = ( 'http://localhost:9999', 418, "I'm a teapot", 'Nothing' ) transport = HTTPTransport(urlparse.urlparse(url)) mock_urlopen.side_effect = HTTPError( url, status, message, hdrs={}, fp=six.StringIO(body) ) with pytest.raises(TransportException) as exc_info: transport.send('x', {}) for val in (url, status, message, body): assert str(val) in str(exc_info.value)
def test_client_uses_sync_mode_when_master_process(self, is_master_process): # when in the master process, the client should use the non-async # HTTP transport, even if async_mode is True is_master_process.return_value = True client = Client( servers=["http://example.com"], organization_id="organization_id", app_id="app_id", secret_token="secret", async_mode=True, ) self.assertIsInstance(client._get_transport(urlparse.urlparse("http://exampe.com")), HTTPTransport)
def _send_remote(self, url, data, headers=None): if headers is None: headers = {} parsed = urlparse.urlparse(url) transport = self._get_transport(parsed) if transport.async_mode: transport.send_async( data, headers, success_callback=self.handle_transport_success, fail_callback=self.handle_transport_fail) else: url = transport.send(data, headers, timeout=self.timeout) self.handle_transport_success(url=url)
def test_client_uses_sync_mode_when_master_process(self, is_master_process): # when in the master process, the client should use the non-async # HTTP transport, even if async_mode is True is_master_process.return_value = True client = Client( servers=['http://example.com'], organization_id='organization_id', app_id='app_id', secret_token='secret', async_mode=True, ) self.assertIsInstance( client._get_transport(urlparse.urlparse('http://exampe.com')), HTTPTransport)
def _send_remote(self, url, data, headers=None): if headers is None: headers = {} parsed = urlparse.urlparse(url) transport = self._get_transport(parsed) if transport.async: transport.send_async( data, headers, success_callback=self.handle_transport_success, fail_callback=self.handle_transport_fail ) else: response = transport.send(data, headers, timeout=self.timeout) self.handle_transport_success(url=response.info().get('Location')) return response
def call(self, module, method, wrapped, instance, args, kwargs): if 'operation_name' in kwargs: operation_name = kwargs['operation_name'] else: operation_name = args[0] target_endpoint = instance._endpoint.host parsed_url = urlparse.urlparse(target_endpoint) service, region, _ = parsed_url.hostname.split('.', 2) signature = '{}:{}'.format(service, operation_name) extra_data = { 'service': service, 'region': region, 'operation': operation_name, } with trace(signature, 'ext.http.aws', extra_data, leaf=True): return wrapped(*args, **kwargs)
def test_timeout(self, mock_urlopen): transport = HTTPTransport(urlparse.urlparse('http://localhost:9999')) mock_urlopen.side_effect = socket.timeout() with pytest.raises(TransportException) as exc_info: transport.send('x', {}) assert 'timeout' in str(exc_info.value)