Skip to content

Commit ff82c88

Browse files
author
Gauvain Pocentek
committed
Tests and fixes for the http_* methods
1 parent f754f21 commit ff82c88

File tree

3 files changed

+230
-17
lines changed

3 files changed

+230
-17
lines changed

gitlab/__init__.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -683,8 +683,8 @@ def http_get(self, path, query_data={}, streamed=False, **kwargs):
683683
try:
684684
return result.json()
685685
except Exception:
686-
raise GitlaParsingError(
687-
message="Failed to parse the server message")
686+
raise GitlabParsingError(
687+
error_message="Failed to parse the server message")
688688
else:
689689
return result
690690

@@ -734,14 +734,11 @@ def http_post(self, path, query_data={}, post_data={}, **kwargs):
734734
"""
735735
result = self.http_request('post', path, query_data=query_data,
736736
post_data=post_data, **kwargs)
737-
if result.headers.get('Content-Type', None) == 'application/json':
738-
try:
739-
return result.json()
740-
except Exception:
741-
raise GitlabParsingError(
742-
message="Failed to parse the server message")
743-
else:
744-
return result.content
737+
try:
738+
return result.json()
739+
except Exception:
740+
raise GitlabParsingError(
741+
error_message="Failed to parse the server message")
745742

746743
def http_put(self, path, query_data={}, post_data={}, **kwargs):
747744
"""Make a PUT request to the Gitlab server.
@@ -767,7 +764,7 @@ def http_put(self, path, query_data={}, post_data={}, **kwargs):
767764
return result.json()
768765
except Exception:
769766
raise GitlabParsingError(
770-
message="Failed to parse the server message")
767+
error_message="Failed to parse the server message")
771768

772769
def http_delete(self, path, **kwargs):
773770
"""Make a PUT request to the Gitlab server.
@@ -814,15 +811,15 @@ def _query(self, url, query_data={}, **kwargs):
814811
self._data = result.json()
815812
except Exception:
816813
raise GitlabParsingError(
817-
message="Failed to parse the server message")
814+
error_message="Failed to parse the server message")
818815

819816
self._current = 0
820817

821818
def __iter__(self):
822819
return self
823820

824821
def __len__(self):
825-
return self._total_pages
822+
return int(self._total_pages)
826823

827824
def __next__(self):
828825
return self.next()

gitlab/exceptions.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ class GitlabHttpError(GitlabError):
5555
pass
5656

5757

58-
class GitlaParsingError(GitlabHttpError):
59-
pass
60-
61-
6258
class GitlabListError(GitlabOperationError):
6359
pass
6460

gitlab/tests/test_gitlab.py

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,226 @@ def resp_cont(url, request):
171171
self.assertEqual(resp.status_code, 404)
172172

173173

174+
class TestGitlabHttpMethods(unittest.TestCase):
175+
def setUp(self):
176+
self.gl = Gitlab("http://localhost", private_token="private_token",
177+
api_version=4)
178+
179+
def test_build_url(self):
180+
r = self.gl._build_url('http://localhost/api/v4')
181+
self.assertEqual(r, 'http://localhost/api/v4')
182+
r = self.gl._build_url('https://localhost/api/v4')
183+
self.assertEqual(r, 'https://localhost/api/v4')
184+
r = self.gl._build_url('/projects')
185+
self.assertEqual(r, 'http://localhost/api/v4/projects')
186+
187+
def test_http_request(self):
188+
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects",
189+
method="get")
190+
def resp_cont(url, request):
191+
headers = {'content-type': 'application/json'}
192+
content = '[{"name": "project1"}]'
193+
return response(200, content, headers, None, 5, request)
194+
195+
with HTTMock(resp_cont):
196+
http_r = self.gl.http_request('get', '/projects')
197+
http_r.json()
198+
self.assertEqual(http_r.status_code, 200)
199+
200+
def test_http_request_404(self):
201+
@urlmatch(scheme="http", netloc="localhost",
202+
path="/api/v4/not_there", method="get")
203+
def resp_cont(url, request):
204+
content = {'Here is wh it failed'}
205+
return response(404, content, {}, None, 5, request)
206+
207+
with HTTMock(resp_cont):
208+
self.assertRaises(GitlabHttpError,
209+
self.gl.http_request,
210+
'get', '/not_there')
211+
212+
def test_get_request(self):
213+
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects",
214+
method="get")
215+
def resp_cont(url, request):
216+
headers = {'content-type': 'application/json'}
217+
content = '{"name": "project1"}'
218+
return response(200, content, headers, None, 5, request)
219+
220+
with HTTMock(resp_cont):
221+
result = self.gl.http_get('/projects')
222+
self.assertIsInstance(result, dict)
223+
self.assertEqual(result['name'], 'project1')
224+
225+
def test_get_request_raw(self):
226+
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects",
227+
method="get")
228+
def resp_cont(url, request):
229+
headers = {'content-type': 'application/octet-stream'}
230+
content = 'content'
231+
return response(200, content, headers, None, 5, request)
232+
233+
with HTTMock(resp_cont):
234+
result = self.gl.http_get('/projects')
235+
self.assertEqual(result.content.decode('utf-8'), 'content')
236+
237+
def test_get_request_404(self):
238+
@urlmatch(scheme="http", netloc="localhost",
239+
path="/api/v4/not_there", method="get")
240+
def resp_cont(url, request):
241+
content = {'Here is wh it failed'}
242+
return response(404, content, {}, None, 5, request)
243+
244+
with HTTMock(resp_cont):
245+
self.assertRaises(GitlabHttpError, self.gl.http_get, '/not_there')
246+
247+
def test_get_request_invalid_data(self):
248+
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects",
249+
method="get")
250+
def resp_cont(url, request):
251+
headers = {'content-type': 'application/json'}
252+
content = '["name": "project1"]'
253+
return response(200, content, headers, None, 5, request)
254+
255+
with HTTMock(resp_cont):
256+
self.assertRaises(GitlabParsingError, self.gl.http_get,
257+
'/projects')
258+
259+
def test_list_request(self):
260+
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects",
261+
method="get")
262+
def resp_cont(url, request):
263+
headers = {'content-type': 'application/json', 'X-Total-Pages': 1}
264+
content = '[{"name": "project1"}]'
265+
return response(200, content, headers, None, 5, request)
266+
267+
with HTTMock(resp_cont):
268+
result = self.gl.http_list('/projects')
269+
self.assertIsInstance(result, GitlabList)
270+
self.assertEqual(len(result), 1)
271+
272+
with HTTMock(resp_cont):
273+
result = self.gl.http_list('/projects', all=True)
274+
self.assertIsInstance(result, list)
275+
self.assertEqual(len(result), 1)
276+
277+
def test_list_request_404(self):
278+
@urlmatch(scheme="http", netloc="localhost",
279+
path="/api/v4/not_there", method="get")
280+
def resp_cont(url, request):
281+
content = {'Here is wh it failed'}
282+
return response(404, content, {}, None, 5, request)
283+
284+
with HTTMock(resp_cont):
285+
self.assertRaises(GitlabHttpError, self.gl.http_list, '/not_there')
286+
287+
def test_list_request_invalid_data(self):
288+
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects",
289+
method="get")
290+
def resp_cont(url, request):
291+
headers = {'content-type': 'application/json'}
292+
content = '["name": "project1"]'
293+
return response(200, content, headers, None, 5, request)
294+
295+
with HTTMock(resp_cont):
296+
self.assertRaises(GitlabParsingError, self.gl.http_list,
297+
'/projects')
298+
299+
def test_post_request(self):
300+
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects",
301+
method="post")
302+
def resp_cont(url, request):
303+
headers = {'content-type': 'application/json'}
304+
content = '{"name": "project1"}'
305+
return response(200, content, headers, None, 5, request)
306+
307+
with HTTMock(resp_cont):
308+
result = self.gl.http_post('/projects')
309+
self.assertIsInstance(result, dict)
310+
self.assertEqual(result['name'], 'project1')
311+
312+
def test_post_request_404(self):
313+
@urlmatch(scheme="http", netloc="localhost",
314+
path="/api/v4/not_there", method="post")
315+
def resp_cont(url, request):
316+
content = {'Here is wh it failed'}
317+
return response(404, content, {}, None, 5, request)
318+
319+
with HTTMock(resp_cont):
320+
self.assertRaises(GitlabHttpError, self.gl.http_post, '/not_there')
321+
322+
def test_post_request_invalid_data(self):
323+
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects",
324+
method="post")
325+
def resp_cont(url, request):
326+
headers = {'content-type': 'application/json'}
327+
content = '["name": "project1"]'
328+
return response(200, content, headers, None, 5, request)
329+
330+
with HTTMock(resp_cont):
331+
self.assertRaises(GitlabParsingError, self.gl.http_post,
332+
'/projects')
333+
334+
def test_put_request(self):
335+
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects",
336+
method="put")
337+
def resp_cont(url, request):
338+
headers = {'content-type': 'application/json'}
339+
content = '{"name": "project1"}'
340+
return response(200, content, headers, None, 5, request)
341+
342+
with HTTMock(resp_cont):
343+
result = self.gl.http_put('/projects')
344+
self.assertIsInstance(result, dict)
345+
self.assertEqual(result['name'], 'project1')
346+
347+
def test_put_request_404(self):
348+
@urlmatch(scheme="http", netloc="localhost",
349+
path="/api/v4/not_there", method="put")
350+
def resp_cont(url, request):
351+
content = {'Here is wh it failed'}
352+
return response(404, content, {}, None, 5, request)
353+
354+
with HTTMock(resp_cont):
355+
self.assertRaises(GitlabHttpError, self.gl.http_put, '/not_there')
356+
357+
def test_put_request_invalid_data(self):
358+
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects",
359+
method="put")
360+
def resp_cont(url, request):
361+
headers = {'content-type': 'application/json'}
362+
content = '["name": "project1"]'
363+
return response(200, content, headers, None, 5, request)
364+
365+
with HTTMock(resp_cont):
366+
self.assertRaises(GitlabParsingError, self.gl.http_put,
367+
'/projects')
368+
369+
def test_delete_request(self):
370+
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects",
371+
method="delete")
372+
def resp_cont(url, request):
373+
headers = {'content-type': 'application/json'}
374+
content = 'true'
375+
return response(200, content, headers, None, 5, request)
376+
377+
with HTTMock(resp_cont):
378+
result = self.gl.http_delete('/projects')
379+
self.assertIsInstance(result, requests.Response)
380+
self.assertEqual(result.json(), True)
381+
382+
def test_delete_request_404(self):
383+
@urlmatch(scheme="http", netloc="localhost",
384+
path="/api/v4/not_there", method="delete")
385+
def resp_cont(url, request):
386+
content = {'Here is wh it failed'}
387+
return response(404, content, {}, None, 5, request)
388+
389+
with HTTMock(resp_cont):
390+
self.assertRaises(GitlabHttpError, self.gl.http_delete,
391+
'/not_there')
392+
393+
174394
class TestGitlabMethods(unittest.TestCase):
175395
def setUp(self):
176396
self.gl = Gitlab("http://localhost", private_token="private_token",

0 commit comments

Comments
 (0)