test_12_reuse.py 6.4 KB

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