From c871089eabd996df11b45a2084a016b83e4b9a29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Thu, 7 Jul 2016 14:58:34 +0200 Subject: [PATCH] Added AbstractPillarTest.{get,post,put,post,delete} utility functions. These functions set the correct Content-Type header when JSON is given, add a resp.json() function that also checks the response mime type, and adds the correct Authorization header. --- tests/common_test_class.py | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/common_test_class.py b/tests/common_test_class.py index 6598420b..9bb9fd67 100644 --- a/tests/common_test_class.py +++ b/tests/common_test_class.py @@ -213,3 +213,45 @@ class AbstractPillarTest(TestMinimal): service.fetch_role_to_group_id_map() return group_ids + + def client_request(self, method, url, expected_status=200, auth_token=None, json=None, + data=None): + """Performs a HTTP request to the server.""" + + from application.utils import dumps + import json as mod_json + + headers = {} + if auth_token is not None: + headers['Authorization'] = self.make_header(auth_token) + + if json is not None: + data = dumps(json) + headers['Content-Type'] = 'application/json' + + resp = self.client.open(url, method=method, data=data, headers=headers) + self.assertEqual(expected_status, resp.status_code, resp.data) + + def json(): + if resp.mimetype != 'application/json': + raise TypeError('Unable to load JSON from mimetype %r' % resp.mimetype) + return mod_json.loads(resp.data) + + resp.json = json + + return resp + + def get(self, *args, **kwargs): + return self.client_request('GET', *args, **kwargs) + + def post(self, *args, **kwargs): + return self.client_request('POST', *args, **kwargs) + + def put(self, *args, **kwargs): + return self.client_request('PUT', *args, **kwargs) + + def delete(self, *args, **kwargs): + return self.client_request('DELETE', *args, **kwargs) + + def patch(self, *args, **kwargs): + return self.client_request('PATCH', *args, **kwargs)