Skip to content

Commit

Permalink
Fix JSONResponse default content type (#2738)
Browse files Browse the repository at this point in the history
Fix JSONResponse default content type (#2737)
  • Loading branch information
prryplatypus authored Jul 10, 2023
1 parent 4ad8168 commit fc82b23
Show file tree
Hide file tree
Showing 12 changed files with 10 additions and 13 deletions.
2 changes: 0 additions & 2 deletions sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,6 @@ async def handle_request(self, request: Request): # no cov
] = None
run_middleware = True
try:

await self.dispatch(
"http.routing.before",
inline=True,
Expand Down Expand Up @@ -926,7 +925,6 @@ async def handle_request(self, request: Request): # no cov
and request.stream.request_body
and not route.extra.ignore_body
):

if hasattr(handler, "is_stream"):
# Streaming handler: lift the size limit
request.stream.request_max_size = float("inf")
Expand Down
2 changes: 0 additions & 2 deletions sanic/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class HTTPMethod(UpperStrEnum):

GET = auto()
POST = auto()
PUT = auto()
Expand All @@ -15,7 +14,6 @@ class HTTPMethod(UpperStrEnum):


class LocalCertCreator(UpperStrEnum):

AUTO = auto()
TRUSTME = auto()
MKCERT = auto()
Expand Down
1 change: 0 additions & 1 deletion sanic/http/tls/creators.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ def select(
local_tls_key,
local_tls_cert,
) -> CertCreator:

creator: Optional[CertCreator] = None

cert_creator_options: Tuple[
Expand Down
1 change: 0 additions & 1 deletion sanic/mixins/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,6 @@ async def _run_server(
app: StartupMixin,
server_info: ApplicationServerInfo,
) -> None: # no cov

try:
# We should never get to this point without a server
# This is primarily to keep mypy happy
Expand Down
1 change: 0 additions & 1 deletion sanic/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ def __init__(
head: bytes = b"",
stream_id: int = 0,
):

self.raw_url = url_bytes
try:
self._parsed_url = parse_url(url_bytes)
Expand Down
2 changes: 1 addition & 1 deletion sanic/response/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def __init__(
body: Optional[Any] = None,
status: int = 200,
headers: Optional[Union[Header, Dict[str, str]]] = None,
content_type: Optional[str] = None,
content_type: str = "application/json",
dumps: Optional[Callable[..., str]] = None,
**kwargs: Any,
):
Expand Down
1 change: 0 additions & 1 deletion sanic/server/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ def interrupt_self(*args):

try:
while True:

changed = set()
for filename in itertools.chain(
_iter_module_files(),
Expand Down
1 change: 0 additions & 1 deletion sanic/server/websockets/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ class WebsocketFrameAssembler:
paused: bool

def __init__(self, protocol) -> None:

self.protocol = protocol

self.read_mutex = asyncio.Lock()
Expand Down
1 change: 0 additions & 1 deletion sanic/server/websockets/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,6 @@ async def send(self, message: Union[Data, Iterable[Data]]) -> None:
:raises TypeError: for unsupported inputs
"""
async with self.conn_mutex:

if self.ws_proto.state in (CLOSED, CLOSING):
raise WebsocketClosed(
"Cannot write to websocket interface after it is closed."
Expand Down
1 change: 0 additions & 1 deletion sanic/touchup/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def __new__(cls, name, bases, attrs, **kwargs):
methods = attrs.get("__touchup__")
attrs["__touched__"] = False
if methods:

for method in methods:
if method not in attrs:
raise SanicException(
Expand Down
1 change: 0 additions & 1 deletion sanic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ def load_module_from_file_location(
location = location.decode(encoding)

if isinstance(location, Path) or "/" in location or "$" in location:

if not isinstance(location, Path):
# A) Check if location contains any environment variables
# in format ${some_env_var}.
Expand Down
9 changes: 9 additions & 0 deletions tests/test_response_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,12 @@ def do_pop(request: Request, response: JSONResponse):

_, resp = json_app.test_client.get("/json-pop")
assert resp.body == json_dumps(["b"]).encode()


def test_json_response_class_sets_proper_content_type(json_app: Sanic):
@json_app.get("/json-class")
async def handler(request: Request):
return JSONResponse(JSON_BODY)

_, resp = json_app.test_client.get("/json-class")
assert resp.headers["content-type"] == "application/json"

0 comments on commit fc82b23

Please sign in to comment.