def test_send_http_request_no_follow(vector, mocker, response): session = requests.Session() response.is_redirect = False response.request.method = vector['method'] response.request.url = vector['url'] response.status_code = 200 # without proxy configs = { 'agent': "Mozilla/5.0", 'timeout': 30, 'proxy': "", 'follow': False } mocker.patch("requests.Session.request", return_value=response) test = utility.send_http_request(session, vector, configs) assert test == response # with proxy configs = { 'agent': "Mozilla/5.0", 'timeout': 30, 'proxy': "127.0.0.1:8080", 'follow': False } mocker.patch("requests.Session.request", return_value=response) test = utility.send_http_request(session, vector, configs) assert test == response
def test_send_http_requests_follow(vector, mocker, response): session = requests.Session() configs = {'agent': "Mozilla/5.0", 'timeout': 30, 'proxy': "127.0.0.1:8080", 'follow': True, 'domain': "", 'excludes': []} # set redirect redirect = response() redirect.is_redirect = True redirect.request.method = vector['method'] redirect.request.url = vector['url'] redirect.status_code = 302 redirect.next.url = vector['url'] + "/account" # set response non_redirect = response() non_redirect.is_redirect = False non_redirect.status_code = 200 non_redirect.history = [] # with follow and without redirect mocker.patch("requests.Session.request", return_value=non_redirect) mocker.patch("requests.Session.resolve_redirects", return_value=iter([])) test = utility.send_http_request(session, vector, configs) assert test == non_redirect assert test.history == [] # with follow and redirect mocker.patch("requests.Session.request", return_value=redirect) mocker.patch("requests.Session.resolve_redirects", return_value=iter([non_redirect])) test = utility.send_http_request(session, vector, configs) assert test == non_redirect assert test.history == [redirect]
def _send_request(self, vector): """ Wrapper method for sending GET and POST requests to server. It sets HTTP specific values, such as User-Agent, timeout, and proxy. It also handles exceptions and returns None, if the request was not successful. This behavior is intended to reduce the overhead involved with calling the method. :param vector: vector dictionary :return: response object or None """ try: # send http request response = utility.send_http_request(self._session, vector, self._configs) except (ConnectionError, ConnectionResetError, Timeout, TooManyRedirects) as e: err = re.findall("[A-Z][a-z]+", type(e).__name__) logger.warning("%s for '%s'. Ignoring.", ' '.join(err).capitalize(), parse.quote(vector['url'], safe='/:')) response = None return response