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.setup_incomplete(), 40 reason=f"missing: {Env.incomplete_reason()}") 41@pytest.mark.skipif(condition=Env.curl_uses_lib('bearssl'), reason='BearSSL too slow') 42class TestReuse: 43 44 # check if HTTP/1.1 handles 'Connection: close' correctly 45 @pytest.mark.parametrize("proto", ['http/1.1']) 46 def test_12_01_h1_conn_close(self, env: Env, 47 httpd, nghttpx, repeat, proto): 48 httpd.clear_extra_configs() 49 httpd.set_extra_config('base', [ 50 f'MaxKeepAliveRequests 1', 51 ]) 52 httpd.reload() 53 count = 100 54 curl = CurlClient(env=env) 55 urln = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{count-1}]' 56 r = curl.http_download(urls=[urln], alpn_proto=proto) 57 assert r.exit_code == 0 58 r.check_stats(count=count, exp_status=200) 59 # Server sends `Connection: close` on every 2nd request, requiring 60 # a new connection 61 assert r.total_connects == count/2 62 63 @pytest.mark.parametrize("proto", ['http/1.1']) 64 def test_12_02_h1_conn_timeout(self, env: Env, 65 httpd, nghttpx, repeat, proto): 66 httpd.clear_extra_configs() 67 httpd.set_extra_config('base', [ 68 f'KeepAliveTimeout 1', 69 ]) 70 httpd.reload() 71 count = 5 72 curl = CurlClient(env=env) 73 urln = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{count-1}]' 74 r = curl.http_download(urls=[urln], alpn_proto=proto, extra_args=[ 75 '--rate', '30/m', 76 ]) 77 assert r.exit_code == 0 78 r.check_stats(count=count, exp_status=200) 79 # Connections time out on server before we send another request, 80 assert r.total_connects == count 81 # we do not see how often a request was retried in the stats, so 82 # we cannot check that connection reuse attempted a connection that 83 # was later detected to be "dead". We would like to 84 # assert stat['retry_count'] == 0 85