pytest_http2_request.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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.ethernet
  26. def test_examples_protocol_http2_request(dut: Dut) -> None:
  27. """
  28. steps: |
  29. 1. join AP
  30. 2. connect to http2.github.io
  31. 3. send http2 request
  32. 4. send http2 put response
  33. """
  34. # check and log bin size
  35. binary_file = os.path.join(dut.app.binary_path, 'http2_request.bin')
  36. bin_size = os.path.getsize(binary_file)
  37. logging.info('http2_request_bin_size : {}KB'.format(bin_size // 1024))
  38. # start the test
  39. # check if test server is avilable
  40. test_server_available = is_test_server_available()
  41. # Skip the test if the server test server (http2.github.io) is not available at the moment.
  42. if test_server_available:
  43. logging.info('test server \"{}\" is available'.format(TEST_SERVER))
  44. # check for connection
  45. dut.expect('Connection done', timeout=30)
  46. # check for get response
  47. dut.expect('Frame fully received')
  48. else:
  49. logging.info('test server \"{0}\" is not available at the moment.\nSkipping the test with status = success.'.format(TEST_SERVER))