diff options
| author | David Lord <davidism@gmail.com> | 2020-10-26 16:49:13 -0700 |
|---|---|---|
| committer | David Lord <davidism@gmail.com> | 2020-10-26 16:49:13 -0700 |
| commit | 5cc1e1c60d3f7a4140069147cf191c6da8de9dce (patch) | |
| tree | f627a156822208bac95c111867bbe4edec319332 /tests | |
| parent | 47bf15d663ccff521d48f4809bb6e4d690231798 (diff) | |
| download | werkzeug-test-dev-server.tar.gz | |
only use http.clienttest-dev-server
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/conftest.py | 43 | ||||
| -rw-r--r-- | tests/res/chunked.http | 25 | ||||
| -rw-r--r-- | tests/test_apps/reloader_app.py | 8 | ||||
| -rw-r--r-- | tests/test_debug.py | 2 | ||||
| -rw-r--r-- | tests/test_serving.py | 40 |
5 files changed, 33 insertions, 85 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index ab7943f5..e18533b7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,11 +4,8 @@ import os import socket import ssl import sys -import urllib.error -import urllib.request from itertools import count from pathlib import Path -from pathlib import PurePath import pytest from xprocess import ProcessStarter @@ -41,26 +38,7 @@ class UnixSocketHTTPConnection(http.client.HTTPConnection): self.sock.connect(self.host) -class UnixSocketHandler(urllib.request.AbstractHTTPHandler): - def unix_request(self, req): - h = s = PurePath(req.selector) - - while not h.suffix == ".sock": - h = h.parent - - req.host = str(h) - req.selector = f"/{s.relative_to(h)}" if s != h else "/" - return self.do_request_(req) - - def unix_open(self, req): - return self.do_open(UnixSocketHTTPConnection, req) - - class DevServerClient: - opener = urllib.request.build_opener( - UnixSocketHandler, urllib.request.HTTPSHandler(context=ssl.SSLContext()) - ) - def __init__(self, kwargs): host = kwargs.get("hostname", "127.0.0.1") @@ -74,7 +52,7 @@ class DevServerClient: self.addr = f"{host}:{port}" self.url = f"{scheme}://{self.addr}" else: - self.addr = host[7:] + self.addr = host[7:] # strip "unix://" self.url = host self.log = None @@ -97,17 +75,16 @@ class DevServerClient: return http.client.HTTPConnection(self.addr, **kwargs) - def open(self, path="", **kwargs): - request = urllib.request.Request(f"{self.url}{path}", **kwargs) + def request(self, path="", **kwargs): + kwargs.setdefault("method", "GET") + kwargs.setdefault("url", path) + conn = self.connect() + conn.request(**kwargs) - try: - with self.opener.open(request) as response: - response.data = response.read() - except urllib.error.HTTPError as e: - response = e + with conn.getresponse() as response: + response.data = response.read() - with response: - response.data = response.read() + conn.close() if response.headers.get("Content-Type", "").startswith("application/json"): response.json = json.loads(response.data) @@ -137,7 +114,7 @@ def dev_server(xprocess, request, tmp_path): @cached_property def pattern(self): - client.open("/ensure") + client.request("/ensure") return "GET /ensure" xp_name = f"dev_server-{request.node.name}" diff --git a/tests/res/chunked.http b/tests/res/chunked.http deleted file mode 100644 index 44aa71f6..00000000 --- a/tests/res/chunked.http +++ /dev/null @@ -1,25 +0,0 @@ -94
-----------------------------898239224156930639461866
-Content-Disposition: form-data; name="file"; filename="test.txt"
-Content-Type: text/plain
-
-
-f
-This is a test -
-2
-
-
-65
-----------------------------898239224156930639461866
-Content-Disposition: form-data; name="type"
-
-
-a
-text/plain
-3a
-
-----------------------------898239224156930639461866--
-
-0
-
diff --git a/tests/test_apps/reloader_app.py b/tests/test_apps/reloader_app.py index 531b359f..da404dc5 100644 --- a/tests/test_apps/reloader_app.py +++ b/tests/test_apps/reloader_app.py @@ -1,3 +1,4 @@ +import os import sys from werkzeug import _reloader @@ -6,9 +7,10 @@ from werkzeug.wrappers import Response # Tox puts the tmp dir in the venv sys.prefix, patch the reloader so # it doesn't skip real_app. -prefixes = set(_reloader._ignore_prefixes) -prefixes -= {p for p in (sys.prefix, sys.exec_prefix) if "/.tox/" in p} -_reloader._ignore_prefixes = tuple(prefixes) +if "TOX_ENV_DIR" in os.environ: + _reloader._ignore_prefixes = tuple( + set(_reloader._ignore_prefixes) - {sys.prefix, sys.exec_prefix} + ) @Request.application diff --git a/tests/test_debug.py b/tests/test_debug.py index ddeef5f8..a0cdcc89 100644 --- a/tests/test_debug.py +++ b/tests/test_debug.py @@ -305,7 +305,7 @@ def test_get_machine_id(): @pytest.mark.parametrize("crash", (True, False)) def test_basic(dev_server, crash): c = dev_server(use_debugger=True) - r = c.open("/crash" if crash else "") + r = c.request("/crash" if crash else "") assert r.status == (500 if crash else 200) if crash: diff --git a/tests/test_serving.py b/tests/test_serving.py index 43a8db65..1d712f9b 100644 --- a/tests/test_serving.py +++ b/tests/test_serving.py @@ -36,50 +36,44 @@ def test_server(tmp_path, dev_server, kwargs: dict): kwargs["hostname"] = f"unix://{tmp_path / 'test.sock'}" client = dev_server(**kwargs) - r = client.open() + r = client.request() assert r.status == 200 assert r.json["PATH_INFO"] == "/" def test_untrusted_host(standard_app): - conn = standard_app.connect() - conn.request( - "GET", + r = standard_app.request( "http://missing.test:1337/index.html#ignore", headers={"x-base-url": standard_app.url}, ) - response = conn.getresponse() - environ = json.load(response) - response.close() - conn.close() - assert environ["HTTP_HOST"] == "missing.test:1337" - assert environ["PATH_INFO"] == "/index.html" - host, _, port = environ["HTTP_X_BASE_URL"].rpartition(":") - assert environ["SERVER_NAME"] == host.partition("http://")[2] - assert environ["SERVER_PORT"] == port + assert r.json["HTTP_HOST"] == "missing.test:1337" + assert r.json["PATH_INFO"] == "/index.html" + host, _, port = r.json["HTTP_X_BASE_URL"].rpartition(":") + assert r.json["SERVER_NAME"] == host.partition("http://")[2] + assert r.json["SERVER_PORT"] == port def test_double_slash_path(standard_app): - r = standard_app.open("//double-slash") + r = standard_app.request("//double-slash") assert "double-slash" not in r.json["HTTP_HOST"] assert r.json["PATH_INFO"] == "/double-slash" def test_500_error(standard_app): - r = standard_app.open("/crash") + r = standard_app.request("/crash") assert r.status == 500 assert b"Internal Server Error" in r.data def test_ssl_dev_cert(tmp_path, dev_server): client = dev_server(ssl_context=make_ssl_devcert(tmp_path)) - r = client.open() + r = client.request() assert r.json["wsgi.url_scheme"] == "https" def test_ssl_object(dev_server): client = dev_server(ssl_context="custom") - r = client.open() + r = client.request() assert r.json["wsgi.url_scheme"] == "https" @@ -93,12 +87,12 @@ def test_reloader_sys_path(tmp_path, dev_server, reloader_type): real_path.write_text("syntax error causes import error") client = dev_server("reloader", reloader_type=reloader_type) - assert client.open().status == 500 + assert client.request().status == 500 shutil.copyfile(Path(__file__).parent / "test_apps" / "standard_app.py", real_path) client.wait_for_log(f" * Detected change in {str(real_path)!r}, reloading") client.wait_for_reload() - assert client.open().status == 200 + assert client.request().status == 200 def test_windows_get_args_for_reloading(monkeypatch, tmp_path): @@ -124,11 +118,11 @@ def test_wrong_protocol(standard_app): def test_content_type_and_length(standard_app): - r = standard_app.open() + r = standard_app.request() assert "CONTENT_TYPE" not in r.json assert "CONTENT_LENGTH" not in r.json - r = standard_app.open(data=b"{}", headers={"content-type": "application/json"}) + r = standard_app.request(body=b"{}", headers={"content-type": "application/json"}) assert r.json["CONTENT_TYPE"] == "application/json" assert r.json["CONTENT_LENGTH"] == "2" @@ -189,7 +183,7 @@ def test_multiple_headers_concatenated(standard_app): https://tools.ietf.org/html/rfc3875#section-4.1.18 """ - # Can't use open or conn.request, don't support multiple values. + # conn.request doesn't support multiple values. conn = standard_app.connect() conn.putrequest("GET", "/") conn.putheader("XYZ", "a ") # trailing space is preserved @@ -212,7 +206,7 @@ def test_multiline_header_folding(standard_app): https://tools.ietf.org/html/rfc2616#section-2.2 """ - # Can't use open or conn.request, don't support multiline values. + # conn.request doesn't support multiline values. conn = standard_app.connect() conn.putrequest("GET", "/") conn.putheader("XYZ", "first", "second", "third") |
