test_09_push.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. import pytest
  30. from testenv import Env, CurlClient, LocalClient
  31. log = logging.getLogger(__name__)
  32. class TestPush:
  33. @pytest.fixture(autouse=True, scope='class')
  34. def _class_scope(self, env, httpd):
  35. push_dir = os.path.join(httpd.docs_dir, 'push')
  36. if not os.path.exists(push_dir):
  37. os.makedirs(push_dir)
  38. env.make_data_file(indir=push_dir, fname="data1", fsize=1*1024)
  39. env.make_data_file(indir=push_dir, fname="data2", fsize=1*1024)
  40. env.make_data_file(indir=push_dir, fname="data3", fsize=1*1024)
  41. httpd.set_extra_config(env.domain1, [
  42. f'H2EarlyHints on',
  43. f'<Location /push/data1>',
  44. f' H2PushResource /push/data2',
  45. f'</Location>',
  46. f'<Location /push/data2>',
  47. f' H2PushResource /push/data1',
  48. f' H2PushResource /push/data3',
  49. f'</Location>',
  50. ])
  51. # activate the new config
  52. httpd.reload()
  53. yield
  54. httpd.clear_extra_configs()
  55. httpd.reload()
  56. # download a file that triggers a "103 Early Hints" response
  57. def test_09_01_h2_early_hints(self, env: Env, httpd, repeat):
  58. curl = CurlClient(env=env)
  59. url = f'https://{env.domain1}:{env.https_port}/push/data1'
  60. r = curl.http_download(urls=[url], alpn_proto='h2', with_stats=False,
  61. with_headers=True)
  62. r.check_exit_code(0)
  63. assert len(r.responses) == 2, f'{r.responses}'
  64. assert r.responses[0]['status'] == 103, f'{r.responses}'
  65. assert 'link' in r.responses[0]['header'], f'{r.responses[0]}'
  66. assert r.responses[0]['header']['link'] == '</push/data2>; rel=preload', f'{r.responses[0]}'
  67. def test_09_02_h2_push(self, env: Env, httpd, repeat):
  68. # use localhost as we do not have resolve support in local client
  69. url = f'https://localhost:{env.https_port}/push/data1'
  70. client = LocalClient(name='h2-serverpush', env=env)
  71. if not client.exists():
  72. pytest.skip(f'example client not built: {client.name}')
  73. r = client.run(args=[url])
  74. r.check_exit_code(0)
  75. assert os.path.exists(client.download_file(0))
  76. assert os.path.exists(os.path.join(client.run_dir, 'push0')), r.dump_logs()