test_helpers.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import contextlib
  2. import socket
  3. import time
  4. import webbrowser
  5. from pathlib import Path
  6. from nicegui import helpers
  7. TEST_DIR = Path(__file__).parent
  8. DOWNLOAD_DIR = TEST_DIR / 'download'
  9. def test_is_port_open():
  10. with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
  11. sock.bind(('127.0.0.1', 0)) # port = 0 => the OS chooses a port for us
  12. sock.listen(1)
  13. host, port = sock.getsockname()
  14. assert not helpers.is_port_open(host, port), 'after closing the socket, the port should be free'
  15. with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
  16. sock.bind(('127.0.0.1', port))
  17. sock.listen(1)
  18. assert helpers.is_port_open(host, port), 'after opening the socket, the port should be detected'
  19. def test_is_port_open_on_bad_ip():
  20. assert not helpers.is_port_open('1.2', 0), 'should not be able to connect to a bad IP'
  21. def test_schedule_browser(monkeypatch):
  22. called_with_url = None
  23. def mock_webbrowser_open(url):
  24. nonlocal called_with_url
  25. called_with_url = url
  26. monkeypatch.setattr(webbrowser, 'open', mock_webbrowser_open)
  27. with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
  28. sock.bind(('127.0.0.1', 0))
  29. host, port = sock.getsockname()
  30. thread, cancel_event = helpers.schedule_browser(host, port)
  31. try:
  32. # port bound, but not opened yet
  33. assert called_with_url is None
  34. sock.listen()
  35. # port opened
  36. time.sleep(1)
  37. assert called_with_url == f'http://{host}:{port}/'
  38. finally:
  39. cancel_event.set()
  40. def test_canceling_schedule_browser(monkeypatch):
  41. called_with_url = None
  42. def mock_webbrowser_open(url):
  43. nonlocal called_with_url
  44. called_with_url = url
  45. monkeypatch.setattr(webbrowser, 'open', mock_webbrowser_open)
  46. # find a free port ...
  47. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  48. sock.bind(('127.0.0.1', 0))
  49. sock.listen(1)
  50. host, port = sock.getsockname()
  51. # ... and close it so schedule_browser does not launch the browser
  52. sock.close()
  53. thread, cancel_event = helpers.schedule_browser(host, port)
  54. time.sleep(0.2)
  55. cancel_event.set()
  56. time.sleep(0.2)
  57. assert not thread.is_alive()
  58. assert called_with_url is None
  59. def test_is_file():
  60. assert helpers.is_file(TEST_DIR / 'test_helpers.py')
  61. assert helpers.is_file(str(TEST_DIR / 'test_helpers.py'))
  62. assert not helpers.is_file(TEST_DIR / 'nonexistent_file')
  63. assert not helpers.is_file(str(TEST_DIR / 'nonexistent_file'))
  64. assert not helpers.is_file('data:image/png;base64,...')
  65. assert not helpers.is_file(None)
  66. assert not helpers.is_file('x' * 100_000), 'a very long filepath should not lead to OSError 63'
  67. assert not helpers.is_file('http://nicegui.io/logo.png')