@@ -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+
174394class TestGitlabMethods (unittest .TestCase ):
175395 def setUp (self ):
176396 self .gl = Gitlab ("http://localhost" , private_token = "private_token" ,
0 commit comments