Skip to content

Commit

Permalink
Tests and fixes for the http_* methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Gauvain Pocentek committed Jun 2, 2017
1 parent f754f21 commit ff82c88
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 17 deletions.
23 changes: 10 additions & 13 deletions gitlab/__init__.py
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -814,15 +811,15 @@ 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

def __iter__(self):
return self

def __len__(self):
return self._total_pages
return int(self._total_pages)

def __next__(self):
return self.next()
Expand Down
4 changes: 0 additions & 4 deletions gitlab/exceptions.py
Expand Up @@ -55,10 +55,6 @@ class GitlabHttpError(GitlabError):
pass


class GitlaParsingError(GitlabHttpError):
pass


class GitlabListError(GitlabOperationError):
pass

Expand Down
220 changes: 220 additions & 0 deletions gitlab/tests/test_gitlab.py
Expand Up @@ -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",
Expand Down

0 comments on commit ff82c88

Please sign in to comment.