Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion requirements-testing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pytest>=6.0.0
tomli;python_version<"3.11" # Only needed for pytest on Python < 3.11
pytest-cov
pytest-forked
pytest-localserver
pytest-timeout
Comment thread
sentry-warden[bot] marked this conversation as resolved.
pytest-watch
jsonschema
Expand Down
2 changes: 1 addition & 1 deletion scripts/populate_tox/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@
"deps": {
"*": ["websockets<11.0", "aiohttp"],
">=22": ["sanic-testing"],
"py3.6": ["aiocontextvars==0.2.1"],
"py3.6": ["aiocontextvars==0.2.1", "dataclasses"],
"py3.8": ["tracerite<1.1.2"],
},
"num_versions": 4,
Expand Down
890 changes: 500 additions & 390 deletions scripts/populate_tox/package_dependencies.jsonl

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions scripts/populate_tox/tox.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ deps =
# === Common ===
py3.8-common: hypothesis
common: pytest-asyncio
common: pytest_localserver
common: httpcore[asyncio]
common: werkzeug
# See https://github.com/pytest-dev/pytest/issues/9621
# and https://github.com/pytest-dev/pytest-forked/issues/67
# for justification of the upper bound on pytest
Expand All @@ -98,7 +100,9 @@ deps =
{py3.6,py3.7}-gevent: pytest<7.0.0
{py3.8,py3.9,py3.10,py3.11,py3.12}-gevent: pytest
gevent: pytest-asyncio
gevent: pytest_localserver
gevent: setuptools<82
gevent: werkzeug
{py3.10,py3.11}-gevent: zope.event<5.0.0
{py3.10,py3.11}-gevent: zope.interface<8.0

Expand Down
114 changes: 66 additions & 48 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@
import brotli
import jsonschema
import pytest
from pytest_localserver.http import WSGIServer
from werkzeug.wrappers import Request, Response

try:
from pytest_localserver.http import WSGIServer
except ImportError:
WSGIServer = None

try:
from werkzeug.wrappers import Request, Response
except ImportError:
Request = None
Response = None

try:
from starlette.testclient import TestClient
Expand Down Expand Up @@ -1615,52 +1624,61 @@ def __ne__(self, other):
CapturedData = namedtuple("CapturedData", ["path", "event", "envelope", "compressed"])


class CapturingServer(WSGIServer):
def __init__(self, host="127.0.0.1", port=0, ssl_context=None):
WSGIServer.__init__(self, host, port, self, ssl_context=ssl_context)
self.code = 204
self.headers = {}
self.captured = []

def respond_with(self, code=200, headers=None):
self.code = code
if headers:
self.headers = headers

def clear_captured(self):
del self.captured[:]
@pytest.fixture(scope="module")
def wsgi_capturing_server():
assert WSGIServer is not None

class CapturingServer(WSGIServer):
def __init__(self, host="127.0.0.1", port=0, ssl_context=None):
WSGIServer.__init__(self, host, port, self, ssl_context=ssl_context)
self.code = 204
self.headers = {}
self.captured = []

def respond_with(self, code=200, headers=None):
self.code = code
if headers:
self.headers = headers

def clear_captured(self):
del self.captured[:]

def __call__(self, environ, start_response):
"""
This is the WSGI application.
"""
assert Request is not None
assert Response is not None

request = Request(environ)
event = envelope = None
content_encoding = request.headers.get("content-encoding")
if content_encoding == "gzip":
rdr = gzip.GzipFile(fileobj=io.BytesIO(request.data))
compressed = True
elif content_encoding == "br":
rdr = io.BytesIO(brotli.decompress(request.data))
compressed = True
else:
rdr = io.BytesIO(request.data)
compressed = False

def __call__(self, environ, start_response):
"""
This is the WSGI application.
"""
request = Request(environ)
event = envelope = None
content_encoding = request.headers.get("content-encoding")
if content_encoding == "gzip":
rdr = gzip.GzipFile(fileobj=io.BytesIO(request.data))
compressed = True
elif content_encoding == "br":
rdr = io.BytesIO(brotli.decompress(request.data))
compressed = True
else:
rdr = io.BytesIO(request.data)
compressed = False

if request.mimetype == "application/json":
event = parse_json(rdr.read())
else:
envelope = Envelope.deserialize_from(rdr)

self.captured.append(
CapturedData(
path=request.path,
event=event,
envelope=envelope,
compressed=compressed,
if request.mimetype == "application/json":
event = parse_json(rdr.read())
else:
envelope = Envelope.deserialize_from(rdr)

self.captured.append(
CapturedData(
path=request.path,
event=event,
envelope=envelope,
compressed=compressed,
)
)
)

response = Response(status=self.code)
response.headers.extend(self.headers)
return response(environ, start_response)
response = Response(status=self.code)
response.headers.extend(self.headers)
return response(environ, start_response)

return CapturingServer()
14 changes: 6 additions & 8 deletions tests/test_gevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import sentry_sdk
from sentry_sdk._compat import PY37, PY38
from tests.conftest import CapturingServer

pytest.importorskip("gevent")

Expand All @@ -25,18 +24,17 @@ def monkeypatched_gevent():


@pytest.fixture
def capturing_server(request):
server = CapturingServer()
server.start()
request.addfinalizer(server.stop)
return server
def capturing_server(request, wsgi_capturing_server):
wsgi_capturing_server.start()
request.addfinalizer(wsgi_capturing_server.stop)
return wsgi_capturing_server


@pytest.fixture
def make_client(request, capturing_server):
def make_client(request, wsgi_capturing_server):
def inner(**kwargs):
return sentry_sdk.Client(
"http://foobar@{}/132".format(capturing_server.url[len("http://") :]),
"http://foobar@{}/132".format(wsgi_capturing_server.url[len("http://") :]),
**kwargs,
)

Expand Down
6 changes: 2 additions & 4 deletions tests/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

import pytest

from tests.conftest import CapturingServer

try:
import httpcore
except (ImportError, ModuleNotFoundError):
Expand Down Expand Up @@ -73,9 +71,9 @@ def _make_async_transport_options(**overrides):


@pytest.fixture(scope="module", autouse=True)
def make_capturing_server(request):
def make_capturing_server(request, wsgi_capturing_server):
global server
server = CapturingServer()
server = wsgi_capturing_server
server.start()
request.addfinalizer(server.stop)

Expand Down
Loading
Loading