Don't set json content type for non-json data
+corresponding unit test on http client added Change-Id: I8f2eacf595f1c73fea91b426b6c3fe6df4ad7bea Closes-Bug: #1317259
This commit is contained in:
committed by
Sergey Lukjanov
parent
5073933f07
commit
bcecf35d69
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
|
||||
56
saharaclient/tests/unit/test_httpclient.py
Normal file
56
saharaclient/tests/unit/test_httpclient.py
Normal file
@@ -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'])
|
||||
Reference in New Issue
Block a user