def test_video(): """Ensure that it's possible to output videos with hug""" gif_path = os.path.join(BASE_DIRECTORY, 'artwork', 'example.gif') assert hasattr(hug.output_format.mp4_video(gif_path, hug.Response()), 'read') with open(gif_path, 'rb') as image_file: assert hasattr(hug.output_format.mp4_video(image_file, hug.Response()), 'read') assert hug.output_format.mp4_video('Not Existent', hug.Response()) is None class FakeVideoWithSave(): def save(self, to, format): to.write(b'test') assert hasattr( hug.output_format.mp4_video(FakeVideoWithSave(), hug.Response()), 'read') class FakeVideoWithSave(): def render(self): return 'test' assert hug.output_format.avi_video(FakeVideoWithSave(), hug.Response()) == 'test'
def test_on_valid(): """Test to ensure formats that use on_valid content types gracefully handle error dictionaries""" error_dict = {'errors': {'so': 'many'}} expected = hug.output_format.json(error_dict) assert hug.output_format.mp4_video(error_dict, hug.Response()) == expected assert hug.output_format.png_image(error_dict, hug.Response()) == expected @hug.output_format.on_valid('image', hug.output_format.file) def my_output_format(data): raise ValueError('This should never be called') assert my_output_format(error_dict, hug.Response())
def test_video(): """Ensure that it's possible to output videos with hug""" gif_path = os.path.join(BASE_DIRECTORY, "artwork", "example.gif") assert hasattr(hug.output_format.mp4_video(gif_path, hug.Response()), "read") with open(gif_path, "rb") as image_file: assert hasattr(hug.output_format.mp4_video(image_file, hug.Response()), "read") assert hug.output_format.mp4_video("Not Existent", hug.Response()) is None class FakeVideoWithSave: def save(self, to, format): to.write(b"test") assert hasattr(hug.output_format.mp4_video(FakeVideoWithSave(), hug.Response()), "read") class FakeVideoWithSave: def render(self): return "test" assert hug.output_format.avi_video(FakeVideoWithSave(), hug.Response()) == "test"
def test_image(): """Ensure that it's possible to output images with hug""" logo_path = os.path.join(BASE_DIRECTORY, 'artwork', 'logo.png') assert hasattr(hug.output_format.png_image(logo_path, hug.Response()), 'read') with open(logo_path, 'rb') as image_file: assert hasattr(hug.output_format.png_image(image_file, hug.Response()), 'read') assert hug.output_format.png_image('Not Existent', hug.Response()) is None class FakeImageWithSave(): def save(self, to, format): to.write(b'test') assert hasattr( hug.output_format.png_image(FakeImageWithSave(), hug.Response()), 'read') class FakeImageWithRender(): def render(self): return 'test' assert hug.output_format.svg_xml_image(FakeImageWithRender(), hug.Response()) == 'test' class FakeImageWithSaveNoFormat(): def save(self, to): to.write(b'test') assert hasattr( hug.output_format.png_image(FakeImageWithSaveNoFormat(), hug.Response()), 'read')
def test_image(): """Ensure that it's possible to output images with hug""" logo_path = os.path.join(BASE_DIRECTORY, "artwork", "logo.png") assert hasattr(hug.output_format.png_image(logo_path, hug.Response()), "read") with open(logo_path, "rb") as image_file: assert hasattr(hug.output_format.png_image(image_file, hug.Response()), "read") assert hug.output_format.png_image("Not Existent", hug.Response()) is None class FakeImageWithSave: def save(self, to, format): to.write(b"test") assert hasattr(hug.output_format.png_image(FakeImageWithSave(), hug.Response()), "read") class FakeImageWithRender: def render(self): return "test" assert hug.output_format.svg_xml_image(FakeImageWithRender(), hug.Response()) == "test" class FakeImageWithSaveNoFormat: def save(self, to): to.write(b"test") assert hasattr(hug.output_format.png_image(FakeImageWithSaveNoFormat(), hug.Response()), "read")
# send file to the user to view # autodetect to restricted output formats based on suffix # all output formats listed here: # https://hugapi.github.io/hug/documentation/OUTPUT_FORMATS/ @hug.get('/img' + IMAGE_ENDPOINT, output=hug.output_format.suffix({ '.jpg': hug.output_format.image('jpg'), '.jpeg': hug.output_format.image('jpg'), '.png': hug.output_format.image('png'), '.gif': hug.output_format.image('gif'), '.webm': hug.output_format.video('webm', hug.Response(), 'read'), })) def image(board_name: str, img_fname: str): # for image from disk: # img_file = os.path.join(tempfile.gettempdir(), tfile.name) # type: str return get_fourchan_file(IMAGE_ENDPOINT.format(board_name=board_name, img_fname=img_fname), hostname=FOURCHAN_IMAGE_HOST) # static file endpoints are not served here, they should be served from a directory as files, and FLOSS alternatives used for each one # # HTML endpoints #