test_12_reuse.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #
  27. import difflib
  28. import filecmp
  29. import logging
  30. import os
  31. from datetime import datetime, timedelta
  32. import pytest
  33. from testenv import Env, CurlClient
  34. log = logging.getLogger(__name__)
  35. @pytest.mark.skipif(condition=Env.curl_uses_lib('bearssl'), reason='BearSSL too slow')
  36. @pytest.mark.skipif(condition=not Env.have_ssl_curl(), reason=f"curl without SSL")
  37. class TestReuse:
  38. # check if HTTP/1.1 handles 'Connection: close' correctly
  39. @pytest.mark.parametrize("proto", ['http/1.1'])
  40. def test_12_01_h1_conn_close(self, env: Env,
  41. httpd, nghttpx, repeat, proto):
  42. httpd.clear_extra_configs()
  43. httpd.set_extra_config('base', [
  44. f'MaxKeepAliveRequests 1',
  45. ])
  46. httpd.reload()
  47. count = 100
  48. curl = CurlClient(env=env)
  49. urln = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{count-1}]'
  50. r = curl.http_download(urls=[urln], alpn_proto=proto)
  51. r.check_response(count=count, http_status=200)
  52. # Server sends `Connection: close` on every 2nd request, requiring
  53. # a new connection
  54. delta = 5
  55. assert (count/2 - delta) < r.total_connects < (count/2 + delta)
  56. @pytest.mark.skipif(condition=Env.httpd_is_at_least('2.5.0'),
  57. reason=f"httpd 2.5+ handles KeepAlives different")
  58. @pytest.mark.parametrize("proto", ['http/1.1'])
  59. def test_12_02_h1_conn_timeout(self, env: Env,
  60. httpd, nghttpx, repeat, proto):
  61. httpd.clear_extra_configs()
  62. httpd.set_extra_config('base', [
  63. f'KeepAliveTimeout 1',
  64. ])
  65. httpd.reload()
  66. count = 5
  67. curl = CurlClient(env=env)
  68. urln = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{count-1}]'
  69. r = curl.http_download(urls=[urln], alpn_proto=proto, extra_args=[
  70. '--rate', '30/m',
  71. ])
  72. r.check_response(count=count, http_status=200)
  73. # Connections time out on server before we send another request,
  74. assert r.total_connects == count
  75. @pytest.mark.skipif(condition=not Env.have_h3(), reason="h3 not supported")
  76. def test_12_03_alt_svc_h2h3(self, env: Env, httpd, nghttpx):
  77. httpd.clear_extra_configs()
  78. httpd.reload()
  79. count = 2
  80. # write a alt-svc file the advises h3 instead of h2
  81. asfile = os.path.join(env.gen_dir, 'alt-svc-12_03.txt')
  82. ts = datetime.now() + timedelta(hours=24)
  83. expires = f'{ts.year:04}{ts.month:02}{ts.day:02} {ts.hour:02}:{ts.minute:02}:{ts.second:02}'
  84. with open(asfile, 'w') as fd:
  85. fd.write(f'h2 {env.domain1} {env.https_port} h3 {env.domain1} {env.https_port} "{expires}" 0 0')
  86. log.info(f'altscv: {open(asfile).readlines()}')
  87. curl = CurlClient(env=env)
  88. urln = f'https://{env.authority_for(env.domain1, "h2")}/data.json?[0-{count-1}]'
  89. r = curl.http_download(urls=[urln], with_stats=True, extra_args=[
  90. '--alt-svc', f'{asfile}',
  91. ])
  92. r.check_response(count=count, http_status=200)
  93. # We expect the connection to be reused
  94. assert r.total_connects == 1
  95. for s in r.stats:
  96. assert s['http_version'] == '3', f'{s}'
  97. def test_12_04_alt_svc_h3h2(self, env: Env, httpd, nghttpx):
  98. httpd.clear_extra_configs()
  99. httpd.reload()
  100. count = 2
  101. # write a alt-svc file the advises h2 instead of h3
  102. asfile = os.path.join(env.gen_dir, 'alt-svc-12_04.txt')
  103. ts = datetime.now() + timedelta(hours=24)
  104. expires = f'{ts.year:04}{ts.month:02}{ts.day:02} {ts.hour:02}:{ts.minute:02}:{ts.second:02}'
  105. with open(asfile, 'w') as fd:
  106. fd.write(f'h3 {env.domain1} {env.https_port} h2 {env.domain1} {env.https_port} "{expires}" 0 0')
  107. log.info(f'altscv: {open(asfile).readlines()}')
  108. curl = CurlClient(env=env)
  109. urln = f'https://{env.authority_for(env.domain1, "h2")}/data.json?[0-{count-1}]'
  110. r = curl.http_download(urls=[urln], with_stats=True, extra_args=[
  111. '--alt-svc', f'{asfile}',
  112. ])
  113. r.check_response(count=count, http_status=200)
  114. # We expect the connection to be reused
  115. assert r.total_connects == 1
  116. for s in r.stats:
  117. assert s['http_version'] == '2', f'{s}'
  118. def test_12_05_alt_svc_h3h1(self, env: Env, httpd, nghttpx):
  119. httpd.clear_extra_configs()
  120. httpd.reload()
  121. count = 2
  122. # write a alt-svc file the advises h1 instead of h3
  123. asfile = os.path.join(env.gen_dir, 'alt-svc-12_05.txt')
  124. ts = datetime.now() + timedelta(hours=24)
  125. expires = f'{ts.year:04}{ts.month:02}{ts.day:02} {ts.hour:02}:{ts.minute:02}:{ts.second:02}'
  126. with open(asfile, 'w') as fd:
  127. fd.write(f'h3 {env.domain1} {env.https_port} http/1.1 {env.domain1} {env.https_port} "{expires}" 0 0')
  128. log.info(f'altscv: {open(asfile).readlines()}')
  129. curl = CurlClient(env=env)
  130. urln = f'https://{env.authority_for(env.domain1, "h2")}/data.json?[0-{count-1}]'
  131. r = curl.http_download(urls=[urln], with_stats=True, extra_args=[
  132. '--alt-svc', f'{asfile}',
  133. ])
  134. r.check_response(count=count, http_status=200)
  135. # We expect the connection to be reused
  136. assert r.total_connects == 1
  137. # When using http/1.1 from alt-svc, we ALPN-negotiate 'h2,http/1.1' anyway
  138. # which means our server gives us h2
  139. for s in r.stats:
  140. assert s['http_version'] == '2', f'{s}'