No headers in body for create and update

Headers were being passed in the body for create and update.

Change-Id: I31689758880a887d993ba7e0597b8b8b9d542dae
This commit is contained in:
TerryHowe
2015-05-15 15:04:38 -06:00
parent 1a7fe93f84
commit a309e52839
2 changed files with 43 additions and 5 deletions

View File

@@ -504,6 +504,7 @@ class Resource(collections.MutableMapping):
# Convert attributes from Resource types into their ids. # Convert attributes from Resource types into their ids.
attrs = cls._convert_ids(attrs) attrs = cls._convert_ids(attrs)
headers = attrs.pop(HEADERS, None)
if cls.resource_key: if cls.resource_key:
body = {cls.resource_key: attrs} body = {cls.resource_key: attrs}
@@ -514,12 +515,14 @@ class Resource(collections.MutableMapping):
url = cls.base_path % path_args url = cls.base_path % path_args
else: else:
url = cls.base_path url = cls.base_path
args = {'json': body}
if headers:
args[HEADERS] = headers
if resource_id: if resource_id:
url = utils.urljoin(url, resource_id) url = utils.urljoin(url, resource_id)
resp = session.put(url, service=cls.service, json=body).body resp = session.put(url, service=cls.service, **args).body
else: else:
resp = session.post(url, service=cls.service, resp = session.post(url, service=cls.service, **args).body
json=body).body
if cls.resource_key: if cls.resource_key:
resp = resp[cls.resource_key] resp = resp[cls.resource_key]
@@ -709,6 +712,7 @@ class Resource(collections.MutableMapping):
attrs = cls._convert_ids(attrs) attrs = cls._convert_ids(attrs)
if attrs and cls.id_attribute in attrs: if attrs and cls.id_attribute in attrs:
del attrs[cls.id_attribute] del attrs[cls.id_attribute]
headers = attrs.pop(HEADERS, None)
if cls.resource_key: if cls.resource_key:
body = {cls.resource_key: attrs} body = {cls.resource_key: attrs}
@@ -720,10 +724,13 @@ class Resource(collections.MutableMapping):
else: else:
url = cls.base_path url = cls.base_path
url = utils.urljoin(url, resource_id) url = utils.urljoin(url, resource_id)
args = {'json': body}
if headers:
args[HEADERS] = headers
if cls.put_update: if cls.put_update:
resp = session.put(url, service=cls.service, json=body).body resp = session.put(url, service=cls.service, **args).body
else: else:
resp = session.patch(url, service=cls.service, json=body).body resp = session.patch(url, service=cls.service, **args).body
if cls.resource_key: if cls.resource_key:
resp = resp[cls.resource_key] resp = resp[cls.resource_key]

View File

@@ -190,6 +190,10 @@ class PropTests(base.TestCase):
class HeaderTests(base.TestCase): class HeaderTests(base.TestCase):
class Test(resource.Resource): class Test(resource.Resource):
base_path = "/ramones"
service = "punk"
allow_create = True
allow_update = True
hey = resource.header("vocals") hey = resource.header("vocals")
ho = resource.header("guitar") ho = resource.header("guitar")
letsgo = resource.header("bass") letsgo = resource.header("bass")
@@ -232,6 +236,33 @@ class HeaderTests(base.TestCase):
self.assertIsNone(sot.hey) self.assertIsNone(sot.hey)
def test_create_update_headers(self):
sot = HeaderTests.Test()
sot._reset_dirty()
sot.ho = "johnny"
sot.letsgo = "deedee"
response = mock.MagicMock()
response.body = {'id': 1}
sess = mock.MagicMock()
sess.post = mock.MagicMock(return_value=response)
sess.patch = mock.MagicMock(return_value=response)
sot.create(sess)
headers = {'guitar': 'johnny', 'bass': 'deedee'}
sess.post.assert_called_with(HeaderTests.Test.base_path,
service=HeaderTests.Test.service,
headers=headers,
json={})
sot['id'] = 1
sot.letsgo = "cj"
headers = {'guitar': 'johnny', 'bass': 'cj'}
sot.update(sess)
sess.patch.assert_called_with('ramones/1',
service=HeaderTests.Test.service,
headers=headers,
json={})
class ResourceTests(base.TestTransportBase): class ResourceTests(base.TestTransportBase):