Prevent changing content type in request

Now if body contains json, its content type forcibly
changes to application/json, which is not correct in
some case, like json-patch request.

This code fixes this situation and sets application/json
contnent type only if this header hasn't been set before.

Change-Id: I4e0c44d444519f056dfa48c9603dbc3ca6b01822
Closes-bug: #1634110
This commit is contained in:
Mike Fedosin 2016-10-17 15:50:28 +03:00
parent 626886b4aa
commit ab39cf0667
3 changed files with 25 additions and 2 deletions

View File

@ -543,7 +543,7 @@ class Session(object):
'for=%s;by=%s' % (self.original_ip, user_agent))
if json is not None:
headers['Content-Type'] = 'application/json'
headers.setdefault('Content-Type', 'application/json')
kwargs['data'] = self._json.encode(json)
for k, v in self.additional_headers.items():

View File

@ -284,6 +284,25 @@ class SessionTests(utils.TestCase):
session.get,
self.TEST_URL)
def test_json_content_type(self):
session = client_session.Session()
self.stub_url('POST', text='response')
resp = session.post(
self.TEST_URL,
json=[{'op': 'replace',
'path': '/name',
'value': 'new_name'}],
headers={'Content-Type': 'application/json-patch+json'})
self.assertEqual('POST', self.requests_mock.last_request.method)
self.assertEqual(resp.text, 'response')
self.assertTrue(resp.ok)
self.assertRequestBodyIs(
json=[{'op': 'replace',
'path': '/name',
'value': 'new_name'}])
self.assertContentTypeIs('application/json-patch+json')
class RedirectTests(utils.TestCase):

View File

@ -53,7 +53,7 @@ class TestCase(testtools.TestCase):
if json:
kwargs['text'] = jsonutils.dumps(json)
headers = kwargs.setdefault('headers', {})
headers['Content-Type'] = 'application/json'
headers.setdefault('Content-Type', 'application/json')
if parts:
url = '/'.join([p.strip('/') for p in [base_url] + parts])
@ -71,6 +71,10 @@ class TestCase(testtools.TestCase):
elif body:
self.assertEqual(body, last_request_body)
def assertContentTypeIs(self, content_type):
last_request = self.requests_mock.last_request
self.assertEqual(last_request.headers['Content-Type'], content_type)
def assertQueryStringIs(self, qs=''):
r"""Verify the QueryString matches what is expected.