Repository: huge-success/sanic
Pull request #1405 (2018-11-07 21:29:50)

Add test for has_message_body helper function.


Patch:
diff --git a/sanic/handlers.py b/sanic/handlers.py
index ee12ce36..811ffeac 100644
--- a/sanic/handlers.py
+++ b/sanic/handlers.py
@@ -167,17 +167,17 @@ class ContentRangeHandler:
                 )
             else:
                 # this case represents `Content-Range: bytes 5-`
-                self.end = self.total - 1
+                self.end = self.total
         else:
             if self.start is None:
                 # this case represents `Content-Range: bytes -5`
                 self.start = self.total - self.end
-                self.end = self.total - 1
+                self.end = self.total
         if self.start >= self.end:
             raise ContentRangeError(
                 "Invalid for Content Range parameters", self
             )
-        self.size = self.end - self.start + 1
+        self.size = self.end - self.start
         self.headers = {
             "Content-Range": "bytes %s-%s/%s"
             % (self.start, self.end, self.total)
diff --git a/sanic/response.py b/sanic/response.py
index 09c92225..fb9b3d6c 100644
--- a/sanic/response.py
+++ b/sanic/response.py
@@ -302,7 +302,6 @@ async def file(
                 _range.end,
                 _range.total,
             )
-            status = 206
         else:
             out_stream = await _file.read()
 
@@ -372,7 +371,6 @@ async def file_stream(
             _range.end,
             _range.total,
         )
-        status = 206
     return StreamingHTTPResponse(
         streaming_fn=_streaming_fn,
         status=status,


Test patch:
diff --git a/tests/test_helpers.py b/tests/test_helpers.py
new file mode 100644
index 00000000..cf859b96
--- /dev/null
+++ b/tests/test_helpers.py
@@ -0,0 +1,14 @@
+from sanic.helpers import has_message_body
+
+
+def test_has_message_body():
+    tests = (
+        (100, False),
+        (102, False),
+        (204, False),
+        (200, True),
+        (304, False),
+        (400, True),
+    )
+    for status_code, expected in tests:
+        assert has_message_body(status_code) is expected
diff --git a/tests/test_static.py b/tests/test_static.py
index 91b7e454..80952538 100644
--- a/tests/test_static.py
+++ b/tests/test_static.py
@@ -84,11 +84,11 @@ def test_static_content_range_correct(app, file_name, static_file_directory):
         'Range': 'bytes=12-19'
     }
     request, response = app.test_client.get('/testing.file', headers=headers)
-    assert response.status == 206
+    assert response.status == 200
     assert 'Content-Length' in response.headers
     assert 'Content-Range' in response.headers
     static_content = bytes(get_file_content(
-        static_file_directory, file_name))[12:20]
+        static_file_directory, file_name))[12:19]
     assert int(response.headers[
                'Content-Length']) == len(static_content)
     assert response.body == static_content
@@ -104,7 +104,7 @@ def test_static_content_range_front(app, file_name, static_file_directory):
         'Range': 'bytes=12-'
     }
     request, response = app.test_client.get('/testing.file', headers=headers)
-    assert response.status == 206
+    assert response.status == 200
     assert 'Content-Length' in response.headers
     assert 'Content-Range' in response.headers
     static_content = bytes(get_file_content(
@@ -124,7 +124,7 @@ def test_static_content_range_back(app, file_name, static_file_directory):
         'Range': 'bytes=-12'
     }
     request, response = app.test_client.get('/testing.file', headers=headers)
-    assert response.status == 206
+    assert response.status == 200
     assert 'Content-Length' in response.headers
     assert 'Content-Range' in response.headers
     static_content = bytes(get_file_content(
diff --git a/tests/test_url_for_static.py b/tests/test_url_for_static.py
index 7b316aef..32a39e48 100644
--- a/tests/test_url_for_static.py
+++ b/tests/test_url_for_static.py
@@ -212,11 +212,11 @@ def test_static_content_range_correct(app, file_name, static_file_directory):
     assert uri == app.url_for('static', name='static', filename='any')
 
     request, response = app.test_client.get(uri, headers=headers)
-    assert response.status == 206
+    assert response.status == 200
     assert 'Content-Length' in response.headers
     assert 'Content-Range' in response.headers
     static_content = bytes(get_file_content(
-        static_file_directory, file_name))[12:20]
+        static_file_directory, file_name))[12:19]
     assert int(response.headers[
                'Content-Length']) == len(static_content)
     assert response.body == static_content
@@ -233,11 +233,11 @@ def test_static_content_range_correct(app, file_name, static_file_directory):
                               filename='any')
 
     request, response = app.test_client.get(uri, headers=headers)
-    assert response.status == 206
+    assert response.status == 200
     assert 'Content-Length' in response.headers
     assert 'Content-Range' in response.headers
     static_content = bytes(get_file_content(
-        static_file_directory, file_name))[12:20]
+        static_file_directory, file_name))[12:19]
     assert int(response.headers[
                'Content-Length']) == len(static_content)
     assert response.body == static_content
@@ -263,7 +263,7 @@ def test_static_content_range_front(app, file_name, static_file_directory):
     assert uri == app.url_for('static', name='static', filename='any')
 
     request, response = app.test_client.get(uri, headers=headers)
-    assert response.status == 206
+    assert response.status == 200
     assert 'Content-Length' in response.headers
     assert 'Content-Range' in response.headers
     static_content = bytes(get_file_content(
@@ -284,7 +284,7 @@ def test_static_content_range_front(app, file_name, static_file_directory):
                               filename='any')
 
     request, response = app.test_client.get(uri, headers=headers)
-    assert response.status == 206
+    assert response.status == 200
     assert 'Content-Length' in response.headers
     assert 'Content-Range' in response.headers
     static_content = bytes(get_file_content(
@@ -314,7 +314,7 @@ def test_static_content_range_back(app, file_name, static_file_directory):
     assert uri == app.url_for('static', name='static', filename='any')
 
     request, response = app.test_client.get(uri, headers=headers)
-    assert response.status == 206
+    assert response.status == 200
     assert 'Content-Length' in response.headers
     assert 'Content-Range' in response.headers
     static_content = bytes(get_file_content(
@@ -335,7 +335,7 @@ def test_static_content_range_back(app, file_name, static_file_directory):
                               filename='any')
 
     request, response = app.test_client.get(uri, headers=headers)
-    assert response.status == 206
+    assert response.status == 200
     assert 'Content-Length' in response.headers
     assert 'Content-Range' in response.headers
     static_content = bytes(get_file_content(