xref: /curl/tests/http/test_12_reuse.py (revision 5126cbda)
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3#***************************************************************************
4#                                  _   _ ____  _
5#  Project                     ___| | | |  _ \| |
6#                             / __| | | | |_) | |
7#                            | (__| |_| |  _ <| |___
8#                             \___|\___/|_| \_\_____|
9#
10# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
11#
12# This software is licensed as described in the file COPYING, which
13# you should have received as part of this distribution. The terms
14# are also available at https://curl.se/docs/copyright.html.
15#
16# You may opt to use, copy, modify, merge, publish, distribute and/or sell
17# copies of the Software, and permit persons to whom the Software is
18# furnished to do so, under the terms of the COPYING file.
19#
20# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21# KIND, either express or implied.
22#
23# SPDX-License-Identifier: curl
24#
25###########################################################################
26#
27import difflib
28import filecmp
29import logging
30import os
31import pytest
32
33from testenv import Env, CurlClient
34
35
36log = logging.getLogger(__name__)
37
38
39@pytest.mark.skipif(condition=Env.curl_uses_lib('bearssl'), reason='BearSSL too slow')
40@pytest.mark.skipif(condition=not Env.have_ssl_curl(), reason=f"curl without SSL")
41class TestReuse:
42
43    # check if HTTP/1.1 handles 'Connection: close' correctly
44    @pytest.mark.parametrize("proto", ['http/1.1'])
45    def test_12_01_h1_conn_close(self, env: Env,
46                                 httpd, nghttpx, repeat, proto):
47        httpd.clear_extra_configs()
48        httpd.set_extra_config('base', [
49            f'MaxKeepAliveRequests 1',
50        ])
51        httpd.reload()
52        count = 100
53        curl = CurlClient(env=env)
54        urln = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{count-1}]'
55        r = curl.http_download(urls=[urln], alpn_proto=proto)
56        r.check_response(count=count, http_status=200)
57        # Server sends `Connection: close` on every 2nd request, requiring
58        # a new connection
59        delta = 5
60        assert (count/2 - delta) < r.total_connects < (count/2 + delta)
61
62    @pytest.mark.parametrize("proto", ['http/1.1'])
63    def test_12_02_h1_conn_timeout(self, env: Env,
64                                   httpd, nghttpx, repeat, proto):
65        httpd.clear_extra_configs()
66        httpd.set_extra_config('base', [
67            f'KeepAliveTimeout 1',
68        ])
69        httpd.reload()
70        count = 5
71        curl = CurlClient(env=env)
72        urln = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{count-1}]'
73        r = curl.http_download(urls=[urln], alpn_proto=proto, extra_args=[
74            '--rate', '30/m',
75        ])
76        r.check_response(count=count, http_status=200)
77        # Connections time out on server before we send another request,
78        assert r.total_connects == count
79