def mock_perform_request(request): self.assertTrue( request.get_full_url().startswith(service_url + request_url)) self.assertEqual(request_body, request.body) self.assertEqual(request.method, 'PUT') self.assertEqual(request.headers['Content-Type'], content_type) return net_utils.make_fake_response(response_body, request.get_full_url())
def mock_perform_request(request): attempts.append(request) if len(attempts) == 1: raise net.HttpError(404, 'application/text; charset=ASCII', None) return net_utils.make_fake_response(response, request.get_full_url())
def test_url_retrieve(self): # Successfully reads the data. @contextlib.contextmanager def fake_open(_filepath, _mode): yield StringIO.StringIO() self.mock(__builtin__, 'open', fake_open) self.mock(net, 'url_open', lambda url, **_kwargs: net_utils.make_fake_response('111', url)) self.assertEqual( True, net.url_retrieve('filepath', 'https://localhost/test')) # Respects url_open connection errors. self.mock(net, 'url_open', lambda _url, **_kwargs: None) self.assertEqual( False, net.url_retrieve('filepath', 'https://localhost/test')) # Respects read timeout errors. def timeouting_http_response(url): def iter_content_mock(_size=None): raise net.TimeoutError() response = net_utils.make_fake_response('', url) self.mock(response, 'iter_content', iter_content_mock) return response removed = [] self.mock(os, 'remove', removed.append) self.mock(net, 'url_open', lambda url, **_kwargs: timeouting_http_response(url)) self.assertEqual( False, net.url_retrieve('filepath', 'https://localhost/test')) self.assertEqual(['filepath'], removed)
def timeouting_http_response(url): def iter_content_mock(_size=None): raise net.TimeoutError() response = net_utils.make_fake_response(b'', url) self.mock(response, 'iter_content', iter_content_mock) return response
def handle_request(request): self.assertTrue( request.get_full_url().startswith(service_url + request_url)) self.assertEqual('', request.body) self.assertEqual(u'Bearer %s' % token, request.headers['Authorization']) return net_utils.make_fake_response(response, request.get_full_url())
def mock_perform_request(request): attempts.append(request) if len(attempts) == 1: raise net_utils.make_fake_error(404, request.get_full_url(), headers={ 'Content-Type': 'application/text; charset=ASCII', }) return net_utils.make_fake_response(response, request.get_full_url())
def timeouting_http_response(url): def read_mock(_size=None): raise net.TimeoutError() response = net_utils.make_fake_response(b'', url) self.mock(response, 'read', read_mock) return response
def handle_request(request): self.assertTrue(request.get_full_url().startswith(service_url + request_url)) self.assertEqual('', request.body) self.assertIsNone(request.headers.get('Authorization')) return net_utils.make_fake_response(response, request.get_full_url())
def test_url_read(self): # Successfully reads the data. self.mock(net, 'url_open', lambda url, **_kwargs: net_utils.make_fake_response('111', url)) self.assertEqual(net.url_read('https://fake_url.com/test'), '111') # Respects url_open connection errors. self.mock(net, 'url_open', lambda _url, **_kwargs: None) self.assertIsNone(net.url_read('https://fake_url.com/test')) # Respects read timeout errors. def timeouting_http_response(url): def read_mock(_size=None): raise net.TimeoutError() response = net_utils.make_fake_response('', url) self.mock(response, 'read', read_mock) return response self.mock(net, 'url_open', lambda url, **_kwargs: timeouting_http_response(url)) self.assertIsNone(net.url_read('https://fake_url.com/test'))
def mock_perform_request(request): calls.append('request') if 'login' not in calls: raise net_utils.make_fake_error(403, request.get_full_url()) return net_utils.make_fake_response(response, request.get_full_url())
def mock_perform_request(request): attempts.append(request) if len(attempts) == 1: raise net_utils.make_fake_error(500, request.get_full_url()) return net_utils.make_fake_response(response, request.get_full_url())
def mock_perform_request(request): self.assertTrue( request.get_full_url().startswith(service_url + request_url)) self.assertEqual('', request.body) return net_utils.make_fake_response(response, request.get_full_url())
def mock_perform_request(request): attempts.append(request) if len(attempts) == 1: raise net.HttpError(500, 'text/plain', None) return net_utils.make_fake_response(response, request.get_full_url())
def mock_perform_request(request): calls.append('request') if 'login' not in calls: raise net.HttpError(403, 'text/plain', None) return net_utils.make_fake_response(response, request.get_full_url())
def timeouting_http_response(url): def iter_content_mock(_size=None): raise net.TimeoutError() response = net_utils.make_fake_response('', url) self.mock(response, 'iter_content', iter_content_mock) return response
def timeouting_http_response(url): def read_mock(_size=None): raise net.TimeoutError() response = net_utils.make_fake_response('', url) self.mock(response, 'read', read_mock) return response
def mock_perform_request(request): attempts.append(request) if len(attempts) == 1: raise net.ConnectionError() return net_utils.make_fake_response(response, request.get_full_url())