|
1 | 1 | import asyncio |
2 | 2 | from functools import partial |
| 3 | +import logging |
3 | 4 | import os |
4 | 5 | import random |
5 | 6 |
|
|
10 | 11 | from ddtrace.constants import ERROR_MSG |
11 | 12 | from ddtrace.contrib.asgi import TraceMiddleware |
12 | 13 | from ddtrace.contrib.asgi import span_from_scope |
| 14 | +from ddtrace.contrib.internal.asgi.middleware import _parse_response_cookies |
13 | 15 | from ddtrace.propagation import http as http_propagation |
14 | 16 | from tests.conftest import DEFAULT_DDTRACE_SUBPROCESS_TEST_SERVICE_NAME |
15 | 17 | from tests.utils import DummyTracer |
@@ -634,6 +636,52 @@ async def test_tasks_asgi_without_more_body(scope, tracer, test_spans): |
634 | 636 | assert request_span.duration < 1 |
635 | 637 |
|
636 | 638 |
|
| 639 | +@pytest.mark.asyncio |
| 640 | +async def test_request_parse_response_cookies(tracer, test_spans, caplog): |
| 641 | + """ |
| 642 | + Regression test https://github.com/DataDog/dd-trace-py/issues/11818 |
| 643 | + """ |
| 644 | + |
| 645 | + async def tasks_cookies(scope, receive, send): |
| 646 | + message = await receive() |
| 647 | + if message.get("type") == "http.request": |
| 648 | + await send({"type": "http.response.start", "status": 200, "headers": [[b"set-cookie", b"test_cookie"]]}) |
| 649 | + await send({"type": "http.response.body", "body": b"*"}) |
| 650 | + await asyncio.sleep(1) |
| 651 | + |
| 652 | + with caplog.at_level(logging.DEBUG): |
| 653 | + app = TraceMiddleware(tasks_cookies, tracer=tracer) |
| 654 | + async with httpx.AsyncClient(app=app) as client: |
| 655 | + response = await client.get("http://testserver/") |
| 656 | + assert response.status_code == 200 |
| 657 | + |
| 658 | + assert "failed to extract response cookies" not in caplog.text |
| 659 | + |
| 660 | + |
| 661 | +@pytest.mark.parametrize( |
| 662 | + "headers,expected_result", |
| 663 | + [ |
| 664 | + ({}, {}), |
| 665 | + ({"cookie": "cookie1=value1"}, {}), |
| 666 | + ({"header-1": ""}, {}), |
| 667 | + ({"Set-cookie": "cookie1=value1"}, {}), |
| 668 | + ({"set-Cookie": "cookie1=value1"}, {}), |
| 669 | + ({"SET-cookie": "cookie1=value1"}, {}), |
| 670 | + ({"set-cookie": "a"}, {}), |
| 671 | + ({"set-cookie": "1234"}, {}), |
| 672 | + ({"set-cookie": "cookie1=value1"}, {"cookie1": "value1"}), |
| 673 | + ({"set-cookie": "cookie2=value1=value2"}, {"cookie2": "value1=value2"}), |
| 674 | + ({"set-cookie": "cookie3=="}, {"cookie3": "="}), |
| 675 | + ], |
| 676 | +) |
| 677 | +def test__parse_response_cookies(headers, expected_result, caplog): |
| 678 | + with caplog.at_level(logging.DEBUG): |
| 679 | + result = _parse_response_cookies(headers) |
| 680 | + |
| 681 | + assert "failed to extract response cookies" not in caplog.text |
| 682 | + assert result == expected_result |
| 683 | + |
| 684 | + |
637 | 685 | @pytest.mark.asyncio |
638 | 686 | async def test_tasks_asgi_with_more_body(scope, tracer, test_spans): |
639 | 687 | """ |
|
0 commit comments