From bcecf35d69a4f5d285ed3f69cb0396cd09845def Mon Sep 17 00:00:00 2001 From: Andrew Lazarev Date: Tue, 3 Jun 2014 16:00:05 -0700 Subject: [PATCH] Don't set json content type for non-json data +corresponding unit test on http client added Change-Id: I8f2eacf595f1c73fea91b426b6c3fe6df4ad7bea Closes-Bug: #1317259 --- saharaclient/api/base.py | 4 +- saharaclient/api/httpclient.py | 18 +++---- saharaclient/tests/unit/test_httpclient.py | 56 ++++++++++++++++++++++ 3 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 saharaclient/tests/unit/test_httpclient.py diff --git a/saharaclient/api/base.py b/saharaclient/api/base.py index cf4c493b..732b4d5f 100644 --- a/saharaclient/api/base.py +++ b/saharaclient/api/base.py @@ -73,7 +73,7 @@ class ResourceManager(object): def _create(self, url, data, response_key=None, dump_json=True): if dump_json: data = json.dumps(data) - resp = self.api.client.post(url, data) + resp = self.api.client.post(url, data, json=dump_json) if resp.status_code != 202: self._raise_api_exception(resp) @@ -87,7 +87,7 @@ class ResourceManager(object): def _update(self, url, data, response_key=None, dump_json=True): if dump_json: data = json.dumps(data) - resp = self.api.client.put(url, data) + resp = self.api.client.put(url, data, json=dump_json) if resp.status_code != 202: self._raise_api_exception(resp) diff --git a/saharaclient/api/httpclient.py b/saharaclient/api/httpclient.py index 946e9c3f..f057c733 100644 --- a/saharaclient/api/httpclient.py +++ b/saharaclient/api/httpclient.py @@ -25,15 +25,17 @@ class HTTPClient(object): return requests.get(self.base_url + url, headers={'x-auth-token': self.token}) - def post(self, url, body): - return requests.post(self.base_url + url, body, - headers={'x-auth-token': self.token, - 'content-type': 'application/json'}) + def post(self, url, body, json=True): + headers = {'x-auth-token': self.token} + if json: + headers['content-type'] = 'application/json' + return requests.post(self.base_url + url, body, headers=headers) - def put(self, url, body): - return requests.put(self.base_url + url, body, - headers={'x-auth-token': self.token, - 'content-type': 'application/json'}) + def put(self, url, body, json=True): + headers = {'x-auth-token': self.token} + if json: + headers['content-type'] = 'application/json' + return requests.put(self.base_url + url, body, headers=headers) def delete(self, url): return requests.delete(self.base_url + url, diff --git a/saharaclient/tests/unit/test_httpclient.py b/saharaclient/tests/unit/test_httpclient.py new file mode 100644 index 00000000..e09dcbcb --- /dev/null +++ b/saharaclient/tests/unit/test_httpclient.py @@ -0,0 +1,56 @@ +# Copyright (c) 2013 Mirantis Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import mock +import testtools + +from saharaclient.api import httpclient + + +class ResourceTest(testtools.TestCase): + + @mock.patch('requests.post') + def test_post_json_content_type(self, mpost): + client = httpclient.HTTPClient("http://localhost", "token") + client.post('/test', '{"json":"True"}') + + self.assertEqual(1, mpost.call_count) + self.assertEqual('application/json', + mpost.call_args[1]['headers']["content-type"]) + + @mock.patch('requests.put') + def test_put_json_content_type(self, mput): + client = httpclient.HTTPClient("http://localhost", "token") + client.put('/test', '{"json":"True"}') + + self.assertEqual(1, mput.call_count) + self.assertEqual('application/json', + mput.call_args[1]['headers']["content-type"]) + + @mock.patch('requests.post') + def test_post_nonjson_content_type(self, mpost): + client = httpclient.HTTPClient("http://localhost", "token") + client.post('/test', 'nonjson', json=False) + + self.assertEqual(1, mpost.call_count) + self.assertNotIn("content-type", mpost.call_args[1]['headers']) + + @mock.patch('requests.put') + def test_put_nonjson_content_type(self, mput): + client = httpclient.HTTPClient("http://localhost", "token") + client.put('/test', 'nonjson', json=False) + + self.assertEqual(1, mput.call_count) + self.assertNotIn("content-type", mput.call_args[1]['headers'])