test_01_basic.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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=f"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=f"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=f"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=f"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. assert r.stats[0]['time_connect'] > 0, f'{r.stats[0]}'
  87. assert r.stats[0]['time_appconnect'] > 0, f'{r.stats[0]}'