1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
import re
import pytest
from .env import H2Conf, H2TestEnv
@pytest.mark.skipif(condition=H2TestEnv.is_unsupported, reason="mod_http2 not supported here")
class TestGet:
@pytest.fixture(autouse=True, scope='class')
def _class_scope(self, env):
H2Conf(env).add_vhost_cgi(
proxy_self=True, h2proxy_self=True
).add_vhost_test1(
proxy_self=True, h2proxy_self=True
).install()
assert env.apache_restart() == 0
# check SSL environment variables from CGI script
def test_h2_003_01(self, env):
url = env.mkurl("https", "cgi", "/hello.py")
r = env.curl_get(url, 5, options=["--tlsv1.2"])
assert r.response["status"] == 200
assert r.response["json"]["protocol"] == "HTTP/2.0"
assert r.response["json"]["https"] == "on"
tls_version = r.response["json"]["ssl_protocol"]
assert tls_version in ["TLSv1.2", "TLSv1.3"]
assert r.response["json"]["h2"] == "on"
assert r.response["json"]["h2push"] == "off"
r = env.curl_get(url, 5, options=["--http1.1", "--tlsv1.2"])
assert r.response["status"] == 200
assert "HTTP/1.1" == r.response["json"]["protocol"]
assert "on" == r.response["json"]["https"]
tls_version = r.response["json"]["ssl_protocol"]
assert tls_version in ["TLSv1.2", "TLSv1.3"]
assert "" == r.response["json"]["h2"]
assert "" == r.response["json"]["h2push"]
# retrieve a html file from the server and compare it to its source
def test_h2_003_02(self, env):
with open(env.htdocs_src("test1/index.html"), mode='rb') as file:
src = file.read()
url = env.mkurl("https", "test1", "/index.html")
r = env.curl_get(url, 5)
assert r.response["status"] == 200
assert "HTTP/2" == r.response["protocol"]
assert src == r.response["body"]
url = env.mkurl("https", "test1", "/index.html")
r = env.curl_get(url, 5, options=["--http1.1"])
assert r.response["status"] == 200
assert "HTTP/1.1" == r.response["protocol"]
assert src == r.response["body"]
# retrieve chunked content from a cgi script
def check_necho(self, env, n, text):
url = env.mkurl("https", "cgi", "/necho.py")
r = env.curl_get(url, 5, options=["-F", f"count={n}", "-F", f"text={text}"])
assert r.response["status"] == 200
exp = ""
for i in range(n):
exp += text + "\n"
assert exp == r.response["body"].decode('utf-8')
def test_h2_003_10(self, env):
self.check_necho(env, 10, "0123456789")
def test_h2_003_11(self, env):
self.check_necho(env, 100, "0123456789")
def test_h2_003_12(self, env):
self.check_necho(env, 1000, "0123456789")
def test_h2_003_13(self, env):
self.check_necho(env, 10000, "0123456789")
def test_h2_003_14(self, env):
self.check_necho(env, 100000, "0123456789")
# github issue #126
def test_h2_003_20(self, env):
url = env.mkurl("https", "test1", "/006/")
r = env.curl_get(url, 5)
assert r.response["status"] == 200
body = r.response["body"].decode('utf-8')
# our doctype varies between branches and in time, lets not compare
body = re.sub(r'^<!DOCTYPE[^>]+>', '', body)
assert '''
<html>
<head>
<title>Index of /006</title>
</head>
<body>
<h1>Index of /006</h1>
<ul><li><a href="/"> Parent Directory</a></li>
<li><a href="006.css"> 006.css</a></li>
<li><a href="006.js"> 006.js</a></li>
<li><a href="header.html"> header.html</a></li>
</ul>
</body></html>
''' == body
# github issue #133
def clean_header(self, s):
s = re.sub(r'\r\n', '\n', s, flags=re.MULTILINE)
s = re.sub(r'^date:.*\n', '', s, flags=re.MULTILINE)
s = re.sub(r'^server:.*\n', '', s, flags=re.MULTILINE)
s = re.sub(r'^last-modified:.*\n', '', s, flags=re.MULTILINE)
s = re.sub(r'^etag:.*\n', '', s, flags=re.MULTILINE)
s = re.sub(r'^vary:.*\n', '', s, flags=re.MULTILINE)
return re.sub(r'^accept-ranges:.*\n', '', s, flags=re.MULTILINE)
def test_h2_003_21(self, env):
url = env.mkurl("https", "test1", "/index.html")
r = env.curl_get(url, 5, options=["-I"])
assert r.response["status"] == 200
assert "HTTP/2" == r.response["protocol"]
s = self.clean_header(r.response["body"].decode('utf-8'))
assert '''HTTP/2 200
content-length: 2007
content-type: text/html
''' == s
r = env.curl_get(url, 5, options=["-I", url])
assert r.response["status"] == 200
assert "HTTP/2" == r.response["protocol"]
s = self.clean_header(r.response["body"].decode('utf-8'))
assert '''HTTP/2 200
content-length: 2007
content-type: text/html
HTTP/2 200
content-length: 2007
content-type: text/html
''' == s
# test conditionals: if-modified-since
@pytest.mark.parametrize("path", [
"/004.html", "/proxy/004.html", "/h2proxy/004.html"
])
def test_h2_003_30(self, env, path):
url = env.mkurl("https", "test1", path)
r = env.curl_get(url, 5)
assert r.response["status"] == 200
assert "HTTP/2" == r.response["protocol"]
h = r.response["header"]
assert "last-modified" in h
lastmod = h["last-modified"]
r = env.curl_get(url, 5, options=['-H', ("if-modified-since: %s" % lastmod)])
assert 304 == r.response["status"]
# test conditionals: if-etag
@pytest.mark.parametrize("path", [
"/004.html", "/proxy/004.html", "/h2proxy/004.html"
])
def test_h2_003_31(self, env, path):
url = env.mkurl("https", "test1", path)
r = env.curl_get(url, 5)
assert r.response["status"] == 200
assert "HTTP/2" == r.response["protocol"]
h = r.response["header"]
assert "etag" in h
etag = h["etag"]
r = env.curl_get(url, 5, options=['-H', ("if-none-match: %s" % etag)])
assert 304 == r.response["status"]
# test various response body lengths to work correctly
def test_h2_003_40(self, env):
n = 1001
while n <= 1025024:
url = env.mkurl("https", "cgi", f"/mnot164.py?count={n}&text=X")
r = env.curl_get(url, 5)
assert r.response["status"] == 200
assert "HTTP/2" == r.response["protocol"]
assert n == len(r.response["body"])
n *= 2
# test various response body lengths to work correctly
@pytest.mark.parametrize("n", [
0, 1, 1291, 1292, 80000, 80123, 81087, 98452
])
def test_h2_003_41(self, env, n):
url = env.mkurl("https", "cgi", f"/mnot164.py?count={n}&text=X")
r = env.curl_get(url, 5)
assert r.response["status"] == 200
assert "HTTP/2" == r.response["protocol"]
assert n == len(r.response["body"])
# test ranges
@pytest.mark.parametrize("path", [
"/004.html", "/proxy/004.html", "/h2proxy/004.html"
])
def test_h2_003_50(self, env, path, repeat):
# check that the resource supports ranges and we see its raw content-length
url = env.mkurl("https", "test1", path)
r = env.curl_get(url, 5)
assert r.response["status"] == 200
assert "HTTP/2" == r.response["protocol"]
h = r.response["header"]
assert "accept-ranges" in h
assert "bytes" == h["accept-ranges"]
assert "content-length" in h
clen = h["content-length"]
# get the first 1024 bytes of the resource, 206 status, but content-length as original
r = env.curl_get(url, 5, options=["-H", "range: bytes=0-1023"])
assert 206 == r.response["status"]
assert "HTTP/2" == r.response["protocol"]
assert 1024 == len(r.response["body"])
assert "content-length" in h
assert clen == h["content-length"]
# use an invalid scheme
def test_h2_003_51(self, env):
url = env.mkurl("https", "cgi", "/")
opt = ["-H:scheme: invalid"]
r = env.nghttp().get(url, options=opt)
assert r.exit_code == 0, r
assert r.response['status'] == 400
# use an differing scheme, but one that is acceptable
def test_h2_003_52(self, env):
url = env.mkurl("https", "cgi", "/")
opt = ["-H:scheme: http"]
r = env.nghttp().get(url, options=opt)
assert r.exit_code == 0, r
assert r.response['status'] == 200
# Test that we get a proper `Date` and `Server` headers on responses
def test_h2_003_60(self, env):
url = env.mkurl("https", "test1", "/index.html")
r = env.curl_get(url)
assert r.exit_code == 0, r
assert r.response['status'] == 200
assert 'date' in r.response['header']
assert 'server' in r.response['header']
# lets do some error tests
def test_h2_003_70(self, env):
url = env.mkurl("https", "cgi", "/h2test/error?status=500")
r = env.curl_get(url)
assert r.exit_code == 0, r
assert r.response['status'] == 500
url = env.mkurl("https", "cgi", "/h2test/error?error=timeout")
r = env.curl_get(url)
assert r.exit_code == 0, r
assert r.response['status'] == 408
# produce an error during response body
def test_h2_003_71(self, env, repeat):
url = env.mkurl("https", "cgi", "/h2test/error?body_error=timeout")
r = env.curl_get(url)
assert r.exit_code != 0, f"{r}"
url = env.mkurl("https", "cgi", "/h2test/error?body_error=reset")
r = env.curl_get(url)
assert r.exit_code != 0, f"{r}"
# produce an error, fail to generate an error bucket
def test_h2_003_72(self, env, repeat):
url = env.mkurl("https", "cgi", "/h2test/error?body_error=timeout&error_bucket=0")
r = env.curl_get(url)
assert r.exit_code != 0, f"{r}"
|