1import pytest
2
3from .env import H2Conf
4
5
6class TestUpgrade:
7
8    @pytest.fixture(autouse=True, scope='class')
9    def _class_scope(self, env):
10        H2Conf(env).add_vhost_test1().add_vhost_test2().add_vhost_noh2(
11        ).start_vhost(domains=[f"test3.{env.http_tld}"], port=env.https_port, doc_root="htdocs/test1"
12        ).add(
13            """
14            Protocols h2 http/1.1
15            Header unset Upgrade"""
16        ).end_vhost(
17        ).start_vhost(domains=[f"test1b.{env.http_tld}"], port=env.http_port, doc_root="htdocs/test1"
18        ).add(
19            """
20            Protocols h2c http/1.1
21            H2Upgrade off
22            <Location /006.html>
23                H2Upgrade on
24            </Location>"""
25        ).end_vhost(
26        ).install()
27        assert env.apache_restart() == 0
28
29    # accessing http://test1, will not try h2 and advertise h2 in the response
30    def test_h2_103_01(self, env):
31        url = env.mkurl("http", "test1", "/index.html")
32        r = env.curl_get(url)
33        assert 0 == r.exit_code
34        assert r.response
35        assert "upgrade" in r.response["header"]
36        assert "h2c" == r.response["header"]["upgrade"]
37
38    # accessing http://noh2, will not advertise, because noh2 host does not have it enabled
39    def test_h2_103_02(self, env):
40        url = env.mkurl("http", "noh2", "/index.html")
41        r = env.curl_get(url)
42        assert 0 == r.exit_code
43        assert r.response
44        assert "upgrade" not in r.response["header"]
45
46    # accessing http://test2, will not advertise, because h2 has less preference than http/1.1
47    def test_h2_103_03(self, env):
48        url = env.mkurl("http", "test2", "/index.html")
49        r = env.curl_get(url)
50        assert 0 == r.exit_code
51        assert r.response
52        assert "upgrade" not in r.response["header"]
53
54    # accessing https://noh2, will not advertise, because noh2 host does not have it enabled
55    def test_h2_103_04(self, env):
56        url = env.mkurl("https", "noh2", "/index.html")
57        r = env.curl_get(url)
58        assert 0 == r.exit_code
59        assert r.response
60        assert "upgrade" not in r.response["header"]
61
62    # accessing https://test2, will not advertise, because h2 has less preference than http/1.1
63    def test_h2_103_05(self, env):
64        url = env.mkurl("https", "test2", "/index.html")
65        r = env.curl_get(url)
66        assert 0 == r.exit_code
67        assert r.response
68        assert "upgrade" not in r.response["header"]
69
70    # accessing https://test1, will advertise h2 in the response
71    def test_h2_103_06(self, env):
72        url = env.mkurl("https", "test1", "/index.html")
73        r = env.curl_get(url, options=["--http1.1"])
74        assert 0 == r.exit_code
75        assert r.response
76        assert "upgrade" in r.response["header"]
77        assert "h2" == r.response["header"]["upgrade"]
78
79    # accessing https://test3, will not send Upgrade since it is suppressed
80    def test_h2_103_07(self, env):
81        url = env.mkurl("https", "test3", "/index.html")
82        r = env.curl_get(url, options=["--http1.1"])
83        assert 0 == r.exit_code
84        assert r.response
85        assert "upgrade" not in r.response["header"]
86
87    # upgrade to h2c for a request, where h2c is preferred
88    def test_h2_103_20(self, env):
89        url = env.mkurl("http", "test1", "/index.html")
90        r = env.nghttp().get(url, options=["-u"])
91        assert r.response["status"] == 200
92
93    # upgrade to h2c for a request where http/1.1 is preferred, but the clients upgrade
94    # wish is honored nevertheless
95    def test_h2_103_21(self, env):
96        url = env.mkurl("http", "test2", "/index.html")
97        r = env.nghttp().get(url, options=["-u"])
98        assert 404 == r.response["status"]
99
100    # ugrade to h2c on a host where h2c is not enabled will fail
101    def test_h2_103_22(self, env):
102        url = env.mkurl("http", "noh2", "/index.html")
103        r = env.nghttp().get(url, options=["-u"])
104        assert not r.response
105
106    # ugrade to h2c on a host where h2c is preferred, but Upgrade is disabled
107    def test_h2_103_23(self, env):
108        url = env.mkurl("http", "test1b", "/index.html")
109        r = env.nghttp().get(url, options=["-u"])
110        assert not r.response
111
112    # ugrade to h2c on a host where h2c is preferred, but Upgrade is disabled on the server,
113    # but allowed for a specific location
114    def test_h2_103_24(self, env):
115        url = env.mkurl("http", "test1b", "/006.html")
116        r = env.nghttp().get(url, options=["-u"])
117        assert r.response["status"] == 200
118