Skip to content

Commit

Permalink
Relax content-type check (#22)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Buades Marcos <daniel@buad.es>
  • Loading branch information
florimondmanca and dbuades authored Dec 22, 2023
1 parent 0b5e9fd commit dcb6c7b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/httpx_sse/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def __init__(self, response: httpx.Response) -> None:

def _check_content_type(self) -> None:
content_type = self._response.headers.get("content-type", "").partition(";")[0]
if content_type != "text/event-stream":
if "text/event-stream" not in content_type:
raise SSEError(
"Expected response header Content-Type to be 'text/event-stream', "
"Expected response header Content-Type to contain 'text/event-stream', "
f"got {content_type!r}"
)

Expand Down
13 changes: 11 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,24 @@
from httpx_sse import SSEError, aconnect_sse, connect_sse


def test_connect_sse() -> None:
@pytest.mark.parametrize(
"content_type",
[
pytest.param("text/event-stream", id="exact"),
pytest.param(
"application/json, text/event-stream; charset=utf-8", id="contains"
),
],
)
def test_connect_sse(content_type: str) -> None:
def handler(request: httpx.Request) -> httpx.Response:
if request.url.path == "/":
return httpx.Response(200, text="Hello, world!")
else:
assert request.url.path == "/sse"
text = "data: test\n\n"
return httpx.Response(
200, headers={"content-type": "text/event-stream"}, text=text
200, headers={"content-type": content_type}, text=text
)

with httpx.Client(transport=httpx.MockTransport(handler)) as client:
Expand Down

0 comments on commit dcb6c7b

Please sign in to comment.