Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add check for 'no content' responses

  • Loading branch information...
commit ed8d8f36db9789d64b54ddb7c14ca2b80d4178ca 1 parent bc5227e
Byron Ruth authored
Showing with 16 additions and 4 deletions.
  1. +12 −0 restlib2/resources.py
  2. +4 −4 restlib2/tests/cases.py
12 restlib2/resources.py
View
@@ -18,6 +18,16 @@
usable = lambda x, y: callable(getattr(x, y, None))
+def no_content_response(response):
+ "Cautious assessment of the response body for no content."
+ if response._container is None:
+ return True
+ if isinstance(response._container, (list, tuple)):
+ if len(response._container) == 1 and response._container[0] == '':
+ return True
+ return False
+
+
class UncacheableResponse(HttpResponse):
"Response class that will never be cached."
def __init__(self, *args, **kwargs):
@@ -741,6 +751,8 @@ def process_response(self, request, response):
if request.method == methods.HEAD:
response.content = ''
+ elif response.status_code == codes.ok and no_content_response(response):
+ response.status_code = codes.no_content
if request.method in (methods.GET, methods.HEAD):
self.response_cache_control(request, response)
8 restlib2/tests/cases.py
View
@@ -20,7 +20,7 @@ def test_default(self):
# OPTIONS is successful, default response with no content is a 204
request = self.factory.options('/')
response = resource(request)
- self.assertEqual(response.status_code, codes.ok)
+ self.assertEqual(response.status_code, codes.no_content)
# Try another non-default method
request = self.factory.get('/')
@@ -50,7 +50,7 @@ def patch(self, request):
resource = PatchResource()
request = self.factory.options('/')
response = resource(request)
- self.assertEqual(response.status_code, codes.ok)
+ self.assertEqual(response.status_code, codes.no_content)
self.assertEqual(response['Accept-Patch'], 'application/json')
def test_service_unavailable(self):
@@ -212,7 +212,7 @@ def is_too_many_requests(self, request, *args, **kwargs):
# First ten requests are ok
for _ in xrange(0, 10):
response = resource(request)
- self.assertEqual(response.status_code, codes.ok)
+ self.assertEqual(response.status_code, codes.no_content)
# Mimic a slight delay
time.sleep(1)
@@ -227,7 +227,7 @@ def is_too_many_requests(self, request, *args, **kwargs):
for _ in xrange(0, 10):
response = resource(request)
- self.assertEqual(response.status_code, codes.ok)
+ self.assertEqual(response.status_code, codes.no_content)
def test_precondition_required(self):
Please sign in to comment.
Something went wrong with that request. Please try again.