Skip to content

Commit 155918b

Browse files
azmeukcsernazs
authored andcommitted
Added IPV4 and IPV6 only fixtures
1 parent 83f4f88 commit 155918b

4 files changed

Lines changed: 75 additions & 1 deletion

File tree

pytest_httpserver/httpserver.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import abc
2+
import ipaddress
23
import json
34
import queue
45
import re
@@ -663,7 +664,9 @@ def url_for(self, suffix: str):
663664
else:
664665
protocol = "https"
665666

666-
return "{}://{}:{}{}".format(protocol, self.host, self.port, suffix)
667+
host = self.format_host(self.host)
668+
669+
return "{}://{}:{}{}".format(protocol, host, self.port, suffix)
667670

668671
def create_matcher(self, *args, **kwargs) -> RequestMatcher:
669672
"""
@@ -837,6 +840,24 @@ def __exit__(self, *args, **kwargs):
837840
if self.is_running():
838841
self.stop()
839842

843+
@staticmethod
844+
def format_host(host):
845+
"""
846+
Formats a hostname so it can be used in a URL.
847+
Notably, this adds brackets around IPV6 addresses when
848+
they are missing.
849+
"""
850+
try:
851+
ipaddress.IPv6Address(host)
852+
is_ipv6 = True
853+
except ValueError:
854+
is_ipv6 = False
855+
856+
if is_ipv6 and not host.startswith("[") and not host.endswith("]"):
857+
return f"[{host}]"
858+
859+
return host
860+
840861

841862
class HTTPServer(HTTPServerBase): # pylint: disable=too-many-instance-attributes
842863
"""

pytest_httpserver/pytest_plugin.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,37 @@ def httpserver(make_httpserver):
6666
server = make_httpserver
6767
yield server
6868
server.clear()
69+
70+
71+
@pytest.fixture(scope="session")
72+
def make_httpserver_ipv4(httpserver_ssl_context):
73+
server = HTTPServer(host="127.0.0.1", port=0, ssl_context=httpserver_ssl_context)
74+
server.start()
75+
yield server
76+
server.clear()
77+
if server.is_running():
78+
server.stop()
79+
80+
81+
@pytest.fixture
82+
def httpserver_ipv4(make_httpserver_ipv4):
83+
server = make_httpserver_ipv4
84+
yield server
85+
server.clear()
86+
87+
88+
@pytest.fixture(scope="session")
89+
def make_httpserver_ipv6(httpserver_ssl_context):
90+
server = HTTPServer(host="::1", port=0, ssl_context=httpserver_ssl_context)
91+
server.start()
92+
yield server
93+
server.clear()
94+
if server.is_running():
95+
server.stop()
96+
97+
98+
@pytest.fixture
99+
def httpserver_ipv6(make_httpserver_ipv6):
100+
server = make_httpserver_ipv6
101+
yield server
102+
server.clear()

tests/test_ip_protocols.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import requests
2+
3+
4+
def test_ipv4(httpserver_ipv4):
5+
httpserver_ipv4.expect_request("/").respond_with_data("OK")
6+
assert httpserver_ipv4.host == "127.0.0.1"
7+
8+
response = requests.get(httpserver_ipv4.url_for("/"))
9+
assert response.text == "OK"
10+
11+
12+
def test_ipv6(httpserver_ipv6):
13+
httpserver_ipv6.expect_request("/").respond_with_data("OK")
14+
assert httpserver_ipv6.host == "::1"
15+
assert httpserver_ipv6.url_for("/") == f"http://[::1]:{httpserver_ipv6.port}/"
16+
17+
response = requests.get(httpserver_ipv6.url_for("/"))
18+
assert response.text == "OK"

tests/test_release.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ def test_sdist_contents(build: Build, version: str):
199199
"test_blocking_httpserver_howto.py",
200200
"test_handler_errors.py",
201201
"test_headers.py",
202+
"test_ip_protocols.py",
202203
"test_json_matcher.py",
203204
"test_mixed.py",
204205
"test_oneshot.py",

0 commit comments

Comments
 (0)