diff --git a/remoteobjects/http.py b/remoteobjects/http.py index d4dc148..6bab3d4 100644 --- a/remoteobjects/http.py +++ b/remoteobjects/http.py @@ -262,12 +262,13 @@ def update_from_response(self, url, response, content): """ self.raise_for_response(url, response, content) - try: - data = json.loads(content) - except UnicodeDecodeError: - data = json.loads(content, cls=ForgivingDecoder) + if self.response_has_content.get(response.status): + try: + data = json.loads(content) + except UnicodeDecodeError: + data = json.loads(content, cls=ForgivingDecoder) - self.update_from_dict(data) + self.update_from_dict(data) location_header = self.location_headers.get(response.status) if location_header is None: diff --git a/tests/test_http.py b/tests/test_http.py index 295c861..d5a4931 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -156,6 +156,39 @@ class BasicMost(self.cls): self.assertEquals(b._etag, 'xyz') + def test_put_no_content(self): + """ + Don't try to update from a no-content response. + + """ + + class BasicMost(self.cls): + name = fields.Field() + value = fields.Field() + + request = { + 'uri': 'http://example.com/bwuh', + 'headers': {'accept': 'application/json'}, + } + content = """{"name": "Molly", "value": 80}""" + h = utils.mock_http(request, content) + b = BasicMost.get('http://example.com/bwuh', http=h) + self.assertEquals(b.name, 'Molly') + mox.Verify(h) + + headers = { + 'accept': 'application/json', + 'content-type': 'application/json', + 'if-match': '7', + } + request = dict(uri='http://example.com/bwuh', method='PUT', headers=headers, body=content) + response = dict(content="", status=204) + h = utils.mock_http(request, response) + b.put(http=h) + mox.Verify(h) + + self.assertEquals(b.name, 'Molly') + def test_put_failure(self): class BasicMost(self.cls):