xref: /aosp_15_r20/external/curl/tests/http/test_09_push.py (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker#!/usr/bin/env python3
2*6236dae4SAndroid Build Coastguard Worker# -*- coding: utf-8 -*-
3*6236dae4SAndroid Build Coastguard Worker#***************************************************************************
4*6236dae4SAndroid Build Coastguard Worker#                                  _   _ ____  _
5*6236dae4SAndroid Build Coastguard Worker#  Project                     ___| | | |  _ \| |
6*6236dae4SAndroid Build Coastguard Worker#                             / __| | | | |_) | |
7*6236dae4SAndroid Build Coastguard Worker#                            | (__| |_| |  _ <| |___
8*6236dae4SAndroid Build Coastguard Worker#                             \___|\___/|_| \_\_____|
9*6236dae4SAndroid Build Coastguard Worker#
10*6236dae4SAndroid Build Coastguard Worker# Copyright (C) Daniel Stenberg, <[email protected]>, et al.
11*6236dae4SAndroid Build Coastguard Worker#
12*6236dae4SAndroid Build Coastguard Worker# This software is licensed as described in the file COPYING, which
13*6236dae4SAndroid Build Coastguard Worker# you should have received as part of this distribution. The terms
14*6236dae4SAndroid Build Coastguard Worker# are also available at https://curl.se/docs/copyright.html.
15*6236dae4SAndroid Build Coastguard Worker#
16*6236dae4SAndroid Build Coastguard Worker# You may opt to use, copy, modify, merge, publish, distribute and/or sell
17*6236dae4SAndroid Build Coastguard Worker# copies of the Software, and permit persons to whom the Software is
18*6236dae4SAndroid Build Coastguard Worker# furnished to do so, under the terms of the COPYING file.
19*6236dae4SAndroid Build Coastguard Worker#
20*6236dae4SAndroid Build Coastguard Worker# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21*6236dae4SAndroid Build Coastguard Worker# KIND, either express or implied.
22*6236dae4SAndroid Build Coastguard Worker#
23*6236dae4SAndroid Build Coastguard Worker# SPDX-License-Identifier: curl
24*6236dae4SAndroid Build Coastguard Worker#
25*6236dae4SAndroid Build Coastguard Worker###########################################################################
26*6236dae4SAndroid Build Coastguard Worker#
27*6236dae4SAndroid Build Coastguard Workerimport logging
28*6236dae4SAndroid Build Coastguard Workerimport os
29*6236dae4SAndroid Build Coastguard Workerimport pytest
30*6236dae4SAndroid Build Coastguard Worker
31*6236dae4SAndroid Build Coastguard Workerfrom testenv import Env, CurlClient, LocalClient
32*6236dae4SAndroid Build Coastguard Worker
33*6236dae4SAndroid Build Coastguard Worker
34*6236dae4SAndroid Build Coastguard Workerlog = logging.getLogger(__name__)
35*6236dae4SAndroid Build Coastguard Worker
36*6236dae4SAndroid Build Coastguard Worker
37*6236dae4SAndroid Build Coastguard Workerclass TestPush:
38*6236dae4SAndroid Build Coastguard Worker
39*6236dae4SAndroid Build Coastguard Worker    @pytest.fixture(autouse=True, scope='class')
40*6236dae4SAndroid Build Coastguard Worker    def _class_scope(self, env, httpd):
41*6236dae4SAndroid Build Coastguard Worker        push_dir = os.path.join(httpd.docs_dir, 'push')
42*6236dae4SAndroid Build Coastguard Worker        if not os.path.exists(push_dir):
43*6236dae4SAndroid Build Coastguard Worker            os.makedirs(push_dir)
44*6236dae4SAndroid Build Coastguard Worker        env.make_data_file(indir=push_dir, fname="data1", fsize=1*1024)
45*6236dae4SAndroid Build Coastguard Worker        env.make_data_file(indir=push_dir, fname="data2", fsize=1*1024)
46*6236dae4SAndroid Build Coastguard Worker        env.make_data_file(indir=push_dir, fname="data3", fsize=1*1024)
47*6236dae4SAndroid Build Coastguard Worker        httpd.set_extra_config(env.domain1, [
48*6236dae4SAndroid Build Coastguard Worker            'H2EarlyHints on',
49*6236dae4SAndroid Build Coastguard Worker            '<Location /push/data1>',
50*6236dae4SAndroid Build Coastguard Worker            '  H2PushResource /push/data2',
51*6236dae4SAndroid Build Coastguard Worker            '</Location>',
52*6236dae4SAndroid Build Coastguard Worker            '<Location /push/data2>',
53*6236dae4SAndroid Build Coastguard Worker            '  H2PushResource /push/data1',
54*6236dae4SAndroid Build Coastguard Worker            '  H2PushResource /push/data3',
55*6236dae4SAndroid Build Coastguard Worker            '</Location>',
56*6236dae4SAndroid Build Coastguard Worker        ])
57*6236dae4SAndroid Build Coastguard Worker        # activate the new config
58*6236dae4SAndroid Build Coastguard Worker        httpd.reload()
59*6236dae4SAndroid Build Coastguard Worker        yield
60*6236dae4SAndroid Build Coastguard Worker        httpd.clear_extra_configs()
61*6236dae4SAndroid Build Coastguard Worker        httpd.reload()
62*6236dae4SAndroid Build Coastguard Worker
63*6236dae4SAndroid Build Coastguard Worker    # download a file that triggers a "103 Early Hints" response
64*6236dae4SAndroid Build Coastguard Worker    def test_09_01_h2_early_hints(self, env: Env, httpd, repeat):
65*6236dae4SAndroid Build Coastguard Worker        curl = CurlClient(env=env)
66*6236dae4SAndroid Build Coastguard Worker        url = f'https://{env.domain1}:{env.https_port}/push/data1'
67*6236dae4SAndroid Build Coastguard Worker        r = curl.http_download(urls=[url], alpn_proto='h2', with_stats=False,
68*6236dae4SAndroid Build Coastguard Worker                               with_headers=True)
69*6236dae4SAndroid Build Coastguard Worker        r.check_exit_code(0)
70*6236dae4SAndroid Build Coastguard Worker        assert len(r.responses) == 2, f'{r.responses}'
71*6236dae4SAndroid Build Coastguard Worker        assert r.responses[0]['status'] == 103, f'{r.responses}'
72*6236dae4SAndroid Build Coastguard Worker        assert 'link' in r.responses[0]['header'], f'{r.responses[0]}'
73*6236dae4SAndroid Build Coastguard Worker        assert r.responses[0]['header']['link'] == '</push/data2>; rel=preload', f'{r.responses[0]}'
74*6236dae4SAndroid Build Coastguard Worker
75*6236dae4SAndroid Build Coastguard Worker    def test_09_02_h2_push(self, env: Env, httpd, repeat):
76*6236dae4SAndroid Build Coastguard Worker        # use localhost as we do not have resolve support in local client
77*6236dae4SAndroid Build Coastguard Worker        url = f'https://localhost:{env.https_port}/push/data1'
78*6236dae4SAndroid Build Coastguard Worker        client = LocalClient(name='h2-serverpush', env=env)
79*6236dae4SAndroid Build Coastguard Worker        if not client.exists():
80*6236dae4SAndroid Build Coastguard Worker            pytest.skip(f'example client not built: {client.name}')
81*6236dae4SAndroid Build Coastguard Worker        r = client.run(args=[url])
82*6236dae4SAndroid Build Coastguard Worker        r.check_exit_code(0)
83*6236dae4SAndroid Build Coastguard Worker        assert os.path.exists(client.download_file(0))
84*6236dae4SAndroid Build Coastguard Worker        assert os.path.exists(os.path.join(client.run_dir, 'push0')), r.dump_logs()
85