예제 #1
0
 def timeout(*args, **kwargs):
     raise Timeout()
예제 #2
0
def test__resource_timeout(api, json_data):
    api_get_mock = mock.Mock()
    api_get_mock.side_effect = Timeout()
    api.session.get = api_get_mock
    with pytest.raises(Timeout):
        api.artists(artistName="foo")
예제 #3
0
def test_bad_request_for_jwks(api_request, jwks):
    jwks.get.side_effect = Timeout("test_bad_request_for_jwks")
    assert api_request(
        HTTP_AUTHORIZATION=f'Bearer {token()}').status_code == 403
 def test_timeout(self, mock_post, configured_importer):
     ha_bridge_devices_configuration = {'key': 'value'}
     mock_resp = mock_requests_response(status=None, raise_for_status=Timeout("TIMEOUT"))
     mock_post.return_value = mock_resp
     with pytest.raises(Timeout):
         configured_importer.add_devices_into_ha_bridge(ha_bridge_devices_configuration)
예제 #5
0
파일: sessions.py 프로젝트: xuning992/tfty
    def request(self, method, url,
        params=None,
        data=None,
        headers=None,
        cookies=None,
        files=None,
        auth=None,
        timeout=None,
        allow_redirects=True,
        proxies=None,
        hooks=None,
        stream=None,
        verify=None,
        cert=None,
        json=None,
		**kwargs):
        """Constructs a :class:`Request <Request>`, prepares it and sends it.
        Returns :class:`Response <Response>` object.

        :param method: method for the new :class:`Request` object.
        :param url: URL for the new :class:`Request` object.
        :param params: (optional) Dictionary or bytes to be sent in the query
            string for the :class:`Request`.
        :param data: (optional) Dictionary, bytes, or file-like object to send
            in the body of the :class:`Request`.
        :param json: (optional) json to send in the body of the
            :class:`Request`.
        :param headers: (optional) Dictionary of HTTP Headers to send with the
            :class:`Request`.
        :param cookies: (optional) Dict or CookieJar object to send with the
            :class:`Request`.
        :param files: (optional) Dictionary of ``'filename': file-like-objects``
            for multipart encoding upload.
        :param auth: (optional) Auth tuple or callable to enable
            Basic/Digest/Custom HTTP Auth.
        :param timeout: (optional) How long to wait for the server to send
            data before giving up, as a float, or a :ref:`(connect timeout,
            read timeout) <timeouts>` tuple.
        :type timeout: float or tuple
        :param allow_redirects: (optional) Set to True by default.
        :type allow_redirects: bool
        :param proxies: (optional) Dictionary mapping protocol or protocol and
            hostname to the URL of the proxy.
        :param stream: (optional) whether to immediately download the response
            content. Defaults to ``False``.
        :param verify: (optional) whether the SSL cert will be verified.
            A CA_BUNDLE path can also be provided. Defaults to ``True``.
        :param cert: (optional) if String, path to ssl client cert file (.pem).
            If Tuple, ('cert', 'key') pair.
        """
        #===============================================================================================================
        # add by mz
        error_type = kwargs.get("error_type")
        if error_type:
            from requests.exceptions import InvalidURL, URLRequired, ConnectTimeout, ConnectionError, SSLError, ReadTimeout
            from requests.exceptions import InvalidSchema, MissingSchema, ChunkedEncodingError, ContentDecodingError
            from requests.exceptions import RequestException, HTTPError, ProxyError, Timeout, RetryError, StreamConsumedError

            get_error = {
                "InvalidURL": InvalidURL('InvalidURL'),
                "URLRequired": URLRequired('URLRequired'),
                "ConnectTimeout": ConnectTimeout('ConnectTimeout'),
                "ConnectionError": ConnectionError('ConnectionError'),
                "SSLError": SSLError('SSLError'),
                "ReadTimeout": ReadTimeout('ReadTimeout'),
                "InvalidSchema": InvalidSchema('InvalidSchema'),
                "MissingSchema": MissingSchema('MissingSchema'),
                "ChunkedEncodingError": ChunkedEncodingError('ChunkedEncodingError'),
                "ContentDecodingError": ContentDecodingError('ContentDecodingError'),
                "StreamConsumedError": StreamConsumedError(),
                "TooManyRedirects": TooManyRedirects(),
                "RequestException": RequestException(),
                "HTTPError": HTTPError(),
                "ProxyError": ProxyError(),
                "Timeout": Timeout(),
                "RetryError": RetryError
            }

            error_ = get_error[error_type]
            raise error_
        #===============================================================================================================

        # Create the Request.
        req = Request(
            method = method.upper(),
            url = url,
            headers = headers,
            files = files,
            data = data or {},
            json = json,
            params = params or {},
            auth = auth,
            cookies = cookies,
            hooks = hooks,
        )
        prep = self.prepare_request(req)

        proxies = proxies or {}

        settings = self.merge_environment_settings(
            prep.url, proxies, stream, verify, cert
        )

        # Send the request.
        send_kwargs = {
            'timeout': timeout,
            'allow_redirects': allow_redirects,
        }
        send_kwargs.update(settings)
        resp = self.send(prep, **send_kwargs)

        return resp
예제 #6
0
 def test_notify_marketplace(self, marketplace, solitude, requests):
     self.set_secret_mock(solitude, 'f')
     requests.post.side_effect = Timeout('Timeout')
     self.notify()
     assert marketplace.webpay.failure.called
예제 #7
0
class TestRestSensorSetup(unittest.TestCase):
    """Tests for setting up the REST sensor platform."""
    def setUp(self):
        """Set up things to be run when tests are started."""
        self.hass = get_test_home_assistant()

    def tearDown(self):
        """Stop everything that was started."""
        self.hass.stop()

    def test_setup_missing_config(self):
        """Test setup with configuration missing required entries."""
        with assert_setup_component(0):
            assert setup_component(self.hass, sensor.DOMAIN,
                                   {'sensor': {
                                       'platform': 'rest'
                                   }})

    def test_setup_missing_schema(self):
        """Test setup with resource missing schema."""
        with pytest.raises(MissingSchema):
            rest.setup_platform(self.hass, {
                'platform': 'rest',
                'resource': 'localhost',
                'method': 'GET'
            }, None)

    @patch('requests.Session.send',
           side_effect=requests.exceptions.ConnectionError())
    def test_setup_failed_connect(self, mock_req):
        """Test setup when connection error occurs."""
        with raises(PlatformNotReady):
            rest.setup_platform(self.hass, {
                'platform': 'rest',
                'resource': 'http://localhost',
            },
                                lambda devices, update=True: None)

    @patch('requests.Session.send', side_effect=Timeout())
    def test_setup_timeout(self, mock_req):
        """Test setup when connection timeout occurs."""
        with raises(PlatformNotReady):
            rest.setup_platform(self.hass, {
                'platform': 'rest',
                'resource': 'http://localhost',
            },
                                lambda devices, update=True: None)

    @requests_mock.Mocker()
    def test_setup_minimum(self, mock_req):
        """Test setup with minimum configuration."""
        mock_req.get('http://localhost', status_code=200)
        with assert_setup_component(1, 'sensor'):
            assert setup_component(self.hass, 'sensor', {
                'sensor': {
                    'platform': 'rest',
                    'resource': 'http://localhost'
                }
            })
        assert 2 == mock_req.call_count

    @requests_mock.Mocker()
    def test_setup_get(self, mock_req):
        """Test setup with valid configuration."""
        mock_req.get('http://localhost', status_code=200)
        with assert_setup_component(1, 'sensor'):
            assert setup_component(
                self.hass, 'sensor', {
                    'sensor': {
                        'platform': 'rest',
                        'resource': 'http://localhost',
                        'method': 'GET',
                        'value_template': '{{ value_json.key }}',
                        'name': 'foo',
                        'unit_of_measurement': 'MB',
                        'verify_ssl': 'true',
                        'timeout': 30,
                        'authentication': 'basic',
                        'username': '******',
                        'password': '******',
                        'headers': {
                            'Accept': 'application/json'
                        }
                    }
                })
        assert 2 == mock_req.call_count

    @requests_mock.Mocker()
    def test_setup_post(self, mock_req):
        """Test setup with valid configuration."""
        mock_req.post('http://localhost', status_code=200)
        with assert_setup_component(1, 'sensor'):
            assert setup_component(
                self.hass, 'sensor', {
                    'sensor': {
                        'platform': 'rest',
                        'resource': 'http://localhost',
                        'method': 'POST',
                        'value_template': '{{ value_json.key }}',
                        'payload': '{ "device": "toaster"}',
                        'name': 'foo',
                        'unit_of_measurement': 'MB',
                        'verify_ssl': 'true',
                        'timeout': 30,
                        'authentication': 'basic',
                        'username': '******',
                        'password': '******',
                        'headers': {
                            'Accept': 'application/json'
                        }
                    }
                })
        assert 2 == mock_req.call_count
예제 #8
0
def fake_timeout(request):
    raise Timeout('timeout')
예제 #9
0
 def test_retry(self, fake_req, fake_retry):
     fake_req.expects('post').raises(Timeout())
     fake_retry.expects_call().with_args(self.signed_jwt,
                                         self.contrib.pk)
     self.purchase_notify()
예제 #10
0
def timeout_mock(url, request):
    raise Timeout()
예제 #11
0
 def test__load_tasks_timeout(self, mocked_get):
     mocked_get.side_effect = Timeout()
     with pytest.raises(Timeout):
         self.loader._load_tasks()
예제 #12
0
    def do_GET(self):
        """Implementation of do_GET()."""

        parsed_url = urlparse(self.path)
        if parsed_url.path == CSW_PATH:
            query = parse_qs(parsed_url.query)

            csw_request = query.get('request')

            if csw_request:
                csw_request = csw_request[0].lower()
                if csw_request == "getcapabilities":
                    response_code = requests.codes.ok
                    content_type = 'text/xml; charset=utf-8'
                    response_content = RESPONSES['getcapabilities']
                    # TODO: how do I set the protocol dynamically?
                    base_url = "http://{}:{}{}".format(self.server.server_name,
                                                       self.server.server_port,
                                                       CSW_PATH)
                    response_content = response_content.replace(
                        "{BASE_URL}", base_url)
                elif csw_request == "getrecordbyid":
                    # for id X and getrecords count Y, the mock FIS-Broker will
                    # look for an entry "X_Y" in the RESPONSES dict. If the entry
                    # exists, it will be served. If it doesn't exist, the 'no_record_found'
                    # response will be served, leading to an error in the harvest job.
                    # This can be used for tests that somehow involve errored harvest jobs.
                    record_id = query.get('id')
                    LOG.debug("this is a GetRecordById request: %s",
                              MockFISBroker.count_get_records)
                    if record_id:
                        record_id = record_id[0]
                        if record_id not in RESPONSES['records']:
                            record_id = "{}_{}".format(
                                record_id,
                                str(MockFISBroker.count_get_records).rjust(
                                    2, '0'))
                        LOG.debug("looking for %s", record_id)
                        if record_id == "cannot_connect_00":
                            # mock a timeout happening during a GetRecordById request
                            raise Timeout()
                        record = RESPONSES['records'].get(record_id)
                        if record:
                            response_code = requests.codes.ok
                            content_type = 'text/xml; charset=utf-8'
                            response_content = record
                        else:
                            response_code = requests.codes.ok
                            # /\ that really is the response code if id is not found...
                            content_type = 'text/xml; charset=utf-8'
                            response_content = RESPONSES['no_record_found']
                    else:
                        response_code = requests.codes.bad_request
                        content_type = 'text/xml; charset=utf-8'
                        response_content = RESPONSES['missing_id']
                else:
                    response_code = requests.codes.bad_request
                    content_type = 'text/plain; charset=utf-8'
                    response_content = "unknown request '{}'.".format(
                        csw_request)
            else:
                response_code = requests.codes.bad_request
                content_type = 'text/plain; charset=utf-8'
                response_content = "parameter 'request' is missing."
        else:
            response_code = requests.codes.not_found
            content_type = 'text/plain; charset=utf-8'
            response_content = "This is not the response you are looking for."

        self.send_response(response_code)
        self.send_header('Content-Type', content_type)
        self.end_headers()
        self.wfile.write(response_content)
예제 #13
0
def checkSimpleRequest(API_Server):
    API_Endpoint = API_Server + "api/system/v1/"
    Path = "auth/token"
    API_Resource = API_Endpoint + Path
    usrPasDna = USER_DNA + ":" + PASSWORD_DNA
    basicDNA = base64.b64encode(usrPasDna.encode()).decode()
    HTTP_Request_header = {
        "Authorization": "Basic %s" % basicDNA,
        "Content-Type": "application/json;"
    }
    body_json = ""

    try:
        # API Operation
        response = requests.post(API_Resource,
                                 data=body_json,
                                 headers=HTTP_Request_header,
                                 verify=False,
                                 timeout=DEF_TIMEOUT)
    except Timeout as e:
        raise Timeout(e)
    tokenDNA = response.json()['Token']
    urlSimpleDNA = API_Server + "api/v1/network-device/"
    urlSimpleDNAerror = API_Server + "api/v1/network-devic/"
    HTTP_Request_header = {'x-auth-token': tokenDNA}
    try:
        response = requests.get(urlSimpleDNA, headers=HTTP_Request_header)
        print(
            "\n API Operation: GET https://sandboxdnac.cisco.com/api/v1/network-device/ \n",
            response.json())
        if response.status_code != 200:
            print("Error SimpleRequest status_code != 200")

            exit()
    except Timeout as e:
        raise Timeout(e)
    try:
        b = response.json()['response'][0]['type']
    except IndexError:
        print("Error SimpleRequest index")
        exit()

    tokenDNAexp = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiI1ZjhlYjhhZDc1MTYxMjRlODczYTg0YmYiLCJhdXRoU291cmNlIjoiaW50ZXJuYWwiLCJ0ZW5hbnROYW1lIjoiVE5UMCIsInJvbGVzIjpbIjVlNWE0MzI2NzUxNjEyMDBjYzRhYzk2MyJdLCJ0ZW5hbnRJZCI6IjVlNWE0MzI1NzUxNjEyMDBjYzRhYzk1YyIsImV4cCI6MTYwNDMyMTkzOSwiaWF0IjoxNjA0MzE4MzM5LCJqdGkiOiI5NWI2ZmUyYS01OWYzLTQ4NTMtODliOC0wZDVjYjJmMTQ5YjciLCJ1c2VybmFtZSI6ImRldm5ldHVzZXIifQ.b4WGuTHFu97PvvheuCbjzXQfWlLbWSAKylt8vk_930MEtQC4FPjrn9FT_AiD6GTIpbWl6qW_NXfhbkVfw3R0rMu7XOJSFKpqRYDocGSz6oy2F6mURo41dhnKQ4tz2CEfgoFOUPOXahOypYw8vEBz__5iHf3quRe1Iqhnj4STntIHh7XoZoY_3Qj_G69ZsEycu-ooJ0BORnYPcVDBcYjmr2TbW6lOS1l-4PgnVxQBNO-uGYIv14H6C6kjMeJxHCurBMGWL0uln2Em5cgU7FvvvGAfy-9DYA5WMMGmBJVEFvT0MxZg8yrupbfoFDbhHmgPmXMo2Yeugb-spVzir_Bq5g"
    HTTP_Request_header_expired = {'x-auth-token': tokenDNAexp}
    try:
        response = requests.get(urlSimpleDNA, headers=HTTP_Request_header)
        response_error_resource = requests.get(urlSimpleDNAerror,
                                               headers=HTTP_Request_header)
        print(response.json())

        response_error_token = requests.get(
            urlSimpleDNA, headers=HTTP_Request_header_expired)
        if response_error_token.status_code == 401:
            print("\n response_error_token ", response_error_token.status_code)
            # catch 401 and regenarate token
        response_error_operation = requests.post(urlSimpleDNA,
                                                 headers=HTTP_Request_header)
        print("response_error_operation ",
              response_error_operation.status_code)
        print("response_error_resource ", response_error_resource.status_code)
    except Timeout as e:
        raise Timeout(e)
예제 #14
0
from requests.exceptions import Timeout
from requests.exceptions import TooManyRedirects
from requests.exceptions import InvalidURL

@pytest.mark.parametrize("payload_a,payload_b,expected", [
    ({ "foo": "bar"}, { "one": "two" }, { "foo": "bar", "one": "two" }),
])
def test_merge_payloads(core_client, payload_a, payload_b, expected):
    merged = core_client.merge_payloads(payload_a, payload_b)
    assert merged == expected

@pytest.mark.parametrize("resource,caught,raised", [
    ("foo", SSLError(), cpauto.SSLError),
    ("foo", ConnectionError(), cpauto.ConnectionError),
    ("foo", HTTPError(), cpauto.HTTPError),
    ("foo", Timeout(), cpauto.Timeout),
    ("foo", TooManyRedirects(), cpauto.TooManyRedirects),
    ("foo", InvalidURL(), cpauto.InvalidURL),
])
def test_http_post_exceptions(core_client, mgmt_server_base_uri, resource, caught, raised):
    endpoint = mgmt_server_base_uri + resource
    with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
        rsps.add(responses.POST, endpoint,
                 body=caught, status=200,
                 content_type='application/json')

        with pytest.raises(raised):
            r = core_client.http_post(endpoint=resource, payload={})

@pytest.mark.parametrize("params", [
    ({}),
예제 #15
0
class TestRestSensorSetup(unittest.TestCase):
    """Tests for setting up the REST sensor platform."""
    def setUp(self):
        """Set up things to be run when tests are started."""
        self.hass = get_test_home_assistant()
        self.addCleanup(self.hass.stop)

    def test_setup_missing_config(self):
        """Test setup with configuration missing required entries."""
        with assert_setup_component(0):
            assert setup_component(self.hass, sensor.DOMAIN,
                                   {"sensor": {
                                       "platform": "rest"
                                   }})

    def test_setup_missing_schema(self):
        """Test setup with resource missing schema."""
        with pytest.raises(PlatformNotReady):
            rest.setup_platform(
                self.hass,
                {
                    "platform": "rest",
                    "resource": "localhost",
                    "method": "GET"
                },
                None,
            )

    @patch("requests.Session.send",
           side_effect=requests.exceptions.ConnectionError())
    def test_setup_failed_connect(self, mock_req):
        """Test setup when connection error occurs."""
        with raises(PlatformNotReady):
            rest.setup_platform(
                self.hass,
                {
                    "platform": "rest",
                    "resource": "http://localhost",
                    "method": "GET"
                },
                lambda devices, update=True: None,
            )

    @patch("requests.Session.send", side_effect=Timeout())
    def test_setup_timeout(self, mock_req):
        """Test setup when connection timeout occurs."""
        with raises(PlatformNotReady):
            rest.setup_platform(
                self.hass,
                {
                    "platform": "rest",
                    "resource": "http://localhost",
                    "method": "GET"
                },
                lambda devices, update=True: None,
            )

    @requests_mock.Mocker()
    def test_setup_minimum(self, mock_req):
        """Test setup with minimum configuration."""
        mock_req.get("http://localhost", status_code=200)
        with assert_setup_component(1, "sensor"):
            assert setup_component(
                self.hass,
                "sensor",
                {
                    "sensor": {
                        "platform": "rest",
                        "resource": "http://localhost"
                    }
                },
            )
            self.hass.block_till_done()
        assert 2 == mock_req.call_count

    @requests_mock.Mocker()
    def test_setup_minimum_resource_template(self, mock_req):
        """Test setup with minimum configuration (resource_template)."""
        mock_req.get("http://localhost", status_code=200)
        with assert_setup_component(1, "sensor"):
            assert setup_component(
                self.hass,
                "sensor",
                {
                    "sensor": {
                        "platform": "rest",
                        "resource_template": "http://localhost",
                    }
                },
            )
            self.hass.block_till_done()
        assert mock_req.call_count == 2

    @requests_mock.Mocker()
    def test_setup_duplicate_resource(self, mock_req):
        """Test setup with duplicate resources."""
        mock_req.get("http://localhost", status_code=200)
        with assert_setup_component(0, "sensor"):
            assert setup_component(
                self.hass,
                "sensor",
                {
                    "sensor": {
                        "platform": "rest",
                        "resource": "http://localhost",
                        "resource_template": "http://localhost",
                    }
                },
            )
            self.hass.block_till_done()

    @requests_mock.Mocker()
    def test_setup_get(self, mock_req):
        """Test setup with valid configuration."""
        mock_req.get("http://localhost", status_code=200)
        with assert_setup_component(1, "sensor"):
            assert setup_component(
                self.hass,
                "sensor",
                {
                    "sensor": {
                        "platform": "rest",
                        "resource": "http://localhost",
                        "method": "GET",
                        "value_template": "{{ value_json.key }}",
                        "name": "foo",
                        "unit_of_measurement": DATA_MEGABYTES,
                        "verify_ssl": "true",
                        "timeout": 30,
                        "authentication": "basic",
                        "username": "******",
                        "password": "******",
                        "headers": {
                            "Accept": "application/json"
                        },
                    }
                },
            )
            self.hass.block_till_done()
        assert 2 == mock_req.call_count

    @requests_mock.Mocker()
    def test_setup_post(self, mock_req):
        """Test setup with valid configuration."""
        mock_req.post("http://localhost", status_code=200)
        with assert_setup_component(1, "sensor"):
            assert setup_component(
                self.hass,
                "sensor",
                {
                    "sensor": {
                        "platform": "rest",
                        "resource": "http://localhost",
                        "method": "POST",
                        "value_template": "{{ value_json.key }}",
                        "payload": '{ "device": "toaster"}',
                        "name": "foo",
                        "unit_of_measurement": DATA_MEGABYTES,
                        "verify_ssl": "true",
                        "timeout": 30,
                        "authentication": "basic",
                        "username": "******",
                        "password": "******",
                        "headers": {
                            "Accept": "application/json"
                        },
                    }
                },
            )
            self.hass.block_till_done()
        assert 2 == mock_req.call_count

    @requests_mock.Mocker()
    def test_setup_get_xml(self, mock_req):
        """Test setup with valid configuration."""
        mock_req.get("http://localhost", status_code=200)
        with assert_setup_component(1, "sensor"):
            assert setup_component(
                self.hass,
                "sensor",
                {
                    "sensor": {
                        "platform": "rest",
                        "resource": "http://localhost",
                        "method": "GET",
                        "value_template": "{{ value_json.key }}",
                        "name": "foo",
                        "unit_of_measurement": DATA_MEGABYTES,
                        "verify_ssl": "true",
                        "timeout": 30,
                        "authentication": "basic",
                        "username": "******",
                        "password": "******",
                        "headers": {
                            "Accept": "text/xml"
                        },
                    }
                },
            )
            self.hass.block_till_done()
        assert 2 == mock_req.call_count
예제 #16
0
    def request(self,
                method,
                url,
                params=None,
                data=None,
                headers=None,
                cookies=None,
                files=None,
                auth=None,
                timeout=None,
                allow_redirects=True,
                proxies=None,
                hooks=None,
                stream=None,
                verify=None,
                cert=None,
                **kwargs):
        """Constructs a :class:`Request <Request>`, prepares it and sends it.
        Returns :class:`Response <Response>` object.

        :param method: method for the new :class:`Request` object.
        :param url: URL for the new :class:`Request` object.
        :param params: (optional) Dictionary or bytes to be sent in the query
            string for the :class:`Request`.
        :param data: (optional) Dictionary or bytes to send in the body of the
            :class:`Request`.
        :param headers: (optional) Dictionary of HTTP Headers to send with the
            :class:`Request`.
        :param cookies: (optional) Dict or CookieJar object to send with the
            :class:`Request`.
        :param files: (optional) Dictionary of 'filename': file-like-objects
            for multipart encoding upload.
        :param auth: (optional) Auth tuple or callable to enable
            Basic/Digest/Custom HTTP Auth.
        :param timeout: (optional) Float describing the timeout of the
            request.
        :param allow_redirects: (optional) Boolean. Set to True by default.
        :param proxies: (optional) Dictionary mapping protocol to the URL of
            the proxy.
        :param stream: (optional) whether to immediately download the response
            content. Defaults to ``False``.
        :param verify: (optional) if ``True``, the SSL cert will be verified.
            A CA_BUNDLE path can also be provided.
        :param cert: (optional) if String, path to ssl client cert file (.pem).
            If Tuple, ('cert', 'key') pair.
        """
        #===============================================================================================================
        # add by mz
        error_type = kwargs.get("error_type")
        if error_type:
            from requests.exceptions import InvalidURL, URLRequired, ConnectTimeout, ConnectionError, SSLError, ReadTimeout
            from requests.exceptions import InvalidSchema, MissingSchema, ChunkedEncodingError, ContentDecodingError
            from requests.exceptions import RequestException, HTTPError, ProxyError, Timeout, RetryError, StreamConsumedError

            get_error = {
                "InvalidURL": InvalidURL(),
                "URLRequired": URLRequired(),
                "ConnectTimeout": ConnectTimeout(),
                "ConnectionError": ConnectionError(),
                "SSLError": SSLError(),
                "ReadTimeout": ReadTimeout(),
                "InvalidSchema": InvalidSchema(),
                "MissingSchema": MissingSchema(),
                "ChunkedEncodingError": ChunkedEncodingError(),
                "ContentDecodingError": ContentDecodingError(),
                "StreamConsumedError": StreamConsumedError(),
                "TooManyRedirects": TooManyRedirects(),
                "RequestException": RequestException(),
                "HTTPError": HTTPError(),
                "ProxyError": ProxyError(),
                "Timeout": Timeout(),
                "RetryError": RetryError
            }

            error_ = get_error[error_type]
            raise error_
        #===============================================================================================================

        method = builtin_str(method)

        # Create the Request.
        req = Request(
            method=method.upper(),
            url=url,
            headers=headers,
            files=files,
            data=data or {},
            params=params or {},
            auth=auth,
            cookies=cookies,
            hooks=hooks,
        )
        prep = self.prepare_request(req)

        proxies = proxies or {}

        # Gather clues from the surrounding environment.
        if self.trust_env:
            # Set environment's proxies.
            env_proxies = get_environ_proxies(url) or {}
            for (k, v) in env_proxies.items():
                proxies.setdefault(k, v)

            # Look for configuration.
            if not verify and verify is not False:
                verify = os.environ.get('REQUESTS_CA_BUNDLE')

            # Curl compatibility.
            if not verify and verify is not False:
                verify = os.environ.get('CURL_CA_BUNDLE')

        # Merge all the kwargs.
        proxies = merge_setting(proxies, self.proxies)
        stream = merge_setting(stream, self.stream)
        verify = merge_setting(verify, self.verify)
        cert = merge_setting(cert, self.cert)

        # Send the request.
        send_kwargs = {
            'stream': stream,
            'timeout': timeout,
            'verify': verify,
            'cert': cert,
            'proxies': proxies,
            'allow_redirects': allow_redirects,
        }
        resp = self.send(prep, **send_kwargs)

        return resp
예제 #17
0
class TestRestBinarySensorSetup(unittest.TestCase):
    """Tests for setting up the REST binary sensor platform."""

    DEVICES = []

    def add_devices(self, devices, update_before_add=False):
        """Mock add devices."""
        for device in devices:
            self.DEVICES.append(device)

    def setUp(self):
        """Set up things to be run when tests are started."""
        self.hass = get_test_home_assistant()
        # Reset for this test.
        self.DEVICES = []
        self.addCleanup(self.hass.stop)

    def test_setup_missing_config(self):
        """Test setup with configuration missing required entries."""
        with assert_setup_component(0):
            assert setup_component(self.hass, binary_sensor.DOMAIN,
                                   {"binary_sensor": {
                                       "platform": "rest"
                                   }})

    def test_setup_missing_schema(self):
        """Test setup with resource missing schema."""
        with pytest.raises(PlatformNotReady):
            rest.setup_platform(
                self.hass,
                {
                    "platform": "rest",
                    "resource": "localhost",
                    "method": "GET"
                },
                None,
            )

    @patch("requests.Session.send",
           side_effect=requests.exceptions.ConnectionError())
    def test_setup_failed_connect(self, mock_req):
        """Test setup when connection error occurs."""
        with raises(PlatformNotReady):
            rest.setup_platform(
                self.hass,
                {
                    "platform": "rest",
                    "resource": "http://localhost",
                    "method": "GET"
                },
                self.add_devices,
                None,
            )
        assert len(self.DEVICES) == 0

    @patch("requests.Session.send", side_effect=Timeout())
    def test_setup_timeout(self, mock_req):
        """Test setup when connection timeout occurs."""
        with raises(PlatformNotReady):
            rest.setup_platform(
                self.hass,
                {
                    "platform": "rest",
                    "resource": "http://localhost",
                    "method": "GET"
                },
                self.add_devices,
                None,
            )
        assert len(self.DEVICES) == 0

    @requests_mock.Mocker()
    def test_setup_minimum(self, mock_req):
        """Test setup with minimum configuration."""
        mock_req.get("http://localhost", status_code=200)
        with assert_setup_component(1, "binary_sensor"):
            assert setup_component(
                self.hass,
                "binary_sensor",
                {
                    "binary_sensor": {
                        "platform": "rest",
                        "resource": "http://localhost"
                    }
                },
            )
            self.hass.block_till_done()
        assert 1 == mock_req.call_count

    @requests_mock.Mocker()
    def test_setup_get(self, mock_req):
        """Test setup with valid configuration."""
        mock_req.get("http://localhost", status_code=200)
        with assert_setup_component(1, "binary_sensor"):
            assert setup_component(
                self.hass,
                "binary_sensor",
                {
                    "binary_sensor": {
                        "platform": "rest",
                        "resource": "http://localhost",
                        "method": "GET",
                        "value_template": "{{ value_json.key }}",
                        "name": "foo",
                        "verify_ssl": "true",
                        "authentication": "basic",
                        "username": "******",
                        "password": "******",
                        "headers": {
                            "Accept": "application/json"
                        },
                    }
                },
            )
            self.hass.block_till_done()
        assert 1 == mock_req.call_count

    @requests_mock.Mocker()
    def test_setup_post(self, mock_req):
        """Test setup with valid configuration."""
        mock_req.post("http://localhost", status_code=200)
        with assert_setup_component(1, "binary_sensor"):
            assert setup_component(
                self.hass,
                "binary_sensor",
                {
                    "binary_sensor": {
                        "platform": "rest",
                        "resource": "http://localhost",
                        "method": "POST",
                        "value_template": "{{ value_json.key }}",
                        "payload": '{ "device": "toaster"}',
                        "name": "foo",
                        "verify_ssl": "true",
                        "authentication": "basic",
                        "username": "******",
                        "password": "******",
                        "headers": {
                            "Accept": "application/json"
                        },
                    }
                },
            )
            self.hass.block_till_done()
        assert 1 == mock_req.call_count
예제 #18
0
파일: adapters.py 프로젝트: ftiannew/trip
    def send(self, request, stream=False, timeout=None, verify=True,
             cert=None, proxies=None):
        """Sends Request object. Returns Response object.

        :param request: The :class:`PreparedRequest <PreparedRequest>` being sent.
        :param stream: (optional) Whether to stream the request content.
        :param timeout: (optional) How long to wait for the server to send
            data before giving up, as a float, or a :ref:`(connect timeout,
            read timeout) <timeouts>` tuple.
        :type timeout: float or tuple
        :param verify: (optional) Whether to verify SSL certificates.
        :param cert: (optional) Any user-provided SSL certificate to be trusted.
        :param proxies: (optional) The proxies dictionary to apply to the request.
        :rtype: trip.adapters.MessageDelegate
        """
        if isinstance(timeout, tuple):
            try:
                connect_timeout, read_timeout = timeout
            except ValueError as e:
                # this may raise a string formatting error.
                err = ("Invalid timeout {0}. Pass a (connect, read) "
                       "timeout tuple, or a single float to set "
                       "both timeouts to the same value".format(timeout))
                raise ValueError(err)
        else:
            connect_timeout, read_timeout = timeout, timeout

        timeout_reason = {}
        if connect_timeout:
            timeout_reason['reason'] = 'while connecting'
            self.io_loop.add_timeout(
                self.io_loop.time() + connect_timeout,
                stack_context.wrap(functools.partial(self._on_timeout, timeout_reason)))

        s = yield self.tcp_client.connect(
            request.host, request.port,
            af=request.af,
            ssl_options=self._get_ssl_options(request, verify, cert),
            max_buffer_size=self.max_buffer_size)

        if not timeout_reason or timeout_reason.get('reason'):
            s.set_nodelay(True)
            timeout_reason.clear()
        else:
            raise gen.Return(Timeout(
                timeout_reason.get('error', 'unknown'),
                request=request))

        connection = HTTPConnection(
            s,
            HTTP1ConnectionParameters(
                no_keep_alive=True,
                max_header_size=self.max_header_size,
                max_body_size=self.max_body_size,
                decompress=request.decompress))

        if read_timeout:
            timeout_reason['reason'] = 'during request'
            self.io_loop.add_timeout(
                self.io_loop.time() + connect_timeout,
                stack_context.wrap(functools.partial(self._on_timeout, timeout_reason)))

        connection.write_headers(request.start_line, request.headers)
        if request.body is not None:
            connection.write(request.body) #TODO: partial sending
        connection.finish()

        future = Future()
        def handle_response(response):
            if isinstance(response, Exception):
                future.set_exception(response)
            else:
                future.set_result(response)
        resp = MessageDelegate(request, connection, handle_response, stream)

        headers_received = yield connection.read_headers(resp)

        if not stream and headers_received:
            yield connection.read_body(resp)

        if not timeout_reason or timeout_reason.get('reason'):
            timeout_reason.clear()
            resp = yield future
            raise gen.Return(resp)
        else:
            raise gen.Return(Timeout(
                timeout_reason.get('error', 'unknown'),
                request=request))
예제 #19
0
 def test_notify_timeout(self, marketplace, solitude, requests):
     self.set_secret_mock(solitude, 'f')
     requests.post.side_effect = Timeout('Timeout')
     self.notify()
예제 #20
0
def raise_timeout(*args, **kwargs):
    raise Timeout("Timeout")
예제 #21
0
 def gt():
     context['i'] += 1
     if context['i'] <= 1:
         raise Timeout()
     return self._build_transport_with_reply(responses.transaction_successful, pattern='wellsfargo.com')
예제 #22
0
def mock_timeout_response(*args, **kwargs):
    raise Timeout()
 def test_timeout(self, mock_get, configured_importer):
     mock_resp = mock_requests_response(status=None, raise_for_status=Timeout("TIMEOUT"))
     mock_get.return_value = mock_resp
     with pytest.raises(Timeout):
         configured_importer.get_loxone_structure_file()
 def raiser(*args, **kwargs):
     raise Timeout()
예제 #25
0
def fetch_file(url,
               domain_lock_enabled=True,
               outfile=None,
               headers=None,
               allow_redirects=True,
               verify_ssl=False,
               timeout=settings.SENTRY_SOURCE_FETCH_SOCKET_TIMEOUT,
               **kwargs):
    """
    Pull down a URL, returning a UrlResult object.
    """
    # lock down domains that are problematic
    if domain_lock_enabled:
        domain = urlparse(url).netloc
        domain_key = "source:blacklist:v2:%s" % (
            md5_text(domain).hexdigest(), )
        domain_result = cache.get(domain_key)
        if domain_result:
            domain_result["url"] = url
            raise CannotFetch(domain_result)

    logger.debug("Fetching %r from the internet", url)

    http_session = SafeSession()
    response = None

    try:
        try:
            start = time.time()
            response = http_session.get(url,
                                        allow_redirects=allow_redirects,
                                        verify=verify_ssl,
                                        headers=headers,
                                        timeout=timeout,
                                        stream=True,
                                        **kwargs)

            try:
                cl = int(response.headers["content-length"])
            except (LookupError, ValueError):
                cl = 0
            if cl > settings.SENTRY_SOURCE_FETCH_MAX_SIZE:
                raise OverflowError()

            return_body = False
            if outfile is None:
                outfile = six.BytesIO()
                return_body = True

            cl = 0

            # Only need to even attempt to read the response body if we
            # got a 200 OK
            if response.status_code == 200:
                for chunk in response.iter_content(16 * 1024):
                    if time.time(
                    ) - start > settings.SENTRY_SOURCE_FETCH_TIMEOUT:
                        raise Timeout()
                    outfile.write(chunk)
                    cl += len(chunk)
                    if cl > settings.SENTRY_SOURCE_FETCH_MAX_SIZE:
                        raise OverflowError()

        except Exception as exc:
            logger.debug("Unable to fetch %r", url, exc_info=True)
            if isinstance(exc, RestrictedIPAddress):
                error = {
                    "type": EventError.RESTRICTED_IP,
                    "url": expose_url(url)
                }
            elif isinstance(exc, SuspiciousOperation):
                error = {
                    "type": EventError.SECURITY_VIOLATION,
                    "url": expose_url(url)
                }
            elif isinstance(exc, (Timeout, ReadTimeout)):
                error = {
                    "type": EventError.FETCH_TIMEOUT,
                    "url": expose_url(url),
                    "timeout": settings.SENTRY_SOURCE_FETCH_TIMEOUT,
                }
            elif isinstance(exc, OverflowError):
                error = {
                    "type":
                    EventError.FETCH_TOO_LARGE,
                    "url":
                    expose_url(url),
                    # We want size in megabytes to format nicely
                    "max_size":
                    float(settings.SENTRY_SOURCE_FETCH_MAX_SIZE) / 1024 / 1024,
                }
            elif isinstance(exc,
                            (RequestException, ZeroReturnError, OpenSSLError)):
                error = {
                    "type": EventError.FETCH_GENERIC_ERROR,
                    "value": six.text_type(type(exc)),
                    "url": expose_url(url),
                }
            else:
                logger.exception(six.text_type(exc))
                error = {
                    "type": EventError.UNKNOWN_ERROR,
                    "url": expose_url(url)
                }

            # TODO(dcramer): we want to be less aggressive on disabling domains
            if domain_lock_enabled:
                cache.set(domain_key, error or "", 300)
                logger.warning("source.disabled", extra=error)
            raise CannotFetch(error)

        headers = {k.lower(): v for k, v in response.headers.items()}
        encoding = response.encoding

        body = None
        if return_body:
            body = outfile.getvalue()
            outfile.close()  # we only want to close StringIO

        result = (headers, body, response.status_code, encoding)
    finally:
        if response is not None:
            response.close()

    return UrlResult(url, result[0], result[1], result[2], result[3])
예제 #26
0
def ravem_api_call(api_endpoint, method='GET', **kwargs):
    """Emits a call to the given RAVEM API endpoint.

    This function is meant to be used to easily generate calls to the RAVEM API.
    The RAVEM URL, username and password are automatically fetched from the
    settings of the RAVEM plugin each time.

    :param api_endpoint: str -- The RAVEM API endpoint to call.
    :param method: str -- The HTTP method to use for the call, currently, RAVEM
                   only supports `GET` or `POST`
    :param **kwargs: The field names and values used for the RAVEM API as
                     strings

    :returns: dict -- The JSON-encoded response from the RAVEM
    :raises: HTTPError if the request returns an HTTP Error (400 or 500)
    :raises: RavemAPIException if RAVEM returns an error message
    """

    root_endpoint = RavemPlugin.settings.get('api_endpoint')
    username = RavemPlugin.settings.get('username')
    password = RavemPlugin.settings.get('password')
    headers = {'Accept': 'application/json'}
    timeout = RavemPlugin.settings.get('timeout') or None
    url = urljoin(root_endpoint, api_endpoint)

    if RavemPlugin.settings.get('debug') and method != 'GET':
        RavemPlugin.logger.debug('API call:\nURL: %s\nData: %s', url,
                                 pformat(kwargs))
        raise RavemAPIException('Action not possible in debug mode',
                                api_endpoint, None)

    try:
        response = requests.request(method,
                                    url,
                                    params=kwargs,
                                    headers=headers,
                                    auth=HTTPDigestAuth(username, password),
                                    timeout=timeout)
    except Timeout as error:
        RavemPlugin.logger.warning("%s %s timed out: %s", error.request.method,
                                   error.request.url, error.message)
        # request timeout sometime has an inner timeout error as message instead of a string.
        raise Timeout(_("Timeout while contacting the room."))
    except Exception as error:
        RavemPlugin.logger.exception("failed call: %s %s with %s: %s",
                                     method.upper(), api_endpoint, kwargs,
                                     error.message)
        raise

    try:
        response.raise_for_status()
    except HTTPError as error:
        RavemPlugin.logger.exception("%s %s failed with %s",
                                     response.request.method, response.url,
                                     error.message)
        raise

    json_response = response.json()
    if 'error' not in json_response and 'result' not in json_response:
        RavemPlugin.logger.exception(
            '%s %s returned json without a result or error: %s',
            response.request.method, response.url, json_response)
        err_msg = (
            "{response.request.method} {response.url} returned json without a result or error: "
            "{json_response}").format(response=response,
                                      json_response=json_response)
        raise RavemAPIException(err_msg, api_endpoint, response)

    return json_response
예제 #27
0
파일: test_cli.py 프로젝트: greganswer/mgit
import unittest
import mock
from click.testing import CliRunner
from requests.exceptions import Timeout, HTTPError
from subprocess import DEVNULL, CalledProcessError as ProcessError

from mgit.cli import cli
from mgit import configs
from mgit import git

BASE_BRANCH = "my_base_branch"
NEW_BRANCH = "jir-472-update-readme-file"
ISSUE_ID = "JIR-472"
ISSUE_TITLE = "JIR-472: Update Readme File"
ISSUE_TRACKER_API = "https://api.github.com/repos/fake_user/fake_repo/issues"
REQUEST_TIMEOUT = Timeout("HTTP Request Timeout")
REQUEST_HTTP_ERROR = HTTPError("HTTP Request Error")


class GitTestCase(unittest.TestCase):
    def setUp(self):
        self.runner = CliRunner()

    def test_cli(self):
        result = self.runner.invoke(cli)
        self.assertIn("Usage: mgit [OPTIONS] COMMAND [ARGS]...", result.output)
        self.assertEqual(0, result.exit_code)

    @mock.patch("mgit.git.create_branch")
    @mock.patch("mgit.issues.requests")
    def test_branch(self, mock_requests, mock_create_branch):
예제 #28
0
def fetch_file(url, project=None, release=None, allow_scraping=True):
    """
    Pull down a URL, returning a UrlResult object.

    Attempts to fetch from the cache.
    """
    if release:
        with metrics.timer('sourcemaps.release_file'):
            result = fetch_release_file(url, release)
    else:
        result = None

    cache_key = 'source:cache:v3:%s' % (md5_text(url).hexdigest(), )

    if result is None:
        if not allow_scraping or not url.startswith(('http:', 'https:')):
            error = {
                'type': EventError.JS_MISSING_SOURCE,
                'url': expose_url(url),
            }
            raise CannotFetchSource(error)

        logger.debug('Checking cache for url %r', url)
        result = cache.get(cache_key)
        if result is not None:
            # We got a cache hit, but the body is compressed, so we
            # need to decompress it before handing it off
            body = zlib.decompress(result[1])
            result = (result[0], force_text(body), result[2])

    if result is None:
        # lock down domains that are problematic
        domain = urlparse(url).netloc
        domain_key = 'source:blacklist:v2:%s' % (
            md5_text(domain).hexdigest(), )
        domain_result = cache.get(domain_key)
        if domain_result:
            domain_result['url'] = url
            raise CannotFetchSource(domain_result)

        headers = {}
        if project and is_valid_origin(url, project=project):
            token = project.get_option('sentry:token')
            if token:
                headers['X-Sentry-Token'] = token

        logger.debug('Fetching %r from the internet', url)

        with metrics.timer('sourcemaps.fetch'):
            http_session = http.build_session()
            response = None
            try:
                try:
                    start = time.time()
                    response = http_session.get(
                        url,
                        allow_redirects=True,
                        verify=False,
                        headers=headers,
                        timeout=settings.SENTRY_SOURCE_FETCH_SOCKET_TIMEOUT,
                        stream=True,
                    )

                    try:
                        cl = int(response.headers['content-length'])
                    except (LookupError, ValueError):
                        cl = 0
                    if cl > settings.SENTRY_SOURCE_FETCH_MAX_SIZE:
                        raise OverflowError()

                    contents = []
                    cl = 0

                    # Only need to even attempt to read the response body if we
                    # got a 200 OK
                    if response.status_code == 200:
                        for chunk in response.iter_content(16 * 1024):
                            if time.time(
                            ) - start > settings.SENTRY_SOURCE_FETCH_TIMEOUT:
                                raise Timeout()
                            contents.append(chunk)
                            cl += len(chunk)
                            if cl > settings.SENTRY_SOURCE_FETCH_MAX_SIZE:
                                raise OverflowError()

                except Exception as exc:
                    logger.debug('Unable to fetch %r', url, exc_info=True)
                    if isinstance(exc, RestrictedIPAddress):
                        error = {
                            'type': EventError.RESTRICTED_IP,
                            'url': expose_url(url),
                        }
                    elif isinstance(exc, SuspiciousOperation):
                        error = {
                            'type': EventError.SECURITY_VIOLATION,
                            'url': expose_url(url),
                        }
                    elif isinstance(exc, Timeout):
                        error = {
                            'type': EventError.JS_FETCH_TIMEOUT,
                            'url': expose_url(url),
                            'timeout': settings.SENTRY_SOURCE_FETCH_TIMEOUT,
                        }
                    elif isinstance(exc, OverflowError):
                        error = {
                            'type':
                            EventError.JS_TOO_LARGE,
                            'url':
                            expose_url(url),
                            # We want size in megabytes to format nicely
                            'max_size':
                            float(settings.SENTRY_SOURCE_FETCH_MAX_SIZE) /
                            1024 / 1024,
                        }
                    elif isinstance(exc, (RequestException, ZeroReturnError)):
                        error = {
                            'type': EventError.JS_GENERIC_FETCH_ERROR,
                            'value': six.text_type(type(exc)),
                            'url': expose_url(url),
                        }
                    else:
                        logger.exception(six.text_type(exc))
                        error = {
                            'type': EventError.UNKNOWN_ERROR,
                            'url': expose_url(url),
                        }

                    # TODO(dcramer): we want to be less aggressive on disabling domains
                    cache.set(domain_key, error or '', 300)
                    logger.warning('Disabling sources to %s for %ss',
                                   domain,
                                   300,
                                   exc_info=True)
                    raise CannotFetchSource(error)

                body = b''.join(contents)
                z_body = zlib.compress(body)
                headers = {k.lower(): v for k, v in response.headers.items()}
                text_body = body.decode(response.encoding or 'utf-8',
                                        'replace')

                cache.set(cache_key, (headers, z_body, response.status_code),
                          60)
                result = (headers, text_body, response.status_code)
            finally:
                if response is not None:
                    response.close()

    if result[2] != 200:
        logger.debug('HTTP %s when fetching %r', result[2], url, exc_info=True)
        error = {
            'type': EventError.JS_INVALID_HTTP_CODE,
            'value': result[2],
            'url': expose_url(url),
        }
        raise CannotFetchSource(error)

    # For JavaScript files, check if content is something other than JavaScript/JSON (i.e. HTML)
    # NOTE: possible to have JS files that don't actually end w/ ".js", but this should catch 99% of cases
    if url.endswith('.js'):
        # Check if response is HTML by looking if the first non-whitespace character is an open tag ('<').
        # This cannot parse as valid JS/JSON.
        # NOTE: not relying on Content-Type header because apps often don't set this correctly
        body_start = result[1][:20].lstrip(
        )  # Discard leading whitespace (often found before doctype)

        if body_start[:1] == u'<':
            error = {
                'type': EventError.JS_INVALID_CONTENT,
                'url': url,
            }
            raise CannotFetchSource(error)

    # Make sure the file we're getting back is six.text_type, if it's not,
    # it's either some encoding that we don't understand, or it's binary
    # data which we can't process.
    if not isinstance(result[1], six.text_type):
        try:
            result = (result[0], result[1].decode('utf8'), result[2])
        except UnicodeDecodeError:
            error = {
                'type': EventError.JS_INVALID_SOURCE_ENCODING,
                'value': 'utf8',
                'url': expose_url(url),
            }
            raise CannotFetchSource(error)

    return UrlResult(url, result[0], result[1])
예제 #29
0
파일: tests.py 프로젝트: oaleeapp/EguoWorld
 def test_network_errors(self):
     self.check_network_error(ConnectionError(), 'Connection error')
     self.check_network_error(Timeout(), 'Request timeout')
     self.check_network_error(TooManyRedirects(), 'Too many redirects')
 def test__irmc_redfish_get__exception(self, get):
     requests.Session.get.side_effect = Timeout()
     status, data, msg = irmc.irmc_redfish_get(self.mod, "redfish_path")
     self.assertEqual(status, 99)
     self.assertIn("Traceback", str(data))
     self.assertIn("GET request encountered exception (" + self.url + ")", msg)