From ff82c88df5794dbf0020989cfc52412cefc4c176 Mon Sep 17 00:00:00 2001 From: Gauvain Pocentek Date: Mon, 29 May 2017 22:48:53 +0200 Subject: [PATCH] Tests and fixes for the http_* methods --- gitlab/__init__.py | 23 ++-- gitlab/exceptions.py | 4 - gitlab/tests/test_gitlab.py | 220 ++++++++++++++++++++++++++++++++++++ 3 files changed, 230 insertions(+), 17 deletions(-) diff --git a/gitlab/__init__.py b/gitlab/__init__.py index d42dbd339..57a91edcf 100644 --- a/gitlab/__init__.py +++ b/gitlab/__init__.py @@ -683,8 +683,8 @@ def http_get(self, path, query_data={}, streamed=False, **kwargs): try: return result.json() except Exception: - raise GitlaParsingError( - message="Failed to parse the server message") + raise GitlabParsingError( + error_message="Failed to parse the server message") else: return result @@ -734,14 +734,11 @@ def http_post(self, path, query_data={}, post_data={}, **kwargs): """ result = self.http_request('post', path, query_data=query_data, post_data=post_data, **kwargs) - if result.headers.get('Content-Type', None) == 'application/json': - try: - return result.json() - except Exception: - raise GitlabParsingError( - message="Failed to parse the server message") - else: - return result.content + try: + return result.json() + except Exception: + raise GitlabParsingError( + error_message="Failed to parse the server message") def http_put(self, path, query_data={}, post_data={}, **kwargs): """Make a PUT request to the Gitlab server. @@ -767,7 +764,7 @@ def http_put(self, path, query_data={}, post_data={}, **kwargs): return result.json() except Exception: raise GitlabParsingError( - message="Failed to parse the server message") + error_message="Failed to parse the server message") def http_delete(self, path, **kwargs): """Make a PUT request to the Gitlab server. @@ -814,7 +811,7 @@ def _query(self, url, query_data={}, **kwargs): self._data = result.json() except Exception: raise GitlabParsingError( - message="Failed to parse the server message") + error_message="Failed to parse the server message") self._current = 0 @@ -822,7 +819,7 @@ def __iter__(self): return self def __len__(self): - return self._total_pages + return int(self._total_pages) def __next__(self): return self.next() diff --git a/gitlab/exceptions.py b/gitlab/exceptions.py index 9f27c21f5..c9048a556 100644 --- a/gitlab/exceptions.py +++ b/gitlab/exceptions.py @@ -55,10 +55,6 @@ class GitlabHttpError(GitlabError): pass -class GitlaParsingError(GitlabHttpError): - pass - - class GitlabListError(GitlabOperationError): pass diff --git a/gitlab/tests/test_gitlab.py b/gitlab/tests/test_gitlab.py index c2cd19bf4..1710fff05 100644 --- a/gitlab/tests/test_gitlab.py +++ b/gitlab/tests/test_gitlab.py @@ -171,6 +171,226 @@ def resp_cont(url, request): self.assertEqual(resp.status_code, 404) +class TestGitlabHttpMethods(unittest.TestCase): + def setUp(self): + self.gl = Gitlab("http://localhost", private_token="private_token", + api_version=4) + + def test_build_url(self): + r = self.gl._build_url('http://localhost/api/v4') + self.assertEqual(r, 'http://localhost/api/v4') + r = self.gl._build_url('https://localhost/api/v4') + self.assertEqual(r, 'https://localhost/api/v4') + r = self.gl._build_url('/projects') + self.assertEqual(r, 'http://localhost/api/v4/projects') + + def test_http_request(self): + @urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects", + method="get") + def resp_cont(url, request): + headers = {'content-type': 'application/json'} + content = '[{"name": "project1"}]' + return response(200, content, headers, None, 5, request) + + with HTTMock(resp_cont): + http_r = self.gl.http_request('get', '/projects') + http_r.json() + self.assertEqual(http_r.status_code, 200) + + def test_http_request_404(self): + @urlmatch(scheme="http", netloc="localhost", + path="/api/v4/not_there", method="get") + def resp_cont(url, request): + content = {'Here is wh it failed'} + return response(404, content, {}, None, 5, request) + + with HTTMock(resp_cont): + self.assertRaises(GitlabHttpError, + self.gl.http_request, + 'get', '/not_there') + + def test_get_request(self): + @urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects", + method="get") + def resp_cont(url, request): + headers = {'content-type': 'application/json'} + content = '{"name": "project1"}' + return response(200, content, headers, None, 5, request) + + with HTTMock(resp_cont): + result = self.gl.http_get('/projects') + self.assertIsInstance(result, dict) + self.assertEqual(result['name'], 'project1') + + def test_get_request_raw(self): + @urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects", + method="get") + def resp_cont(url, request): + headers = {'content-type': 'application/octet-stream'} + content = 'content' + return response(200, content, headers, None, 5, request) + + with HTTMock(resp_cont): + result = self.gl.http_get('/projects') + self.assertEqual(result.content.decode('utf-8'), 'content') + + def test_get_request_404(self): + @urlmatch(scheme="http", netloc="localhost", + path="/api/v4/not_there", method="get") + def resp_cont(url, request): + content = {'Here is wh it failed'} + return response(404, content, {}, None, 5, request) + + with HTTMock(resp_cont): + self.assertRaises(GitlabHttpError, self.gl.http_get, '/not_there') + + def test_get_request_invalid_data(self): + @urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects", + method="get") + def resp_cont(url, request): + headers = {'content-type': 'application/json'} + content = '["name": "project1"]' + return response(200, content, headers, None, 5, request) + + with HTTMock(resp_cont): + self.assertRaises(GitlabParsingError, self.gl.http_get, + '/projects') + + def test_list_request(self): + @urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects", + method="get") + def resp_cont(url, request): + headers = {'content-type': 'application/json', 'X-Total-Pages': 1} + content = '[{"name": "project1"}]' + return response(200, content, headers, None, 5, request) + + with HTTMock(resp_cont): + result = self.gl.http_list('/projects') + self.assertIsInstance(result, GitlabList) + self.assertEqual(len(result), 1) + + with HTTMock(resp_cont): + result = self.gl.http_list('/projects', all=True) + self.assertIsInstance(result, list) + self.assertEqual(len(result), 1) + + def test_list_request_404(self): + @urlmatch(scheme="http", netloc="localhost", + path="/api/v4/not_there", method="get") + def resp_cont(url, request): + content = {'Here is wh it failed'} + return response(404, content, {}, None, 5, request) + + with HTTMock(resp_cont): + self.assertRaises(GitlabHttpError, self.gl.http_list, '/not_there') + + def test_list_request_invalid_data(self): + @urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects", + method="get") + def resp_cont(url, request): + headers = {'content-type': 'application/json'} + content = '["name": "project1"]' + return response(200, content, headers, None, 5, request) + + with HTTMock(resp_cont): + self.assertRaises(GitlabParsingError, self.gl.http_list, + '/projects') + + def test_post_request(self): + @urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects", + method="post") + def resp_cont(url, request): + headers = {'content-type': 'application/json'} + content = '{"name": "project1"}' + return response(200, content, headers, None, 5, request) + + with HTTMock(resp_cont): + result = self.gl.http_post('/projects') + self.assertIsInstance(result, dict) + self.assertEqual(result['name'], 'project1') + + def test_post_request_404(self): + @urlmatch(scheme="http", netloc="localhost", + path="/api/v4/not_there", method="post") + def resp_cont(url, request): + content = {'Here is wh it failed'} + return response(404, content, {}, None, 5, request) + + with HTTMock(resp_cont): + self.assertRaises(GitlabHttpError, self.gl.http_post, '/not_there') + + def test_post_request_invalid_data(self): + @urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects", + method="post") + def resp_cont(url, request): + headers = {'content-type': 'application/json'} + content = '["name": "project1"]' + return response(200, content, headers, None, 5, request) + + with HTTMock(resp_cont): + self.assertRaises(GitlabParsingError, self.gl.http_post, + '/projects') + + def test_put_request(self): + @urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects", + method="put") + def resp_cont(url, request): + headers = {'content-type': 'application/json'} + content = '{"name": "project1"}' + return response(200, content, headers, None, 5, request) + + with HTTMock(resp_cont): + result = self.gl.http_put('/projects') + self.assertIsInstance(result, dict) + self.assertEqual(result['name'], 'project1') + + def test_put_request_404(self): + @urlmatch(scheme="http", netloc="localhost", + path="/api/v4/not_there", method="put") + def resp_cont(url, request): + content = {'Here is wh it failed'} + return response(404, content, {}, None, 5, request) + + with HTTMock(resp_cont): + self.assertRaises(GitlabHttpError, self.gl.http_put, '/not_there') + + def test_put_request_invalid_data(self): + @urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects", + method="put") + def resp_cont(url, request): + headers = {'content-type': 'application/json'} + content = '["name": "project1"]' + return response(200, content, headers, None, 5, request) + + with HTTMock(resp_cont): + self.assertRaises(GitlabParsingError, self.gl.http_put, + '/projects') + + def test_delete_request(self): + @urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects", + method="delete") + def resp_cont(url, request): + headers = {'content-type': 'application/json'} + content = 'true' + return response(200, content, headers, None, 5, request) + + with HTTMock(resp_cont): + result = self.gl.http_delete('/projects') + self.assertIsInstance(result, requests.Response) + self.assertEqual(result.json(), True) + + def test_delete_request_404(self): + @urlmatch(scheme="http", netloc="localhost", + path="/api/v4/not_there", method="delete") + def resp_cont(url, request): + content = {'Here is wh it failed'} + return response(404, content, {}, None, 5, request) + + with HTTMock(resp_cont): + self.assertRaises(GitlabHttpError, self.gl.http_delete, + '/not_there') + + class TestGitlabMethods(unittest.TestCase): def setUp(self): self.gl = Gitlab("http://localhost", private_token="private_token",