def _response_generator(bad_status_code): # TODO: Should this be a mock? We shouldn't know any more than # necessary about the interface of Response bad_response = Response({}) bad_response.status = bad_status_code good_response = Response({}) good_response.status = 200 content = 'some content' return [(bad_response, content), (bad_response, content), (good_response, content)]
def _response_generator(bad_status_code, reason): # TODO: Should this be a mock? We shouldn't know any more than # necessary about the interface of Response bad_response = Response({}) bad_response.status = bad_status_code bad_response.reason = reason good_response = Response({}) good_response.status = 200 content = 'some content' return [(bad_response, content), (bad_response, content), (good_response, content)]
def test_request_raises_error_after_5_retries(self, mock_sleep, mock_request): service_unavailable_response = Response({}) service_unavailable_response.status = 503 content = 'content' mock_request.return_value = (service_unavailable_response, content) response = HttpWithBackoff().request('http://fake.com', method="PUT", body="bar", headers="foo", redirections=10, connection_type="wibble") assert_equal(response, (service_unavailable_response, content)) assert_equal( [call(10), call(20), call(40), call(80), call(160)], mock_sleep.call_args_list) mock_request.assert_called_with('http://fake.com', "PUT", "bar", "foo", 10, "wibble")
def test_request_does_not_sleep_on_404(self, mock_sleep, mock_request): not_found_response = Response({}) not_found_response.status = 404 content = 'content' mock_request.return_value = (not_found_response, content) HttpWithBackoff().request('http://fake.com', method="PUT", body="bar", headers="foo", redirections=10, connection_type="wibble") assert_equal( [], mock_sleep.call_args_list) mock_request.assert_called_with('http://fake.com', "PUT", "bar", "foo", 10, "wibble")
def test_request_raises_error_after_5_retries(self, mock_sleep, mock_request): service_unavailable_response = Response({}) service_unavailable_response.status = 403 content = google_error_response(403, "userRateLimitExceeded") logging.info(content) mock_request.return_value = (service_unavailable_response, content) response = HttpWithBackoff().request('http://fake.com', method="PUT", body="bar", headers="foo", redirections=10, connection_type="wibble") assert_equal(response, (service_unavailable_response, content)) assert_equal( [call(10), call(20), call(40), call(80), call(160)], mock_sleep.call_args_list) mock_request.assert_called_with('http://fake.com', "PUT", "bar", "foo", 10, "wibble")
def test_request_treats_403_usageLimits_userRateLimitExceededUnreg_as_bad_error(self, mock_error, mock_sleep, mock_request): bad_response = Response({}) bad_response.status = 403 content = google_error_response( 403, 'usageLimits.userRateLimitExceededUnreg') mock_request.return_value = (bad_response, content) HttpWithBackoff().request('http://fake.com', method="PUT", body="bar", headers="foo", redirections=10, connection_type="wibble") assert_equal( [], mock_sleep.call_args_list) mock_request.assert_called_with('http://fake.com', "PUT", "bar", "foo", 10, "wibble") mock_error.assert_called()
def test_request_does_not_sleep_on_502(self, mock_sleep, mock_request): backendError = Response({}) backendError.status = 502 content = google_error_response(502, "we made it up") mock_request.return_value = (backendError, content) HttpWithBackoff().request('http://fake.com', method="PUT", body="bar", headers="foo", redirections=10, connection_type="wibble") assert_equal( [], mock_sleep.call_args_list) mock_request.assert_called_with('http://fake.com', "PUT", "bar", "foo", 10, "wibble")
def test_request_with_only_uri_calls_Http_with_default_params( self, mock_request): response = Response({}) response.status = 200 mock_request.return_value = (response, 'content') HttpWithBackoff().request('http://fake.com') mock_request.assert_called_with('http://fake.com', "GET", None, None, 5, None)
def test_request_with_full_args_calls_Http_with_args(self, mock_request): response = Response({}) response.status = 200 mock_request.return_value = (response, 'content') HttpWithBackoff().request('http://fake.com', method="PUT", body="bar", headers="foo", redirections=10, connection_type="wibble") mock_request.assert_called_with('http://fake.com', "PUT", "bar", "foo", 10, "wibble")
def test_request_with_only_uri_calls_Http_with_default_params( self, mock_request): response = Response({}) response.status = 200 mock_request.return_value = (response, 'content') HttpWithBackoff().request('http://fake.com') mock_request.assert_called_with('http://fake.com', "GET", None, None, 5, None)
def test_request_does_not_sleep_on_503(self, mock_sleep, mock_request): backendError = Response({}) backendError.status = 503 content = google_error_response(503, "backendError") mock_request.return_value = (backendError, content) HttpWithBackoff().request('http://fake.com', method="PUT", body="bar", headers="foo", redirections=10, connection_type="wibble") assert_equal([], mock_sleep.call_args_list) mock_request.assert_called_with('http://fake.com', "PUT", "bar", "foo", 10, "wibble")
def test_request_does_not_sleep_on_404(self, mock_sleep, mock_request): not_found_response = Response({}) not_found_response.status = 404 content = 'content' mock_request.return_value = (not_found_response, content) HttpWithBackoff().request('http://fake.com', method="PUT", body="bar", headers="foo", redirections=10, connection_type="wibble") assert_equal([], mock_sleep.call_args_list) mock_request.assert_called_with('http://fake.com', "PUT", "bar", "foo", 10, "wibble")
def test_request_with_full_args_calls_Http_with_args( self, mock_request): response = Response({}) response.status = 200 mock_request.return_value = (response, 'content') HttpWithBackoff().request('http://fake.com', method="PUT", body="bar", headers="foo", redirections=10, connection_type="wibble") mock_request.assert_called_with('http://fake.com', "PUT", "bar", "foo", 10, "wibble")
def test_request_treats_403_dailyLimitExceeded_as_bad_error( self, mock_error, mock_sleep, mock_request): bad_response = Response({}) bad_response.status = 403 content = google_error_response(403, 'dailyLimitExceeded') mock_request.return_value = (bad_response, content) HttpWithBackoff().request('http://fake.com', method="PUT", body="bar", headers="foo", redirections=10, connection_type="wibble") assert_equal([], mock_sleep.call_args_list) mock_request.assert_called_with('http://fake.com', "PUT", "bar", "foo", 10, "wibble") mock_error.assert_called()
def _nb_request(self, method, path, params): uri, method, body, headers = self._build_request_args(path, method, params) self._display_debug(uri, method, body) self.buffer = '' http_client = http.HttpClient() #AsyncHTTPClient() logging.warning("request: "+method+" "+uri) #http_request = http_client.request(method=method,uri,data=body,headers=headers) try: #response = yield http_client.get('https://github.com/timeline.json') res = yield from http_client.request(method,uri,data=body,headers=headers, process_data=self._process_data,post_request=self._post_request) except: traceback.print_exc() try: fakeresponse = Response(res.headers) fakeresponse.status = res.code rc = self.response_class((fakeresponse, self.buffer),self.config) except: traceback.print_exc() return rc
def getNumberOfAvailableServers(self, getAvailUrl): response = Response({"status": 200, "Content": "Test"}) response.status = 200 return response, "128"