2023-02-09 20:08:26 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#***************************************************************************
|
|
|
|
# _ _ ____ _
|
|
|
|
# Project ___| | | | _ \| |
|
|
|
|
# / __| | | | |_) | |
|
|
|
|
# | (__| |_| | _ <| |___
|
|
|
|
# \___|\___/|_| \_\_____|
|
|
|
|
#
|
2023-03-14 23:17:46 +08:00
|
|
|
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2023-02-09 20:08:26 +08:00
|
|
|
#
|
|
|
|
# This software is licensed as described in the file COPYING, which
|
|
|
|
# you should have received as part of this distribution. The terms
|
|
|
|
# are also available at https://curl.se/docs/copyright.html.
|
|
|
|
#
|
|
|
|
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
# copies of the Software, and permit persons to whom the Software is
|
|
|
|
# furnished to do so, under the terms of the COPYING file.
|
|
|
|
#
|
|
|
|
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
# KIND, either express or implied.
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: curl
|
|
|
|
#
|
|
|
|
###########################################################################
|
|
|
|
#
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import pytest
|
|
|
|
|
2023-04-26 18:38:22 +08:00
|
|
|
from testenv import Env, CurlClient, LocalClient
|
2023-02-09 20:08:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class TestPush:
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True, scope='class')
|
|
|
|
def _class_scope(self, env, httpd):
|
|
|
|
push_dir = os.path.join(httpd.docs_dir, 'push')
|
|
|
|
if not os.path.exists(push_dir):
|
|
|
|
os.makedirs(push_dir)
|
2023-04-26 18:38:22 +08:00
|
|
|
env.make_data_file(indir=push_dir, fname="data1", fsize=1*1024)
|
|
|
|
env.make_data_file(indir=push_dir, fname="data2", fsize=1*1024)
|
|
|
|
env.make_data_file(indir=push_dir, fname="data3", fsize=1*1024)
|
2023-02-09 20:08:26 +08:00
|
|
|
httpd.set_extra_config(env.domain1, [
|
|
|
|
f'H2EarlyHints on',
|
|
|
|
f'<Location /push/data1>',
|
|
|
|
f' H2PushResource /push/data2',
|
|
|
|
f'</Location>',
|
|
|
|
f'<Location /push/data2>',
|
|
|
|
f' H2PushResource /push/data1',
|
|
|
|
f' H2PushResource /push/data3',
|
|
|
|
f'</Location>',
|
|
|
|
])
|
|
|
|
# activate the new config
|
|
|
|
httpd.reload()
|
2023-03-09 17:50:55 +08:00
|
|
|
yield
|
|
|
|
httpd.clear_extra_configs()
|
|
|
|
httpd.reload()
|
2023-02-09 20:08:26 +08:00
|
|
|
|
|
|
|
# download a file that triggers a "103 Early Hints" response
|
2023-04-26 18:38:22 +08:00
|
|
|
def test_09_01_h2_early_hints(self, env: Env, httpd, repeat):
|
2023-02-09 20:08:26 +08:00
|
|
|
curl = CurlClient(env=env)
|
|
|
|
url = f'https://{env.domain1}:{env.https_port}/push/data1'
|
|
|
|
r = curl.http_download(urls=[url], alpn_proto='h2', with_stats=False,
|
|
|
|
with_headers=True)
|
2023-03-24 20:09:40 +08:00
|
|
|
r.check_exit_code(0)
|
2023-02-09 20:08:26 +08:00
|
|
|
assert len(r.responses) == 2, f'{r.responses}'
|
|
|
|
assert r.responses[0]['status'] == 103, f'{r.responses}'
|
|
|
|
assert 'link' in r.responses[0]['header'], f'{r.responses[0]}'
|
|
|
|
assert r.responses[0]['header']['link'] == '</push/data2>; rel=preload', f'{r.responses[0]}'
|
2023-04-26 18:38:22 +08:00
|
|
|
|
|
|
|
def test_09_02_h2_push(self, env: Env, httpd, repeat):
|
|
|
|
# use localhost as we do not have resolve support in local client
|
|
|
|
url = f'https://localhost:{env.https_port}/push/data1'
|
|
|
|
client = LocalClient(name='h2-serverpush', env=env)
|
|
|
|
if not client.exists():
|
|
|
|
pytest.skip(f'example client not built: {client.name}')
|
|
|
|
r = client.run(args=[url])
|
|
|
|
r.check_exit_code(0)
|
|
|
|
assert os.path.exists(client.download_file(0))
|
|
|
|
assert os.path.exists(os.path.join(client.run_dir, 'push0')), r.dump_logs()
|