test_01_basic.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 pytest
  29. from testenv import Env
  30. from testenv import CurlClient
  31. log = logging.getLogger(__name__)
  32. class TestBasic:
  33. @pytest.fixture(autouse=True, scope='class')
  34. def _class_scope(self, env, nghttpx):
  35. if env.have_h3():
  36. nghttpx.start_if_needed()
  37. # simple http: GET
  38. def test_01_01_http_get(self, env: Env, httpd):
  39. curl = CurlClient(env=env)
  40. url = f'http://{env.domain1}:{env.http_port}/data.json'
  41. r = curl.http_get(url=url)
  42. r.check_response(http_status=200)
  43. assert r.json['server'] == env.domain1
  44. # simple https: GET, any http version
  45. @pytest.mark.skipif(condition=not Env.have_ssl_curl(), reason="curl without SSL")
  46. def test_01_02_https_get(self, env: Env, httpd):
  47. curl = CurlClient(env=env)
  48. url = f'https://{env.domain1}:{env.https_port}/data.json'
  49. r = curl.http_get(url=url)
  50. r.check_response(http_status=200)
  51. assert r.json['server'] == env.domain1
  52. # simple https: GET, h2 wanted and got
  53. @pytest.mark.skipif(condition=not Env.have_ssl_curl(), reason="curl without SSL")
  54. def test_01_03_h2_get(self, env: Env, httpd):
  55. curl = CurlClient(env=env)
  56. url = f'https://{env.domain1}:{env.https_port}/data.json'
  57. r = curl.http_get(url=url, extra_args=['--http2'])
  58. r.check_response(http_status=200, protocol='HTTP/2')
  59. assert r.json['server'] == env.domain1
  60. # simple https: GET, h2 unsupported, fallback to h1
  61. @pytest.mark.skipif(condition=not Env.have_ssl_curl(), reason="curl without SSL")
  62. def test_01_04_h2_unsupported(self, env: Env, httpd):
  63. curl = CurlClient(env=env)
  64. url = f'https://{env.domain2}:{env.https_port}/data.json'
  65. r = curl.http_get(url=url, extra_args=['--http2'])
  66. r.check_response(http_status=200, protocol='HTTP/1.1')
  67. assert r.json['server'] == env.domain2
  68. # simple h3: GET, want h3 and get it
  69. @pytest.mark.skipif(condition=not Env.have_h3(), reason="h3 not supported")
  70. def test_01_05_h3_get(self, env: Env, httpd, nghttpx):
  71. curl = CurlClient(env=env)
  72. url = f'https://{env.domain1}:{env.h3_port}/data.json'
  73. r = curl.http_get(url=url, extra_args=['--http3-only'])
  74. r.check_response(http_status=200, protocol='HTTP/3')
  75. assert r.json['server'] == env.domain1
  76. # simple download, check connect/handshake timings
  77. @pytest.mark.skipif(condition=not Env.have_ssl_curl(), reason="curl without SSL")
  78. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  79. def test_01_06_timings(self, env: Env, httpd, nghttpx, repeat, proto):
  80. if proto == 'h3' and not env.have_h3():
  81. pytest.skip("h3 not supported")
  82. curl = CurlClient(env=env)
  83. url = f'https://{env.authority_for(env.domain1, proto)}/data.json'
  84. r = curl.http_download(urls=[url], alpn_proto=proto, with_stats=True)
  85. r.check_stats(http_status=200, count=1,
  86. remote_port=env.port_for(alpn_proto=proto),
  87. remote_ip='127.0.0.1')
  88. assert r.stats[0]['time_connect'] > 0, f'{r.stats[0]}'
  89. assert r.stats[0]['time_appconnect'] > 0, f'{r.stats[0]}'
  90. # simple https: HEAD
  91. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  92. @pytest.mark.skipif(condition=not Env.have_ssl_curl(), reason="curl without SSL")
  93. def test_01_07_head(self, env: Env, httpd, nghttpx, repeat, proto):
  94. if proto == 'h3' and not env.have_h3():
  95. pytest.skip("h3 not supported")
  96. curl = CurlClient(env=env)
  97. url = f'https://{env.authority_for(env.domain1, proto)}/data.json'
  98. r = curl.http_download(urls=[url], with_stats=True, with_headers=True,
  99. extra_args=['-I'])
  100. r.check_stats(http_status=200, count=1, exitcode=0,
  101. remote_port=env.port_for(alpn_proto=proto),
  102. remote_ip='127.0.0.1')
  103. # got the Conten-Length: header, but did not download anything
  104. assert r.responses[0]['header']['content-length'] == '30', f'{r.responses[0]}'
  105. assert r.stats[0]['size_download'] == 0, f'{r.stats[0]}'
  106. # http: GET for HTTP/2, see Upgrade:, 101 switch
  107. def test_01_08_h2_upgrade(self, env: Env, httpd):
  108. curl = CurlClient(env=env)
  109. url = f'http://{env.domain1}:{env.http_port}/data.json'
  110. r = curl.http_get(url=url, extra_args=['--http2'])
  111. r.check_exit_code(0)
  112. assert len(r.responses) == 2, f'{r.responses}'
  113. assert r.responses[0]['status'] == 101, f'{r.responses[0]}'
  114. assert r.responses[1]['status'] == 200, f'{r.responses[1]}'
  115. assert r.responses[1]['protocol'] == 'HTTP/2', f'{r.responses[1]}'
  116. assert r.json['server'] == env.domain1
  117. # http: GET for HTTP/2 with prior knowledge
  118. def test_01_09_h2_prior_knowledge(self, env: Env, httpd):
  119. curl = CurlClient(env=env)
  120. url = f'http://{env.domain1}:{env.http_port}/data.json'
  121. r = curl.http_get(url=url, extra_args=['--http2-prior-knowledge'])
  122. r.check_exit_code(0)
  123. assert len(r.responses) == 1, f'{r.responses}'
  124. assert r.response['status'] == 200, f'{r.responsw}'
  125. assert r.response['protocol'] == 'HTTP/2', f'{r.response}'
  126. assert r.json['server'] == env.domain1