X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/1e368708ad2a2d553f5e716c473767c660b2f805..1ec89c1c31669eb89bd1997cfa9d3c50f0204dbe:/sdk/python/tests/test_api.py diff --git a/sdk/python/tests/test_api.py b/sdk/python/tests/test_api.py index ecb93a8dff..0d81fdf738 100644 --- a/sdk/python/tests/test_api.py +++ b/sdk/python/tests/test_api.py @@ -1,31 +1,26 @@ #!/usr/bin/env python -import apiclient.errors import arvados import httplib2 import json import mimetypes -import unittest import os import run_test_server +import unittest +from apiclient import errors as apiclient_errors +from apiclient import http as apiclient_http -from apiclient.http import RequestMockBuilder -from httplib import responses as HTTP_RESPONSES +from arvados_testutil import fake_httplib2_response if not mimetypes.inited: mimetypes.init() class ArvadosApiClientTest(unittest.TestCase): - @classmethod - def response_from_code(cls, code): - return httplib2.Response( - {'status': code, - 'reason': HTTP_RESPONSES.get(code, "Unknown Response"), - 'Content-Type': mimetypes.types_map['.json']}) + ERROR_HEADERS = {'Content-Type': mimetypes.types_map['.json']} @classmethod def api_error_response(cls, code, *errors): - return (cls.response_from_code(code), + return (fake_httplib2_response(code, **cls.ERROR_HEADERS), json.dumps({'errors': errors, 'error_token': '1234567890+12345678'})) @@ -38,13 +33,15 @@ class ArvadosApiClientTest(unittest.TestCase): # FIXME: Figure out a better way to stub this out. run_test_server.run() mock_responses = { - 'arvados.humans.delete': (cls.response_from_code(500), ""), + 'arvados.humans.delete': ( + fake_httplib2_response(500, **cls.ERROR_HEADERS), + ""), 'arvados.humans.get': cls.api_error_response( 422, "Bad UUID format", "Bad output format"), 'arvados.humans.list': (None, json.dumps( {'items_available': 0, 'items': []})), } - req_builder = RequestMockBuilder(mock_responses) + req_builder = apiclient_http.RequestMockBuilder(mock_responses) cls.api = arvados.api('v1', cache=False, host=os.environ['ARVADOS_API_HOST'], token='discovery-doc-only-no-token-needed', @@ -61,14 +58,14 @@ class ArvadosApiClientTest(unittest.TestCase): self.assertEqual(answer['items_available'], len(answer['items'])) def test_exceptions_include_errors(self): - with self.assertRaises(apiclient.errors.HttpError) as err_ctx: + with self.assertRaises(apiclient_errors.HttpError) as err_ctx: self.api.humans().get(uuid='xyz-xyz-abcdef').execute() err_s = str(err_ctx.exception) for msg in ["Bad UUID format", "Bad output format"]: self.assertIn(msg, err_s) def test_exceptions_without_errors_have_basic_info(self): - with self.assertRaises(apiclient.errors.HttpError) as err_ctx: + with self.assertRaises(apiclient_errors.HttpError) as err_ctx: self.api.humans().delete(uuid='xyz-xyz-abcdef').execute() self.assertIn("500", str(err_ctx.exception))