test_serving_files.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from pathlib import Path
  2. import httpx
  3. import pytest
  4. import requests
  5. from nicegui import __version__, app, ui
  6. from nicegui.testing import Screen
  7. from .test_helpers import TEST_DIR
  8. IMAGE_FILE = Path(TEST_DIR).parent / 'examples' / 'slideshow' / 'slides' / 'slide1.jpg'
  9. VIDEO_FILE = Path(TEST_DIR) / 'media' / 'test.mp4'
  10. @pytest.fixture(autouse=True)
  11. def provide_media_files():
  12. if not VIDEO_FILE.exists():
  13. VIDEO_FILE.parent.mkdir(exist_ok=True)
  14. url = 'https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/360/Big_Buck_Bunny_360_10s_1MB.mp4'
  15. with httpx.stream('GET', url) as response:
  16. with open(VIDEO_FILE, 'wb') as file:
  17. for chunk in response.iter_raw():
  18. file.write(chunk)
  19. def assert_video_file_streaming(path: str) -> None:
  20. with httpx.Client() as http_client:
  21. r = http_client.get(
  22. path if 'http' in path else f'http://localhost:{Screen.PORT}{path}',
  23. headers={'Range': 'bytes=0-1000'},
  24. timeout=1,
  25. )
  26. assert r.status_code == 206
  27. assert r.headers['Accept-Ranges'] == 'bytes'
  28. assert r.headers['Content-Range'].startswith('bytes 0-1000/')
  29. assert r.headers['Content-Length'] == '1001'
  30. assert r.headers['Content-Type'] == 'video/mp4'
  31. def test_media_files_can_be_streamed(screen: Screen):
  32. app.add_media_files('/media', Path(TEST_DIR) / 'media')
  33. screen.open('/')
  34. assert_video_file_streaming('/media/test.mp4')
  35. def test_adding_single_media_file(screen: Screen):
  36. url_path = app.add_media_file(local_file=VIDEO_FILE)
  37. screen.open('/')
  38. assert_video_file_streaming(url_path)
  39. def test_adding_single_static_file(screen: Screen):
  40. url_path = app.add_static_file(local_file=IMAGE_FILE)
  41. screen.open('/')
  42. with httpx.Client() as http_client:
  43. r = http_client.get(f'http://localhost:{Screen.PORT}{url_path}')
  44. assert r.status_code == 200
  45. assert 'max-age=' in r.headers['Cache-Control']
  46. def test_auto_serving_file_from_image_source(screen: Screen):
  47. ui.image(IMAGE_FILE)
  48. screen.open('/')
  49. img = screen.find_by_tag('img')
  50. assert '/_nicegui/auto/static/' in img.get_attribute('src')
  51. assert screen.selenium.execute_script("""
  52. return arguments[0].complete &&
  53. typeof arguments[0].naturalWidth != "undefined" &&
  54. arguments[0].naturalWidth > 0
  55. """, img), 'image should load successfully'
  56. def test_auto_serving_file_from_video_source(screen: Screen):
  57. ui.video(VIDEO_FILE)
  58. screen.open('/')
  59. video = screen.find_by_tag('video')
  60. assert '/_nicegui/auto/media/' in video.get_attribute('src')
  61. screen.wait(0.5)
  62. assert_video_file_streaming(video.get_attribute('src'))
  63. def test_mimetypes_of_static_files(screen: Screen):
  64. screen.open('/')
  65. response = requests.get(f'http://localhost:{Screen.PORT}/_nicegui/{__version__}/static/vue.global.js', timeout=5)
  66. assert response.status_code == 200
  67. assert response.headers['Content-Type'].startswith('text/javascript')
  68. response = requests.get(f'http://localhost:{Screen.PORT}/_nicegui/{__version__}/static/nicegui.css', timeout=5)
  69. assert response.status_code == 200
  70. assert response.headers['Content-Type'].startswith('text/css')