Exemplo n.º 1
0
    def test__command_fail_all_attempts(self):
        error = 'Connection Timeout'
        method = 'standby.run_image'
        image_info = {'image_id': 'test_image'}
        params = {'image_info': image_info}
        self.client.session.post.side_effect = [
            requests.Timeout(error),
            requests.Timeout(error),
            requests.Timeout(error),
            requests.Timeout(error)
        ]
        self.client._get_command_url(self.node)
        self.client._get_command_body(method, params)

        e = self.assertRaises(exception.AgentConnectionFailed,
                              self.client._command, self.node, method, params)
        self.assertEqual(
            'Connection to agent failed: Failed to connect to '
            'the agent running on node %(node)s for invoking '
            'command %(method)s. Error: %(error)s' % {
                'method': method,
                'node': self.node.uuid,
                'error': error
            }, str(e))
        self.assertEqual(3, self.client.session.post.call_count)
Exemplo n.º 2
0
 def test_timeout(self, httpretty):
     exception = requests.Timeout('Request timed out')
     httpretty.register_uri(httpretty.POST,
                            CHECK_ONE_URL,
                            body=exception_factory(exception))
     res = self.checker.check(self.resource)
     assert res is None
Exemplo n.º 3
0
    def request(self, method, url, *args, **kwargs):
        """
        Does a request, but raises Timeout immediately if site is known to timeout, and records sites that timeout.
        Also raises errors getting the content by default.
        """

        # Raise Timeout right away if site is known to timeout
        if is_unresponsive(url):
            raise requests.Timeout(
                'Requests to this site have timed out recently. Waiting before trying again.'
            )

        # Delay, if needed, before another request to this site
        wait_for_domain(url, self.domain_delay)

        kwargs.setdefault('timeout', self.timeout)
        raise_status = kwargs.pop('raise_status', True)

        # If we do not have an adapter for this url, pass it off to urllib
        if not any(url.startswith(adapter) for adapter in self.adapters):
            return _wrap_urlopen(url, timeout=kwargs['timeout'])

        try:
            result = requests.Session.request(self, method, url, *args,
                                              **kwargs)
        except (requests.Timeout, requests.ConnectionError):
            # Mark this site in known unresponsive list
            set_unresponsive(url)
            raise

        if raise_status:
            result.raise_for_status()

        return result
Exemplo n.º 4
0
def test_app_fetch_manifest_timeout(staff_user, staff_api_client,
                                    permission_manage_apps, monkeypatch):
    mocked_request = Mock()
    mocked_request.side_effect = requests.Timeout()
    monkeypatch.setattr("saleor.graphql.app.mutations.requests.get",
                        mocked_request)
    manifest_url = "http://localhost:3000/manifest-doesnt-exist"
    query = APP_FETCH_MANIFEST_MUTATION
    variables = {
        "manifest_url": manifest_url,
    }
    staff_user.user_permissions.set([permission_manage_apps])
    response = staff_api_client.post_graphql(
        query,
        variables=variables,
    )
    content = get_graphql_content(response)
    errors = content["data"]["appFetchManifest"]["errors"]

    assert len(errors) == 1
    assert errors[0] == {
        "field": "manifestUrl",
        "message": "The request to fetch manifest data timed out.",
        "code": "MANIFEST_URL_CANT_CONNECT",
    }
 def waitForCompletion(self, timeout=None):
     end = time.time() + (timeout or self.timeout)
     while self.status in self.nonterminal_states:
         if time.time() >= end:
             raise requests.Timeout()
         time.sleep(1)
     self.__on_completed()
Exemplo n.º 6
0
class AppSpiderClass:
    
    def SpiderA(self):
        
        appList =[]
        
        while True:
            urls =['https://en.aptoide.com/apps/latest/more','https://en.aptoide.com/apps/latest/more?offset=45','https://en.aptoide.com/apps/latest/more?offset=84','https://en.aptoide.com/apps/latest/more?offset=126','https://en.aptoide.com/apps/latest/more?offset=161','https://en.aptoide.com/apps/latest/more?offset=200','https://en.aptoide.com/apps/latest/more?offset=235','https://en.aptoide.com/apps/latest/more?offset=276','https://en.aptoide.com/apps/latest/more?offset=324','https://en.aptoide.com/apps/latest/more?offset=367','https://en.aptoide.com/apps/latest/more?offset=411','https://en.aptoide.com/apps/latest/more?offset=456','https://en.aptoide.com/apps/latest/more?offset=500','https://en.aptoide.com/apps/latest/more?offset=535','https://en.aptoide.com/apps/latest/more?offset=573','https://en.aptoide.com/apps/latest/more?offset=614','https://en.aptoide.com/apps/latest/more?offset=660','https://en.aptoide.com/apps/latest/more?offset=692','https://en.aptoide.com/apps/latest/more?offset=734','https://en.aptoide.com/apps/latest/more?offset=772','https://en.aptoide.com/apps/latest/more?offset=805','https://en.aptoide.com/apps/latest/more?offset=839','https://en.aptoide.com/apps/latest/more?offset=872','https://en.aptoide.com/apps/latest/more?offset=906','https://en.aptoide.com/apps/latest/more?offset=940']
            
            #try:
            for pagelink in urls:
                htmlPages = requests.get(pagelink)
                text = htmlPages.text
                soup = BeautifulSoup(text,"html.parser")
                
                self.FetchAptoideLinks(soup,appList)
                
                
            except requests.ConnectionError as connError:
                logger.log("Connection Error while connecting to Play store: ", urls," Error: ", connError)
            
            except requests.HTTPError as httpError:
                logger.log("Invalid HTTP response to Play store: ", urls, " Error: ", httpError)
            
            except requests.Timeout() as requestTimeoutError:
                logger.log("Time-out to connect to Play store: ", urls, " Error: ", requestTimeoutError)
Exemplo n.º 7
0
    def test_read_metadata_when_password_server_unreachable(self, m_password):
        """The read_metadata function returns partial results in case the
        password server (only) is unreachable."""
        expected_id = "12345"
        expected_hostname = "myname"
        expected_userdata = "#cloud-config"

        m_password.side_effect = requests.Timeout('Fake Connection Timeout')
        httpretty.register_uri(httpretty.GET,
                               self.userdata_url,
                               body=expected_userdata)
        httpretty.register_uri(httpretty.GET,
                               self.metadata_url,
                               body="instance-id\nlocal-hostname")
        httpretty.register_uri(httpretty.GET,
                               "{}local-hostname".format(self.metadata_url),
                               body=expected_hostname)
        httpretty.register_uri(httpretty.GET,
                               "{}instance-id".format(self.metadata_url),
                               body=expected_id)

        result = read_metadata()

        self.assertIsNone(result.get("password"))
        self.assertEqual(
            result.get("user-data").decode("utf-8"), expected_userdata)
Exemplo n.º 8
0
 def mock_catalog_contains_course_runs(
         self,
         course_run_ids,
         enterprise_customer_uuid,
         enterprise_customer_catalog_uuid=None,
         contains_content=True,
         raise_exception=False
 ):
     self.mock_access_token_response()
     query_params = urlencode({'course_run_ids': course_run_ids}, True)
     body = (
         requests.Timeout('Connection timed out.') if raise_exception else
         json.dumps({'contains_content_items': contains_content})
     )
     responses.add(
         method=responses.GET,
         url='{api_url}{enterprise_customer_uuid}/contains_content_items/?{query_params}'.format(
             api_url=self.ENTERPRISE_CATALOG_URL_CUSTOMER_RESOURCE,
             enterprise_customer_uuid=enterprise_customer_uuid,
             query_params=query_params
         ),
         body=body,
         content_type='application/json'
     )
     if enterprise_customer_catalog_uuid:
         responses.add(
             method=responses.GET,
             url='{api_url}{customer_catalog_uuid}/contains_content_items/?{query_params}'.format(
                 api_url=self.ENTERPRISE_CATALOG_URL,
                 customer_catalog_uuid=enterprise_customer_catalog_uuid,
                 query_params=query_params
             ),
             body=body,
             content_type='application/json'
         )
Exemplo n.º 9
0
    def stream(self, route):
        """ Makes an http request and returns an iterator over the response.

        Parameters
        ----------
        route :
            the http route (under this object's host) to request against.

        """

        url = self._build_url(route)

        start_time = time.perf_counter()
        response = requests.get(url, stream=True)
        response_b = None
        if "Content-length" in response.headers:
            response_b = float(response.headers["Content-length"])

        size_message = f"{response_b / 1024 ** 2:3.3f}MiB" if response_b is not None else "potentially large"
        logging.warning(f"downloading a {size_message} file from {url}")
        progress = tqdm(unit="B",
                        total=response_b,
                        unit_scale=True,
                        desc="Downloading")

        for chunk in response.iter_content(self.chunksize):
            if chunk:  # filter out keep-alive new chunks
                progress.update(len(chunk))
                yield chunk

            elapsed = time.perf_counter() - start_time
            if elapsed > self.timeout:
                raise requests.Timeout(
                    f"Download took {elapsed} seconds, but timeout was set to {self.timeout}"
                )
Exemplo n.º 10
0
 def post(self,
          url,
          data,
          timeout=HTTP_CLIENT_TIME_OUT,
          max_retries=HTTP_CLIENT_MAX_RETRIES,
          referer=HTTP_CLIENT_REFERER,
          **kw):
     """Post with headers.
     """
     for attempt_counter in range(max_retries):
         try:
             post_response = self.session.post(
                 url,
                 headers=self.create_headers(referer),
                 data=data,
                 timeout=timeout,
                 **kw)
             post_response.encoding = "utf-8"
             return post_response
         except requests.Timeout as identifier:
             logging.warning(
                 f'HTTPClient: ({attempt_counter + 1} / {max_retries}) Failed to POST `{data}` to `{url}`: {identifier}'
             )
             attempt_counter += 1
     raise requests.Timeout(
         f'HTTPClient: Max POST retries exceeded with url: {url}')
class TestAPIMethodExecute(TestAPIMethod):
    mock_response = mock.Mock()
    mock_response.status_code = 200
    mock_response.text = "{'status': 'success'}"
    mock_response.json = lambda: {'status': 'success'}

    mock_auth_error = mock.Mock()
    mock_auth_error.status_code = 401

    mock_invalid_json = "not-valid-json"

    @mock.patch('synapsepay.apibits.requester.Requester.request',
                side_effect=requests.Timeout())
    def test_api_error_on_failed_request(self, request_mock):
        with self.assertRaises(synapsepay.apibits.APIError):
            self.api_method.execute()

    @mock.patch('synapsepay.apibits.requester.Requester.request',
                return_value=mock_response)
    def test_response_parsed_as_json(self, request_mock):
        self.assertEqual({'status': 'success'}, self.api_method.execute())

    @mock.patch('synapsepay.apibits.requester.Requester.request',
                return_value=mock_auth_error)
    def test_401_status_authentication_error(self, request_mock):
        with self.assertRaises(synapsepay.apibits.AuthenticationError):
            self.api_method.execute()

    @mock.patch('requests.Response.content', mock_invalid_json)
    def test_invalid_json_api_error(self):
        with self.assertRaises(synapsepay.apibits.APIError):
            ret = self.api_method.execute()
Exemplo n.º 12
0
def download_file(url: str, dest_dir: str, timeout: float = None) -> str:
    """Downloads a file from a web URL to a directory.

    Args:
        url: File url as a string.
        dest_dir: Directory to download the file into, as a string.
        timeout: Maximum time in seconds downloading the file can take,
            as a float. The actual timeout will be a rough approximation to
            this, likely several seconds larger.

    Returns:
        Path to the downloaded file of the form
        <dest_dir>/<basename of the downloaded file>.

    Raises:
        requests.Timeout: Downloading timed out.
    """
    # 9.05 is the connect timeout and 27 is the read timeout. See
    # https://requests.readthedocs.io/en/master/user/advanced/#timeouts.
    with requests.get(url, stream=True, timeout=(9.05, 27)) as response:
        response.raise_for_status()
        filename = _get_filename(response)
        path = os.path.join(dest_dir, filename)
        with open(path, 'wb') as out:
            start = time.time()
            for data in response.iter_content(chunk_size=4096):
                out.write(data)
                if timeout is not None and time.time() - start > timeout:
                    raise requests.Timeout(f'Downloading {url} timed out')
        return path
Exemplo n.º 13
0
 def do_call_mqtt(self, payload, t):
     n = eva.notify.get_notifier(self._notifier_id)
     r = self.Response()
     if not n: return r
     if isinstance(payload, dict) and 'id' in payload:
         request_id = payload['id']
     else:
         request_id = str(uuid.uuid4())
     data = '{}|{}'.format(request_id, rapidjson.dumps(payload))
     cb = self.MQTTCallback()
     n.send_api_request(
         request_id, self._product_code + '/' + self._uri,
         '|{}|{}'.format(self._key_id,
                         self.ce.encrypt(data.encode()).decode()),
         cb.data_handler)
     if not cb.completed.wait(self._timeout):
         n.finish_api_request(request_id)
         raise requests.Timeout()
     if cb.code:
         try:
             r.text = self.ce.decrypt(cb.body.encode()).decode()
             if cb.code == 200:
                 r.ok = True
             r.status_code = cb.code
         except:
             eva.core.log_traceback()
             r.status_code = 403
     return r
Exemplo n.º 14
0
    def stream(self, route):
        """ Makes an http request and returns an iterator over the response.

        Parameters
        ----------
        route :
            the http route (under this object's host) to request against.

        """

        url = self._build_url(route)
        
        start_time = time.perf_counter()
        response = requests.get(url, stream=True)
        response_mb = None
        if "Content-length" in response.headers:
            response_mb = float(response.headers["Content-length"]) / 1024 ** 2

        for ii, chunk in enumerate(response.iter_content(self.chunksize)):
            if ii == 0:
                size_message = f"{response_mb:3.3}mb" if response_mb is not None else "potentially large"
                logging.warning(f"downloading a {size_message} file from {url}")
            yield chunk

            elapsed = time.perf_counter() - start_time
            if elapsed > self.timeout:
                raise requests.Timeout(f"Download took {elapsed} seconds, but timeout was set to {self.timeout}")
Exemplo n.º 15
0
    def test_raises_unavailable_when_request_timeout(self, responses):
        request_url = 'http://www.omdbapi.com/?apikey=my-api-key&t=Die+Hard'
        responses.add(responses.GET, request_url, match_querystring=True,
                      body=requests.Timeout())

        with pytest.raises(Unavailable):
            OMDBClient().get_rating_for_movie(title='Die Hard')
Exemplo n.º 16
0
    def test_source_fetch_fails(self, mock_get, mock_post):
        mock_get.side_effect = (requests_response(
            self.reply_html.replace('http://orig/post', 'bad'),
            content_type=CONTENT_TYPE_HTML), requests.Timeout('foo bar'))

        got = self.client.post('/webmention', data={'source': 'http://a/post'})
        self.assertEqual(502, got.status_code)
Exemplo n.º 17
0
def search():
    GREEN(f'[+] Searching for:\t{uname}')
    match = True
    FAILED = []
    SUCCESS = []
    try:
        for url in WEBSITES:
            requests.Timeout(0.5)
            r = requests.get(url)
            #r.close()
            if r.status_code == 200:
                GREEN(f"[+] {r.status_code}! {url} - Valid! ")
                SUCCESS.append(url)
            elif r.status_code != 200:
                RED(f"[-] {r.status_code}! {url} -  Failed! maybe this is a false positive"
                    )
                FAILED.append(url)
    except Exception as e:
        RED(f'Error has occurred {e}')

    YELLOW(f'[X] TOTAL = {len(WEBSITES)}')
    YELLOW(f'[X] SUCCESS = {len(SUCCESS)}')
    YELLOW(f'[X] FAILED = {len(FAILED)}')

    if (input('Show "SUCCESS" websites?:[y\\n]\t')) == 'Y' or 'y':
        x = 0
        for i in SUCCESS:
            x += 1
            print(f'{x}) ', i)
Exemplo n.º 18
0
def test_http_errors(monkeypatch):
    resp = MagicMock()
    resp.status_code = 404
    resp.raise_for_status.side_effect = requests.HTTPError('Not Found')
    get = MagicMock()
    get.return_value = resp
    monkeypatch.setattr('requests.get', get)
    http = HttpWrapper('http://example.org')
    # the code method will not raise an exception..
    assert 404 == http.code()
    for meth in ('time', 'json', 'cookies', 'headers'):
        with pytest.raises(HttpError) as ex:
            # ..but other methods will!
            getattr(http, meth)()
        assert 'Not Found' == ex.value.message

    get.side_effect = requests.Timeout('timed out')
    http = HttpWrapper('http://example.org')
    with pytest.raises(HttpError) as ex:
        http.time()
    assert 'timeout' == ex.value.message

    get.side_effect = requests.ConnectionError('connfail')
    http = HttpWrapper('http://example.org')
    with pytest.raises(HttpError) as ex:
        http.code()
    assert 'connection failed' == ex.value.message

    get.side_effect = Exception('foofail')
    http = HttpWrapper('http://example.org')
    with pytest.raises(HttpError) as ex:
        http.code()
    assert 'foofail' == ex.value.message
Exemplo n.º 19
0
def test_mocking():
    url = "http://ti.ny"

    # Multiple responses to the same url must be added before any request to it has been made
    responses.add(responses.GET, url, body='{"title": "Test Deal"}', content_type="application/json")
    responses.add(responses.GET, url, json=dict(a=10, b='foo'), status=401)
    responses.add(responses.GET, url, body=requests.HTTPError('Some HttpError'), status=500)
    responses.add(responses.GET, url, body=requests.Timeout('cannot do'), status=500)

    r = requests.get(url)
    assert r.json() == {"title": "Test Deal"}

    r = requests.get(url)
    assert r.json().get('a') == 10
    assert r.json().get('b') == 'foo'
    assert r.status_code == 401

    with pytest.raises(requests.HTTPError):
        requests.get(url)

    with pytest.raises(ValueError):
        try:
            requests.get(url)
        except requests.Timeout as e:
            raise ValueError(e)

    # Requests to url without a defined response will raise ConnectionError
    responses.add(responses.GET, 'http://www.google.com', body='OK')
    with pytest.raises(requests.ConnectionError):
        requests.get('http://nrk.no')
Exemplo n.º 20
0
 def wait_for_completion(self, timeout=None):
     """ Wait up to <timeout> seconds for the query to complete """
     end = time.time() + (timeout or self.timeout)
     while self.status in self.nonterminal_states:
         if time.time() >= end:
             raise requests.Timeout()
         time.sleep(1)
     self._on_completed()
Exemplo n.º 21
0
    def test_fb_with_timeout__timing_out(self):
        with mock.patch.object(self.fb_timeout.client.session, 'request') as request:
            request.side_effect = requests.Timeout('Timed out')
            with self.assertRaisesRegexp(Timeout, 'Timed out'):
                self.fb_timeout.make_request(self.test_url)

        request.assert_called_once()
        self.assertEqual(10, request.call_args[1]['timeout'])
Exemplo n.º 22
0
def _func_raise_requests_exception_once(val, timeout=None):
    if len(val) > 0:
        response = MagicMock()
        response.raise_for_status = lambda: None
        return response
    val.append(0)
    ex = requests.Timeout()
    raise ex
Exemplo n.º 23
0
 def setUp(self):
     """
     Sets up simple exceptions to wrap
     """
     super(NetworkErrorTests, self).setUp()
     self.exc = requests.RequestException("exc_message")
     self.timeout_exc = requests.Timeout("timeout_message")
     self.connection_exc = requests.ConnectionError("connection_message")
Exemplo n.º 24
0
 def test_render_home_if_blog_timeout(self):
     '''It should render the home page when blog time out'''
     exception = requests.Timeout('Blog timed out')
     httpretty.register_uri(httpretty.GET,
                            WP_ATOM_URL,
                            body=exception_factory(exception))
     response = self.get(url_for('site.home'))
     self.assert200(response)
Exemplo n.º 25
0
 def test_timeout(self):
     url = faker.uri()
     exception = requests.Timeout('Request timed out')
     httpretty.register_uri(httpretty.POST, CHECK_ONE_URL,
                            body=exception_factory(exception))
     response = self.get(url_for('api.checkurl'),
                         qs={'url': url, 'group': ''})
     self.assertStatus(response, 503)
Exemplo n.º 26
0
 def test_timeout_exception(self):
     with mock.patch('requests.get') as requests_get_mock:
         requests_get_mock.side_effect = requests.Timeout()
         with StdoutCapture() as output:
             self.api_caller.get_resource("/resource", DiscoveryContainer())
         self.assertIn(
             "ERROR::Get url=http://1.2.3.4:567/resource Error <class 'requests.exceptions.Timeout'>:;",
             output.raw)
    def __init__(self):
        self.__logger = Logger()

        self._request_exceptions = [
            type(item) for item in [
                requests.ConnectionError(),
                requests.HTTPError(),
                requests.TooManyRedirects(),
                requests.Timeout(),
                requests.TooManyRedirects(),
                requests.RequestException(),
                requests.ConnectTimeout(),
                requests.ReadTimeout()
            ]
        ]

        self._system_errors = [
            type(item) for item in [
                KeyError(),
                AttributeError(),
                IndexError(),
                ZeroDivisionError(),
                SystemError(),
                ValueError(),
                AssertionError()
            ]
        ]

        self._file_errors = [
            type(item) for item in [FileExistsError(),
                                    FileNotFoundError()]
        ]

        self._database_errors = [
            type(item) for item in [
                sqlite3.Error(),
                sqlite3.DataError(),
                sqlite3.ProgrammingError(),
                sqlite3.DatabaseError(),
                sqlite3.NotSupportedError(),
                sqlite3.IntegrityError(),
                sqlite3.InterfaceError(),
                sqlite3.InternalError(),
                sqlite3.OperationalError()
            ]
        ]

        self._speech_recognizer_errors = [
            type(item) for item in [
                sr.RequestError(),
                sr.UnknownValueError(),
                sr.WaitTimeoutError(),
                sr.RequestError()
            ]
        ]

        self.__logger.info('ExceptionsHandler was successfully initialized.',
                           __name__)
Exemplo n.º 28
0
 def test_timeout(self):
     responses.get('https://ipapi.co/192.0.2.0/json',
                   body=requests.Timeout("too slow"))
     with self.assertLogs(logger, level='ERROR') as logged:
         tz = ip_to_timezone('192.0.2.0')
     self.assertEqual(len(logged.output), 1)
     self.assertRegex(logged.output[0],
                      f'^ERROR:{logger.name}:External query timed out')
     self.assertEqual(tz, None)
Exemplo n.º 29
0
 def mock_enterprise_learner_api_raise_exception(self):
     """
     Helper function to register enterprise learner API endpoint and raise an exception.
     """
     self.mock_access_token_response()
     responses.add(
         method=responses.GET,
         url=self.ENTERPRISE_LEARNER_URL,
         body=requests.Timeout('Connection timed out.')
     )
Exemplo n.º 30
0
 def test_get_lms_footer_timeout(self):
     """ Verify proper logger message is displayed in case of a time out. """
     with mock.patch('requests.get', side_effect=requests.Timeout()):
         with LogCapture(LOGGER_NAME) as l:
             response = get_lms_footer()
             l.check((
                 LOGGER_NAME, 'ERROR',
                 u'Failed to retrieve data for {lms_url} provider, connection timeout'
                 .format(lms_url=settings.LMS_URL_ROOT)))
             self.assertIsNone(response)