def testResponseTooLarge(self): self._request.return_value = ({'status': '200'}, 'response') response = request.Request('https://example.com', use_cache=True) self.assertEqual(response, 'response') self.assertEqual(self._request.call_count, 1) response = request.Request('https://example.com', use_cache=True) self.assertEqual(response, 'response') self.assertEqual(self._request.call_count, 2)
def Retrieve(server, digest): # TODO(dberris): Migrate this to return a file-like object, so that we can do # incremental decoding and decompression, instead of attempting to load data # in memory for processing. url = server + '/_ah/api/isolateservice/v1/retrieve' body = { 'namespace': { 'namespace': 'default-gzip' }, 'digest': digest, } isolate_info = request.RequestJson(url, 'POST', body) if 'url' in isolate_info: # TODO(dberris): If this is a URL for a file in Google Cloud Storage, we # should use the file interface instead. return zlib.decompress(request.Request(isolate_info['url'], 'GET')) if 'content' in isolate_info: # TODO(dberris): If the data is inline in the response, then we should use # an adapter (like io.StringIO). return zlib.decompress(base64.b64decode(isolate_info['content'])) raise NotImplementedError( 'Isolate information for %s is in an unknown format: %s' % (digest, json.dumps(isolate_info)))
def testRequest(self): self._request.return_value = ({'status': '200'}, 'response') response = request.Request('https://example.com') self._service_account_http.assert_called_once_with(timeout=30) self._request.assert_called_once_with('https://example.com', method='GET') self.assertEqual(response, 'response')
def testHttpExceptionSuccessOnRetry(self): return_value = ({'status': '200'}, 'response') self._request.side_effect = httplib.HTTPException, return_value response = request.Request('https://example.com') self._request.assert_called_with('https://example.com', method='GET') self.assertEqual(self._request.call_count, 2) self.assertEqual(response, 'response')
def PostChangeComment(server_url, change_id, comment): url = '%s/a/changes/%s/revisions/current/review' % (server_url, change_id) request.Request(url, method='POST', body=comment, use_cache=False, use_auth=True, scope=GERRIT_SCOPE)
def testHttpErrorCodeSuccessOnRetry(self): failure_return_value = ({'status': '500'}, '') success_return_value = ({'status': '200'}, 'response') self._request.side_effect = failure_return_value, success_return_value response = request.Request('https://example.com') self._request.assert_called_with('https://example.com', method='GET') self.assertEqual(self._request.call_count, 2) self.assertEqual(response, 'response')
def testRequestWithBodyAndParameters(self): self._request.return_value = ({'status': '200'}, 'response') response = request.Request('https://example.com', 'POST', body='a string', url_param_1='value_1', url_param_2='value_2') self._request.assert_called_once_with( 'https://example.com?url_param_1=value_1&url_param_2=value_2', method='POST', body='"a string"', headers={'Content-Type': 'application/json'}) self.assertEqual(response, 'response')
def testNoAuth(self): http = mock.MagicMock() patcher = mock.patch('httplib2.Http') httplib2_http = patcher.start() httplib2_http.return_value = http self.addCleanup(patcher.stop) http.request.return_value = ({'status': '200'}, 'response') response = request.Request('https://example.com', use_auth=False) http.request.assert_called_once_with('https://example.com', method='GET') self.assertEqual(self._request.call_count, 0) self.assertEqual(response, 'response')
def Retrieve(digest): url = 'https://isolateserver.appspot.com/_ah/api/isolateservice/v1/retrieve' body = { 'namespace': {'namespace': 'default-gzip'}, 'digest': digest, } isolate_info = request.RequestJson(url, 'POST', body) if 'url' in isolate_info: return zlib.decompress(request.Request(isolate_info['url'], 'GET')) if 'content' in isolate_info: return zlib.decompress(base64.b64decode(isolate_info['content'])) raise NotImplementedError( 'Isolate information for %s is in an unknown format: %s' % (digest, json.dumps(isolate_info)))
def FileContents(repository_url, git_hash, path): """Fetches the contents of a file at a particular commit. Args: repository_url: The git url of the repository. git_hash: The git hash of the commit, or "HEAD". path: The path in the repository to the file. Returns: A string containing the file contents. Raises: NotFoundError: The repository, commit, or file was not found in Gitiles. httplib.HTTPException: A network or HTTP error occurred. """ url = '%s/+/%s/%s?format=TEXT' % (repository_url, git_hash, path) return base64.b64decode( request.Request(url, use_cache=_IsHash(git_hash), use_auth=False))
def testRequestBody(self): self._request.return_value = ({'status': '200'}, 'response') with self.assertRaises(NotImplementedError): request.Request('https://example.com', body='body', use_cache=True)
def testSocketError(self): self._request.side_effect = socket.error with self.assertRaises(socket.error): request.Request('https://example.com') self._request.assert_called_with('https://example.com', method='GET') self.assertEqual(self._request.call_count, 2)
def testInternalTransientError(self): self._request.side_effect = urlfetch_errors.InternalTransientError with self.assertRaises(urlfetch_errors.InternalTransientError): request.Request('https://example.com') self._request.assert_called_with('https://example.com', method='GET') self.assertEqual(self._request.call_count, 2)
def testHttpException(self): self._request.side_effect = httplib.HTTPException with self.assertRaises(httplib.HTTPException): request.Request('https://example.com') self._request.assert_called_with('https://example.com', method='GET') self.assertEqual(self._request.call_count, 2)
def testHttpErrorCode(self): self._request.return_value = ({'status': '500'}, '') with self.assertRaises(httplib.HTTPException): request.Request('https://example.com') self._request.assert_called_with('https://example.com', method='GET') self.assertEqual(self._request.call_count, 2)
def _TestRetry(self): response = request.Request('https://example.com') self._request.assert_called_with('https://example.com', method='GET') self.assertEqual(self._request.call_count, 2) self.assertEqual(response, 'response')
def testHttpNotAuthorized(self): self._request.return_value = ({'status': '403'}, b'\x00\xe2') with self.assertRaises(request.RequestError): request.Request('https://example.com')
def testNotFound(self): self._request.return_value = ({'status': '404'}, '') with self.assertRaises(request.NotFoundError): request.Request('https://example.com') self._request.assert_called_with('https://example.com', method='GET') self.assertEqual(self._request.call_count, 1)