pytest_http2_request.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env python
  2. #
  3. # SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  4. # SPDX-License-Identifier: Apache-2.0
  5. import http.client
  6. import logging
  7. import os
  8. import pytest
  9. from pytest_embedded import Dut
  10. HTTP_OK = 200
  11. TEST_SERVER = 'http2.github.io'
  12. def is_test_server_available(): # type: () -> bool
  13. # 443 - default https port
  14. try:
  15. conn = http.client.HTTPSConnection(TEST_SERVER, 443, timeout=10)
  16. conn.request('GET', '/')
  17. resp = conn.getresponse()
  18. return True if resp.status == HTTP_OK else False
  19. except Exception as msg:
  20. logging.info('Exception occurred when connecting to {}: {}'.format(TEST_SERVER, msg))
  21. return False
  22. finally:
  23. conn.close()
  24. @pytest.mark.esp32
  25. @pytest.mark.esp32c3
  26. @pytest.mark.esp32s2
  27. @pytest.mark.esp32s3
  28. @pytest.mark.ethernet
  29. def test_examples_protocol_http2_request(dut: Dut) -> None:
  30. """
  31. steps: |
  32. 1. join AP
  33. 2. connect to http2.github.io
  34. 3. send http2 request
  35. 4. send http2 put response
  36. """
  37. # check and log bin size
  38. binary_file = os.path.join(dut.app.binary_path, 'http2_request.bin')
  39. bin_size = os.path.getsize(binary_file)
  40. logging.info('http2_request_bin_size : {}KB'.format(bin_size // 1024))
  41. # start the test
  42. # check if test server is avilable
  43. test_server_available = is_test_server_available()
  44. # Skip the test if the server test server (http2.github.io) is not available at the moment.
  45. if test_server_available:
  46. logging.info('test server \"{}\" is available'.format(TEST_SERVER))
  47. # check for connection
  48. dut.expect('Connection done', timeout=30)
  49. # check for get response
  50. dut.expect('Frame fully received')
  51. else:
  52. logging.info('test server \"{0}\" is not available at the moment.\nSkipping the test with status = success.'.format(TEST_SERVER))