1import pytest
2
3from . import get_host, request
4
5
6@pytest.mark.parametrize(
7    "hostname, port_type, status",
8    [
9        # Valid hosts
10        ("localhost", "server_port", 200),
11        ("127.0.0.1", "server_port", 200),
12        ("[::1]", "server_port", 200),
13        ("192.168.8.1", "server_port", 200),
14        ("[fdf8:f535:82e4::53]", "server_port", 200),
15        # Invalid hosts
16        ("localhost", "default_port", 500),
17        ("127.0.0.1", "default_port", 500),
18        ("[::1]", "default_port", 500),
19        ("192.168.8.1", "default_port", 500),
20        ("[fdf8:f535:82e4::53]", "default_port", 500),
21        ("example.org", "server_port", 500),
22        ("example.org", "wrong_port", 500),
23        ("example.org", "default_port", 500),
24        ("localhost", "wrong_port", 500),
25        ("127.0.0.1", "wrong_port", 500),
26        ("[::1]", "wrong_port", 500),
27        ("192.168.8.1", "wrong_port", 500),
28        ("[fdf8:f535:82e4::53]", "wrong_port", 500),
29    ],
30    ids=[
31        # Valid hosts
32        "localhost with same port as server",
33        "127.0.0.1 (loopback) with same port as server",
34        "[::1] (ipv6 loopback) with same port as server",
35        "ipv4 address with same port as server",
36        "ipv6 address with same port as server",
37        # Invalid hosts
38        "localhost with default port",
39        "127.0.0.1 (loopback) with default port",
40        "[::1] (ipv6 loopback) with default port",
41        "ipv4 address with default port",
42        "ipv6 address with default port",
43        "random hostname with the same port as server",
44        "random hostname with a different port than server",
45        "random hostname with default port",
46        "localhost with a different port than server",
47        "127.0.0.1 (loopback) with a different port than server",
48        "[::1] (ipv6 loopback) with a different port than server",
49        "ipv4 address with a different port than server",
50        "ipv6 address with a different port than server",
51    ],
52)
53def test_host_header(configuration, hostname, port_type, status):
54    host = get_host(port_type, hostname, configuration["port"])
55    response = request(configuration["host"], configuration["port"], host=host)
56
57    assert response.status == status
58
59
60@pytest.mark.parametrize(
61    "origin, add_port, status",
62    [
63        (None, False, 200),
64        ("", False, 500),
65        ("sometext", False, 500),
66        ("http://localhost", True, 500),
67    ],
68)
69def test_origin_header(configuration, origin, add_port, status):
70    if add_port:
71        origin = f"{origin}:{configuration['port']}"
72    response = request(configuration["host"], configuration["port"], origin=origin)
73    assert response.status == status
74