Skip to content

Commit

Permalink
Merge pull request #13 from carljm/no-content-response
Browse files Browse the repository at this point in the history
Correctly handle success-but-no-content responses
  • Loading branch information
apparentlymart committed Dec 19, 2011
2 parents d9dca10 + cd0b893 commit ff4aea3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
11 changes: 6 additions & 5 deletions remoteobjects/http.py
Expand Up @@ -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:
Expand Down
33 changes: 33 additions & 0 deletions tests/test_http.py
Expand Up @@ -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):
Expand Down

0 comments on commit ff4aea3

Please sign in to comment.