Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _mock_gce_get_project(projectid):
if projectid in results.GCE_GET_PROJECT:
return results.GCE_GET_PROJECT[projectid]
response = httplib2.Response(
{'status': '403', 'content-type': 'application/json'})
content = results.GCE_API_NOT_ENABLED_TEMPLATE.format(id=projectid)
error_403 = errors.HttpError(response, content)
raise api_errors.ApiNotEnabledError('Access Not Configured.', error_403)
def __init__(self, headers, content):
from httplib2 import Response
self._response = Response(headers)
self._content = content
def test_digest_object_stale():
credentials = ("joe", "password")
host = None
request_uri = "/digest/stale/"
headers = {}
response = httplib2.Response({})
response["www-authenticate"] = (
'Digest realm="myrealm", nonce="bd669f", '
'algorithm=MD5, qop="auth", stale=true'
)
response.status = 401
content = b""
d = httplib2.DigestAuthentication(
credentials, host, request_uri, headers, response, content, None
)
# Returns true to force a retry
assert d.response(response, content)
def test_errors(self):
errorResponse = httplib2.Response({'status': 500, 'reason': 'Server Error'})
requestBuilder = RequestMockBuilder({
'plus.activities.list': (errorResponse, '{}')
})
plus = build('plus', 'v1', http=self.http, requestBuilder=requestBuilder)
try:
activity = plus.activities().list(collection='public', userId='me').execute()
self.fail('An exception should have been thrown')
except HttpError, e:
self.assertEqual('{}', e.content)
self.assertEqual(500, e.resp.status)
self.assertEqual('Server Error', e.resp.reason)
def test_fetch_tab_not_found(mocker):
# Exception built by pasting content from a real exception generated
# via Ipython + a pdb trace put in the datasheets code
exception = apiclient.errors.HttpError(
resp=httplib2.Response({
'vary': 'Origin, X-Origin, Referer',
'content-type': 'application/json; charset=UTF-8',
'date': 'Tue, 10 Apr 2018 19:17:12 GMT',
'server': 'ESF',
'cache-control': 'private',
'x-xss-protection': '1; mode=block',
'x-frame-options': 'SAMEORIGIN',
'alt-svc': 'hq=":443"; ma=2592000; quic=51303432; quic=51303431; quic=51303339; quic=51303335,quic=":443"; ma=2592000; v="42,41,39,35"',
'transfer-encoding': 'chunked',
'status': '400',
'content-length': '271',
'-content-encoding': 'gzip'
}),
content=b"""
{
"error": {
def _mock_permission_denied(parentid):
response = httplib2.Response(
{'status': '403', 'content-type': 'application/json'})
content = results.GCP_PERMISSION_DENIED_TEMPLATE.format(id=parentid).\
encode()
error_403 = errors.HttpError(response, content)
raise api_errors.ApiExecutionError(parentid, error_403)
Returns:
A pair (resp, content), such as would be returned from httplib2.request.
"""
# Strip off the status line
status_line, payload = payload.split('\n', 1)
protocol, status, reason = status_line.split(' ', 2)
# Parse the rest of the response
parser = FeedParser()
parser.feed(payload)
msg = parser.close()
msg['status'] = status
# Create httplib2.Response from the parsed headers.
resp = httplib2.Response(msg)
resp.reason = reason
resp.version = int(protocol.split('/', 1)[1].replace('.', ''))
content = payload.split('\r\n\r\n', 1)[1]
return resp, content
def request(self, uri,
method='GET',
body=None,
headers=None,
redirections=1,
connection_type=None):
self.uri = uri
self.method = method
self.body = body
self.headers = headers
return httplib2.Response(self.response_headers), self.data
def __init__(self, resp, content, postproc):
"""Constructor for HttpRequestMock
Args:
resp: httplib2.Response, the response to emulate coming from the request
content: string, the response body
postproc: callable, the post processing function usually supplied by
the model class. See model.JsonModel.response() as an example.
"""
self.resp = resp
self.content = content
self.postproc = postproc
if resp is None:
self.resp = httplib2.Response({'status': 200, 'reason': 'OK'})
if 'reason' in self.resp:
self.resp.reason = self.resp['reason']