2017-02-21 16:05:06 +00:00
|
|
|
import asyncio
|
2017-05-20 00:41:36 +01:00
|
|
|
import inspect
|
|
|
|
import os
|
2019-04-23 22:44:42 +01:00
|
|
|
|
2018-12-13 17:50:50 +00:00
|
|
|
from collections import namedtuple
|
2017-05-20 00:41:36 +01:00
|
|
|
from mimetypes import guess_type
|
2018-11-07 16:02:34 +00:00
|
|
|
from random import choice
|
|
|
|
from unittest.mock import MagicMock
|
2017-05-20 00:41:36 +01:00
|
|
|
from urllib.parse import unquote
|
|
|
|
|
2017-02-21 16:05:06 +00:00
|
|
|
import pytest
|
2019-04-23 22:44:42 +01:00
|
|
|
|
2018-11-07 16:02:34 +00:00
|
|
|
from aiofiles import os as async_os
|
2016-12-25 02:47:15 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
from sanic.response import (
|
|
|
|
HTTPResponse,
|
|
|
|
StreamingHTTPResponse,
|
|
|
|
file,
|
|
|
|
file_stream,
|
|
|
|
json,
|
|
|
|
raw,
|
|
|
|
stream,
|
More robust response datatype handling (#1674)
* HTTP1 header formatting moved to headers.format_headers and rewritten.
- New implementation is one line of code and twice faster than the old one.
- Whole header block encoded to UTF-8 in one pass.
- No longer supports custom encode method on header values.
- Cookie objects now have __str__ in addition to encode, to work with this.
* Linter
* format_http1_response
* Replace encode_body with faster implementation based on f-string.
Benchmarks:
def encode_body(data):
try:
# Try to encode it regularly
return data.encode()
except AttributeError:
# Convert it to a str if you can't
return str(data).encode()
def encode_body2(data):
return f"{data}".encode()
def encode_body3(data):
return str(data).encode()
data_str, data_int = "foo", 123
%timeit encode_body(data_int)
928 ns ± 2.96 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit encode_body2(data_int)
280 ns ± 2.09 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit encode_body3(data_int)
387 ns ± 1.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit encode_body(data_str)
202 ns ± 1.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit encode_body2(data_str)
197 ns ± 0.507 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
%timeit encode_body3(data_str)
313 ns ± 1.28 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
* Wtf linter
* Content-type fixes.
* Body encoding sanitation, first pass.
- body/data type autodetection fixed.
- do not repr(body).encode() bytes-ish values.
- support __html__ and _repr_html_ in sanic.response.html().
* <any type>-to-str response autoconversion limited to sanic.response.text() only.
* Workaround MyPy issue.
* Add an empty line to make isort happy.
* Add html test for __html__ and _repr_html_.
* Remove StreamingHTTPResponse.get_headers helper function.
* Add back HTTPResponse Keep-Alive removed by earlier merge or something.
* Revert "Remove StreamingHTTPResponse.get_headers helper function."
Tests depend on this otherwise useless function.
This reverts commit 9651e6ae017b61bed6dd88af6631cdd6b01eb347.
* Add deprecation warnings; instead of assert for wrong HTTP version, and for non-string response.text.
* Add back missing import.
* Avoid duplicate response header tweaking code.
* Linter errors
2020-01-20 16:34:32 +00:00
|
|
|
text,
|
2018-12-30 11:18:06 +00:00
|
|
|
)
|
2019-12-23 20:16:53 +00:00
|
|
|
from sanic.response import empty
|
2018-08-19 02:12:13 +01:00
|
|
|
from sanic.server import HttpProtocol
|
2018-10-09 03:40:36 +01:00
|
|
|
from sanic.testing import HOST, PORT
|
2016-12-25 02:47:15 +00:00
|
|
|
|
2019-04-23 22:44:42 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
JSON_DATA = {"ok": True}
|
2017-06-05 16:24:23 +01:00
|
|
|
|
2017-05-20 00:41:36 +01:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_response_body_not_a_string(app):
|
2016-12-25 02:47:15 +00:00
|
|
|
"""Test when a response body sent from the application is not a string"""
|
|
|
|
random_num = choice(range(1000))
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/hello")
|
2016-12-25 02:47:15 +00:00
|
|
|
async def hello_route(request):
|
More robust response datatype handling (#1674)
* HTTP1 header formatting moved to headers.format_headers and rewritten.
- New implementation is one line of code and twice faster than the old one.
- Whole header block encoded to UTF-8 in one pass.
- No longer supports custom encode method on header values.
- Cookie objects now have __str__ in addition to encode, to work with this.
* Linter
* format_http1_response
* Replace encode_body with faster implementation based on f-string.
Benchmarks:
def encode_body(data):
try:
# Try to encode it regularly
return data.encode()
except AttributeError:
# Convert it to a str if you can't
return str(data).encode()
def encode_body2(data):
return f"{data}".encode()
def encode_body3(data):
return str(data).encode()
data_str, data_int = "foo", 123
%timeit encode_body(data_int)
928 ns ± 2.96 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit encode_body2(data_int)
280 ns ± 2.09 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit encode_body3(data_int)
387 ns ± 1.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit encode_body(data_str)
202 ns ± 1.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit encode_body2(data_str)
197 ns ± 0.507 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
%timeit encode_body3(data_str)
313 ns ± 1.28 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
* Wtf linter
* Content-type fixes.
* Body encoding sanitation, first pass.
- body/data type autodetection fixed.
- do not repr(body).encode() bytes-ish values.
- support __html__ and _repr_html_ in sanic.response.html().
* <any type>-to-str response autoconversion limited to sanic.response.text() only.
* Workaround MyPy issue.
* Add an empty line to make isort happy.
* Add html test for __html__ and _repr_html_.
* Remove StreamingHTTPResponse.get_headers helper function.
* Add back HTTPResponse Keep-Alive removed by earlier merge or something.
* Revert "Remove StreamingHTTPResponse.get_headers helper function."
Tests depend on this otherwise useless function.
This reverts commit 9651e6ae017b61bed6dd88af6631cdd6b01eb347.
* Add deprecation warnings; instead of assert for wrong HTTP version, and for non-string response.text.
* Add back missing import.
* Avoid duplicate response header tweaking code.
* Linter errors
2020-01-20 16:34:32 +00:00
|
|
|
return text(random_num)
|
2016-12-25 02:47:15 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/hello")
|
2016-12-25 02:47:15 +00:00
|
|
|
assert response.text == str(random_num)
|
2017-02-21 16:05:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def sample_streaming_fn(response):
|
2018-12-30 11:18:06 +00:00
|
|
|
await response.write("foo,")
|
|
|
|
await asyncio.sleep(0.001)
|
|
|
|
await response.write("bar")
|
2018-08-19 02:12:13 +01:00
|
|
|
|
2017-02-21 16:05:06 +00:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_method_not_allowed(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.get("/")
|
2018-10-22 21:25:38 +01:00
|
|
|
async def test_get(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return response.json({"hello": "world"})
|
2017-12-12 04:11:20 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.head("/")
|
|
|
|
assert response.headers["Allow"] == "GET"
|
2017-12-12 04:11:20 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.post("/")
|
|
|
|
assert response.headers["Allow"] == "GET"
|
2018-02-28 05:42:41 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.post("/")
|
2018-10-22 21:25:38 +01:00
|
|
|
async def test_post(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return response.json({"hello": "world"})
|
2017-12-12 04:11:20 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.head("/")
|
2017-12-12 04:11:20 +00:00
|
|
|
assert response.status == 405
|
2018-12-30 11:18:06 +00:00
|
|
|
assert set(response.headers["Allow"].split(", ")) == {"GET", "POST"}
|
|
|
|
assert response.headers["Content-Length"] == "0"
|
2018-02-28 05:42:41 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.patch("/")
|
2018-02-28 05:42:41 +00:00
|
|
|
assert response.status == 405
|
2018-12-30 11:18:06 +00:00
|
|
|
assert set(response.headers["Allow"].split(", ")) == {"GET", "POST"}
|
|
|
|
assert response.headers["Content-Length"] == "0"
|
2017-12-12 04:11:20 +00:00
|
|
|
|
2017-02-21 16:05:06 +00:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_response_header(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.get("/")
|
2018-07-11 09:44:21 +01:00
|
|
|
async def test(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return json({"ok": True}, headers={"CONTENT-TYPE": "application/json"})
|
2018-07-11 09:44:21 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/")
|
2018-07-11 09:44:21 +01:00
|
|
|
assert dict(response.headers) == {
|
2019-04-23 22:44:42 +01:00
|
|
|
"connection": "keep-alive",
|
|
|
|
"keep-alive": str(app.config.KEEP_ALIVE_TIMEOUT),
|
|
|
|
"content-length": "11",
|
|
|
|
"content-type": "application/json",
|
2018-07-11 09:44:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-07 16:37:28 +00:00
|
|
|
def test_response_content_length(app):
|
|
|
|
@app.get("/response_with_space")
|
|
|
|
async def response_with_space(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return json(
|
|
|
|
{"message": "Data", "details": "Some Details"},
|
|
|
|
headers={"CONTENT-TYPE": "application/json"},
|
|
|
|
)
|
2018-11-07 16:37:28 +00:00
|
|
|
|
|
|
|
@app.get("/response_without_space")
|
|
|
|
async def response_without_space(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return json(
|
|
|
|
{"message": "Data", "details": "Some Details"},
|
|
|
|
headers={"CONTENT-TYPE": "application/json"},
|
|
|
|
)
|
2018-11-07 16:37:28 +00:00
|
|
|
|
|
|
|
_, response = app.test_client.get("/response_with_space")
|
2018-12-30 11:18:06 +00:00
|
|
|
content_length_for_response_with_space = response.headers.get(
|
|
|
|
"Content-Length"
|
|
|
|
)
|
2018-11-07 16:37:28 +00:00
|
|
|
|
|
|
|
_, response = app.test_client.get("/response_without_space")
|
2018-12-30 11:18:06 +00:00
|
|
|
content_length_for_response_without_space = response.headers.get(
|
|
|
|
"Content-Length"
|
|
|
|
)
|
2018-11-07 16:37:28 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
assert (
|
|
|
|
content_length_for_response_with_space
|
|
|
|
== content_length_for_response_without_space
|
|
|
|
)
|
2018-11-07 16:37:28 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
assert content_length_for_response_with_space == "43"
|
2018-11-07 16:37:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_response_content_length_with_different_data_types(app):
|
|
|
|
@app.get("/")
|
|
|
|
async def get_data_with_different_types(request):
|
|
|
|
# Indentation issues in the Response is intentional. Please do not fix
|
2018-12-30 11:18:06 +00:00
|
|
|
return json(
|
|
|
|
{"bool": True, "none": None, "string": "string", "number": -1},
|
|
|
|
headers={"CONTENT-TYPE": "application/json"},
|
|
|
|
)
|
2018-11-07 16:37:28 +00:00
|
|
|
|
|
|
|
_, response = app.test_client.get("/")
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.headers.get("Content-Length") == "55"
|
2018-11-07 16:37:28 +00:00
|
|
|
|
|
|
|
|
2017-06-05 16:24:23 +01:00
|
|
|
@pytest.fixture
|
2018-08-26 15:43:14 +01:00
|
|
|
def json_app(app):
|
2017-06-05 16:24:23 +01:00
|
|
|
@app.route("/")
|
|
|
|
async def test(request):
|
|
|
|
return json(JSON_DATA)
|
|
|
|
|
2018-02-01 19:00:32 +00:00
|
|
|
@app.get("/no-content")
|
|
|
|
async def no_content_handler(request):
|
|
|
|
return json(JSON_DATA, status=204)
|
|
|
|
|
2018-02-02 12:24:51 +00:00
|
|
|
@app.get("/no-content/unmodified")
|
|
|
|
async def no_content_unmodified_handler(request):
|
|
|
|
return json(None, status=304)
|
|
|
|
|
|
|
|
@app.get("/unmodified")
|
|
|
|
async def unmodified_handler(request):
|
|
|
|
return json(JSON_DATA, status=304)
|
|
|
|
|
2018-02-01 10:52:55 +00:00
|
|
|
@app.delete("/")
|
2018-02-01 19:00:32 +00:00
|
|
|
async def delete_handler(request):
|
2018-02-01 10:52:55 +00:00
|
|
|
return json(None, status=204)
|
|
|
|
|
2017-06-05 16:24:23 +01:00
|
|
|
return app
|
|
|
|
|
|
|
|
|
|
|
|
def test_json_response(json_app):
|
|
|
|
from sanic.response import json_dumps
|
2018-12-30 11:18:06 +00:00
|
|
|
|
|
|
|
request, response = json_app.test_client.get("/")
|
2017-06-05 16:24:23 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == json_dumps(JSON_DATA)
|
|
|
|
assert response.json == JSON_DATA
|
|
|
|
|
2018-02-01 10:52:55 +00:00
|
|
|
|
|
|
|
def test_no_content(json_app):
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = json_app.test_client.get("/no-content")
|
2018-02-01 19:00:32 +00:00
|
|
|
assert response.status == 204
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == ""
|
|
|
|
assert "Content-Length" not in response.headers
|
2018-02-01 19:00:32 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = json_app.test_client.get("/no-content/unmodified")
|
2018-02-02 12:24:51 +00:00
|
|
|
assert response.status == 304
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == ""
|
|
|
|
assert "Content-Length" not in response.headers
|
|
|
|
assert "Content-Type" not in response.headers
|
2018-02-02 12:24:51 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = json_app.test_client.get("/unmodified")
|
2018-02-02 12:24:51 +00:00
|
|
|
assert response.status == 304
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == ""
|
|
|
|
assert "Content-Length" not in response.headers
|
|
|
|
assert "Content-Type" not in response.headers
|
2018-02-02 12:24:51 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = json_app.test_client.delete("/")
|
2018-02-01 10:52:55 +00:00
|
|
|
assert response.status == 204
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == ""
|
|
|
|
assert "Content-Length" not in response.headers
|
2018-02-01 10:52:55 +00:00
|
|
|
|
|
|
|
|
2017-02-21 16:05:06 +00:00
|
|
|
@pytest.fixture
|
2018-08-26 15:43:14 +01:00
|
|
|
def streaming_app(app):
|
2017-02-21 16:05:06 +00:00
|
|
|
@app.route("/")
|
|
|
|
async def test(request):
|
2019-04-20 20:27:10 +01:00
|
|
|
return stream(
|
|
|
|
sample_streaming_fn,
|
|
|
|
content_type="text/csv",
|
|
|
|
)
|
|
|
|
|
|
|
|
return app
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def non_chunked_streaming_app(app):
|
|
|
|
@app.route("/")
|
|
|
|
async def test(request):
|
|
|
|
return stream(
|
|
|
|
sample_streaming_fn,
|
|
|
|
headers={"Content-Length": "7"},
|
|
|
|
content_type="text/csv",
|
|
|
|
chunked=False,
|
|
|
|
)
|
2017-02-21 16:05:06 +00:00
|
|
|
|
|
|
|
return app
|
|
|
|
|
|
|
|
|
2019-04-20 20:27:10 +01:00
|
|
|
def test_chunked_streaming_adds_correct_headers(streaming_app):
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = streaming_app.test_client.get("/")
|
|
|
|
assert response.headers["Transfer-Encoding"] == "chunked"
|
|
|
|
assert response.headers["Content-Type"] == "text/csv"
|
2019-04-20 20:27:10 +01:00
|
|
|
# Content-Length is not allowed by HTTP/1.1 specification
|
|
|
|
# when "Transfer-Encoding: chunked" is used
|
|
|
|
assert "Content-Length" not in response.headers
|
2017-02-21 16:05:06 +00:00
|
|
|
|
|
|
|
|
2019-04-20 20:27:10 +01:00
|
|
|
def test_chunked_streaming_returns_correct_content(streaming_app):
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = streaming_app.test_client.get("/")
|
|
|
|
assert response.text == "foo,bar"
|
2017-02-21 16:05:06 +00:00
|
|
|
|
|
|
|
|
2019-05-21 23:42:19 +01:00
|
|
|
def test_non_chunked_streaming_adds_correct_headers(non_chunked_streaming_app):
|
2019-04-20 20:27:10 +01:00
|
|
|
request, response = non_chunked_streaming_app.test_client.get("/")
|
|
|
|
assert "Transfer-Encoding" not in response.headers
|
|
|
|
assert response.headers["Content-Type"] == "text/csv"
|
|
|
|
assert response.headers["Content-Length"] == "7"
|
|
|
|
|
|
|
|
|
|
|
|
def test_non_chunked_streaming_returns_correct_content(
|
|
|
|
non_chunked_streaming_app
|
|
|
|
):
|
|
|
|
request, response = non_chunked_streaming_app.test_client.get("/")
|
|
|
|
assert response.text == "foo,bar"
|
|
|
|
|
|
|
|
|
2017-05-20 00:41:36 +01:00
|
|
|
|
2018-12-22 15:21:45 +00:00
|
|
|
def test_stream_response_with_cookies(app):
|
|
|
|
@app.route("/")
|
|
|
|
async def test(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
response = stream(sample_streaming_fn, content_type="text/csv")
|
|
|
|
response.cookies["test"] = "modified"
|
|
|
|
response.cookies["test"] = "pass"
|
2018-12-22 15:21:45 +00:00
|
|
|
return response
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/")
|
2019-04-23 22:44:42 +01:00
|
|
|
assert response.cookies["test"] == "pass"
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_stream_response_without_cookies(app):
|
|
|
|
@app.route("/")
|
|
|
|
async def test(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return stream(sample_streaming_fn, content_type="text/csv")
|
2018-12-22 15:21:45 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/")
|
2018-12-22 15:21:45 +00:00
|
|
|
assert response.cookies == {}
|
|
|
|
|
|
|
|
|
2017-05-20 00:41:36 +01:00
|
|
|
@pytest.fixture
|
|
|
|
def static_file_directory():
|
|
|
|
"""The static directory to serve"""
|
|
|
|
current_file = inspect.getfile(inspect.currentframe())
|
|
|
|
current_directory = os.path.dirname(os.path.abspath(current_file))
|
2018-12-30 11:18:06 +00:00
|
|
|
static_directory = os.path.join(current_directory, "static")
|
2017-05-20 00:41:36 +01:00
|
|
|
return static_directory
|
|
|
|
|
|
|
|
|
|
|
|
def get_file_content(static_file_directory, file_name):
|
|
|
|
"""The content of the static file to check"""
|
2018-12-30 11:18:06 +00:00
|
|
|
with open(os.path.join(static_file_directory, file_name), "rb") as file:
|
2017-05-20 00:41:36 +01:00
|
|
|
return file.read()
|
|
|
|
|
2018-02-02 13:05:57 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"file_name", ["test.file", "decode me.txt", "python.png"]
|
|
|
|
)
|
|
|
|
@pytest.mark.parametrize("status", [200, 401])
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_file_response(app, file_name, static_file_directory, status):
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/files/<filename>", methods=["GET"])
|
2017-05-20 00:41:36 +01:00
|
|
|
def file_route(request, filename):
|
|
|
|
file_path = os.path.join(static_file_directory, filename)
|
|
|
|
file_path = os.path.abspath(unquote(file_path))
|
2018-12-30 11:18:06 +00:00
|
|
|
return file(
|
|
|
|
file_path,
|
|
|
|
status=status,
|
|
|
|
mime_type=guess_type(file_path)[0] or "text/plain",
|
|
|
|
)
|
2017-05-20 00:41:36 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/files/{}".format(file_name))
|
2018-07-20 21:39:10 +01:00
|
|
|
assert response.status == status
|
2017-05-20 00:41:36 +01:00
|
|
|
assert response.body == get_file_content(static_file_directory, file_name)
|
2018-12-30 11:18:06 +00:00
|
|
|
assert "Content-Disposition" not in response.headers
|
2017-05-20 00:41:36 +01:00
|
|
|
|
2018-02-02 13:05:57 +00:00
|
|
|
|
2018-10-22 21:25:38 +01:00
|
|
|
@pytest.mark.parametrize(
|
2018-12-30 11:18:06 +00:00
|
|
|
"source,dest",
|
2018-10-22 21:25:38 +01:00
|
|
|
[
|
2018-12-30 11:18:06 +00:00
|
|
|
("test.file", "my_file.txt"),
|
|
|
|
("decode me.txt", "readme.md"),
|
|
|
|
("python.png", "logo.png"),
|
|
|
|
],
|
2018-10-22 21:25:38 +01:00
|
|
|
)
|
2018-12-30 11:18:06 +00:00
|
|
|
def test_file_response_custom_filename(
|
|
|
|
app, source, dest, static_file_directory
|
|
|
|
):
|
|
|
|
@app.route("/files/<filename>", methods=["GET"])
|
2017-10-09 15:45:42 +01:00
|
|
|
def file_route(request, filename):
|
|
|
|
file_path = os.path.join(static_file_directory, filename)
|
|
|
|
file_path = os.path.abspath(unquote(file_path))
|
|
|
|
return file(file_path, filename=dest)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/files/{}".format(source))
|
2017-10-09 15:45:42 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.body == get_file_content(static_file_directory, source)
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.headers[
|
|
|
|
"Content-Disposition"
|
|
|
|
] == 'attachment; filename="{}"'.format(dest)
|
2017-05-20 00:41:36 +01:00
|
|
|
|
2018-02-02 13:05:57 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("file_name", ["test.file", "decode me.txt"])
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_file_head_response(app, file_name, static_file_directory):
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/files/<filename>", methods=["GET", "HEAD"])
|
2017-05-20 00:41:36 +01:00
|
|
|
async def file_route(request, filename):
|
|
|
|
file_path = os.path.join(static_file_directory, filename)
|
|
|
|
file_path = os.path.abspath(unquote(file_path))
|
|
|
|
stats = await async_os.stat(file_path)
|
|
|
|
headers = dict()
|
2018-12-30 11:18:06 +00:00
|
|
|
headers["Accept-Ranges"] = "bytes"
|
|
|
|
headers["Content-Length"] = str(stats.st_size)
|
2017-05-20 00:41:36 +01:00
|
|
|
if request.method == "HEAD":
|
|
|
|
return HTTPResponse(
|
|
|
|
headers=headers,
|
2018-12-30 11:18:06 +00:00
|
|
|
content_type=guess_type(file_path)[0] or "text/plain",
|
|
|
|
)
|
2017-05-20 00:41:36 +01:00
|
|
|
else:
|
2018-12-30 11:18:06 +00:00
|
|
|
return file(
|
|
|
|
file_path,
|
|
|
|
headers=headers,
|
|
|
|
mime_type=guess_type(file_path)[0] or "text/plain",
|
|
|
|
)
|
2017-05-20 00:41:36 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.head("/files/{}".format(file_name))
|
2017-05-20 00:41:36 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert "Accept-Ranges" in response.headers
|
|
|
|
assert "Content-Length" in response.headers
|
|
|
|
assert int(response.headers["Content-Length"]) == len(
|
|
|
|
get_file_content(static_file_directory, file_name)
|
|
|
|
)
|
2017-05-20 00:41:36 +01:00
|
|
|
|
2018-02-02 13:05:57 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"file_name", ["test.file", "decode me.txt", "python.png"]
|
|
|
|
)
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_file_stream_response(app, file_name, static_file_directory):
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/files/<filename>", methods=["GET"])
|
2017-05-20 00:41:36 +01:00
|
|
|
def file_route(request, filename):
|
|
|
|
file_path = os.path.join(static_file_directory, filename)
|
|
|
|
file_path = os.path.abspath(unquote(file_path))
|
2018-12-30 11:18:06 +00:00
|
|
|
return file_stream(
|
|
|
|
file_path,
|
|
|
|
chunk_size=32,
|
|
|
|
mime_type=guess_type(file_path)[0] or "text/plain",
|
|
|
|
)
|
2017-05-20 00:41:36 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/files/{}".format(file_name))
|
2017-05-20 00:41:36 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.body == get_file_content(static_file_directory, file_name)
|
2018-12-30 11:18:06 +00:00
|
|
|
assert "Content-Disposition" not in response.headers
|
2017-05-20 00:41:36 +01:00
|
|
|
|
2018-02-02 13:05:57 +00:00
|
|
|
|
2018-10-22 21:25:38 +01:00
|
|
|
@pytest.mark.parametrize(
|
2018-12-30 11:18:06 +00:00
|
|
|
"source,dest",
|
2018-10-22 21:25:38 +01:00
|
|
|
[
|
2018-12-30 11:18:06 +00:00
|
|
|
("test.file", "my_file.txt"),
|
|
|
|
("decode me.txt", "readme.md"),
|
|
|
|
("python.png", "logo.png"),
|
|
|
|
],
|
2018-10-22 21:25:38 +01:00
|
|
|
)
|
2018-12-30 11:18:06 +00:00
|
|
|
def test_file_stream_response_custom_filename(
|
|
|
|
app, source, dest, static_file_directory
|
|
|
|
):
|
|
|
|
@app.route("/files/<filename>", methods=["GET"])
|
2017-10-09 15:45:42 +01:00
|
|
|
def file_route(request, filename):
|
|
|
|
file_path = os.path.join(static_file_directory, filename)
|
|
|
|
file_path = os.path.abspath(unquote(file_path))
|
|
|
|
return file_stream(file_path, chunk_size=32, filename=dest)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/files/{}".format(source))
|
2017-10-09 15:45:42 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.body == get_file_content(static_file_directory, source)
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.headers[
|
|
|
|
"Content-Disposition"
|
|
|
|
] == 'attachment; filename="{}"'.format(dest)
|
2017-05-20 00:41:36 +01:00
|
|
|
|
2018-02-02 13:05:57 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("file_name", ["test.file", "decode me.txt"])
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_file_stream_head_response(app, file_name, static_file_directory):
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/files/<filename>", methods=["GET", "HEAD"])
|
2017-05-20 00:41:36 +01:00
|
|
|
async def file_route(request, filename):
|
|
|
|
file_path = os.path.join(static_file_directory, filename)
|
|
|
|
file_path = os.path.abspath(unquote(file_path))
|
|
|
|
headers = dict()
|
2018-12-30 11:18:06 +00:00
|
|
|
headers["Accept-Ranges"] = "bytes"
|
2017-05-20 00:41:36 +01:00
|
|
|
if request.method == "HEAD":
|
|
|
|
# Return a normal HTTPResponse, not a
|
|
|
|
# StreamingHTTPResponse for a HEAD request
|
|
|
|
stats = await async_os.stat(file_path)
|
2018-12-30 11:18:06 +00:00
|
|
|
headers["Content-Length"] = str(stats.st_size)
|
2017-05-20 00:41:36 +01:00
|
|
|
return HTTPResponse(
|
|
|
|
headers=headers,
|
2018-12-30 11:18:06 +00:00
|
|
|
content_type=guess_type(file_path)[0] or "text/plain",
|
|
|
|
)
|
2017-05-20 00:41:36 +01:00
|
|
|
else:
|
2018-10-22 21:25:38 +01:00
|
|
|
return file_stream(
|
2018-12-30 11:18:06 +00:00
|
|
|
file_path,
|
|
|
|
chunk_size=32,
|
|
|
|
headers=headers,
|
|
|
|
mime_type=guess_type(file_path)[0] or "text/plain",
|
2018-10-22 21:25:38 +01:00
|
|
|
)
|
2017-05-20 00:41:36 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.head("/files/{}".format(file_name))
|
2017-05-20 00:41:36 +01:00
|
|
|
assert response.status == 200
|
|
|
|
# A HEAD request should never be streamed/chunked.
|
2018-12-30 11:18:06 +00:00
|
|
|
if "Transfer-Encoding" in response.headers:
|
|
|
|
assert response.headers["Transfer-Encoding"] != "chunked"
|
|
|
|
assert "Accept-Ranges" in response.headers
|
2017-05-20 00:41:36 +01:00
|
|
|
# A HEAD request should get the Content-Length too
|
2018-12-30 11:18:06 +00:00
|
|
|
assert "Content-Length" in response.headers
|
|
|
|
assert int(response.headers["Content-Length"]) == len(
|
|
|
|
get_file_content(static_file_directory, file_name)
|
|
|
|
)
|
2018-12-13 17:50:50 +00:00
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"file_name", ["test.file", "decode me.txt", "python.png"]
|
|
|
|
)
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"size,start,end", [(1024, 0, 1024), (4096, 1024, 8192)]
|
|
|
|
)
|
|
|
|
def test_file_stream_response_range(
|
|
|
|
app, file_name, static_file_directory, size, start, end
|
|
|
|
):
|
2018-12-13 17:50:50 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
Range = namedtuple("Range", ["size", "start", "end", "total"])
|
2018-12-13 17:50:50 +00:00
|
|
|
total = len(get_file_content(static_file_directory, file_name))
|
|
|
|
range = Range(size=size, start=start, end=end, total=total)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/files/<filename>", methods=["GET"])
|
2018-12-13 17:50:50 +00:00
|
|
|
def file_route(request, filename):
|
|
|
|
file_path = os.path.join(static_file_directory, filename)
|
|
|
|
file_path = os.path.abspath(unquote(file_path))
|
|
|
|
return file_stream(
|
|
|
|
file_path,
|
|
|
|
chunk_size=32,
|
2018-12-30 11:18:06 +00:00
|
|
|
mime_type=guess_type(file_path)[0] or "text/plain",
|
|
|
|
_range=range,
|
|
|
|
)
|
2018-12-13 17:50:50 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/files/{}".format(file_name))
|
2018-12-13 17:50:50 +00:00
|
|
|
assert response.status == 206
|
2018-12-30 11:18:06 +00:00
|
|
|
assert "Content-Range" in response.headers
|
|
|
|
assert response.headers["Content-Range"] == "bytes {}-{}/{}".format(
|
|
|
|
range.start, range.end, range.total
|
|
|
|
)
|
2018-12-13 17:50:50 +00:00
|
|
|
|
2018-12-22 15:21:45 +00:00
|
|
|
|
2018-12-13 17:50:50 +00:00
|
|
|
def test_raw_response(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.get("/test")
|
2018-12-13 17:50:50 +00:00
|
|
|
def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return raw(b"raw_response")
|
2018-12-13 17:50:50 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/test")
|
|
|
|
assert response.content_type == "application/octet-stream"
|
|
|
|
assert response.body == b"raw_response"
|
2019-12-23 20:16:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_empty_response(app):
|
|
|
|
@app.get("/test")
|
|
|
|
def handler(request):
|
|
|
|
return empty()
|
|
|
|
|
|
|
|
request, response = app.test_client.get("/test")
|
|
|
|
assert response.content_type is None
|
|
|
|
assert response.body == b""
|