if message["type"] == "websocket.receive":
data = await self.decode(websocket, message)
await self.on_receive(websocket, data)
- elif message["type"] == "websocket.disconnect":
+ elif message["type"] == "websocket.disconnect": # pragma: no branch
close_code = int(message.get("code") or status.WS_1000_NORMAL_CLOSURE)
break
except Exception as exc:
exc_info: typing.Any = None,
) -> None:
self.exc_info = exc_info
- if not self.response_started:
+ if not self.response_started: # pragma: no branch
self.response_started = True
status_code_string, _ = status.split(" ", 1)
status_code = int(status_code_string)
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
request = self.context.get("request", {})
extensions = request.get("extensions", {})
- if "http.response.debug" in extensions:
+ if "http.response.debug" in extensions: # pragma: no branch
await send(
{
"type": "http.response.debug",
assert response.headers["set-cookie"] == "mycookie=myvalue; SameSite=lax"
+def test_set_cookie_samesite_none(test_client_factory: TestClientFactory) -> None:
+ async def app(scope: Scope, receive: Receive, send: Send) -> None:
+ response = Response("Hello, world!", media_type="text/plain")
+ response.set_cookie("mycookie", "myvalue", samesite=None)
+ await response(scope, receive, send)
+
+ client = test_client_factory(app)
+ response = client.get("/")
+ assert response.text == "Hello, world!"
+ assert response.headers["set-cookie"] == "mycookie=myvalue; Path=/"
+
+
@pytest.mark.parametrize(
"expires",
[
assert second_resp.content == b""
+def test_staticfiles_200_with_etag_mismatch(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
+ path = os.path.join(tmpdir, "example.txt")
+ with open(path, "w") as file:
+ file.write("<file content>")
+
+ app = StaticFiles(directory=tmpdir)
+ client = test_client_factory(app)
+ first_resp = client.get("/example.txt")
+ assert first_resp.status_code == 200
+ assert first_resp.headers["etag"] != '"123"'
+ second_resp = client.get("/example.txt", headers={"if-none-match": '"123"'})
+ assert second_resp.status_code == 200
+ assert second_resp.content == b"<file content>"
+
+
def test_staticfiles_304_with_last_modified_compare_last_req(
tmpdir: Path, test_client_factory: TestClientFactory
) -> None: