Пример #1
0
    def test_convert_png_data_url_to_binary(self) -> None:
        image_data_url = '%s%s' % (
            utils.PNG_DATA_URL_PREFIX,
            python_utils.url_quote(base64.b64encode(b'test123'))) # type: ignore[no-untyped-call]

        self.assertEqual(
            utils.convert_png_data_url_to_binary(image_data_url), b'test123')
Пример #2
0
    def sanitize_url(obj):
        """Takes a string representing a URL and sanitizes it.

        Args:
            obj: a string representing a URL.

        Returns:
            An empty string if the URL does not start with http:// or https://
            except when the string is empty. Otherwise, returns the original
            URL.

        Raises:
            AssertionError: The string is non-empty and does not start with
            http:// or https://
        """
        if obj == '':
            return obj
        url_components = python_utils.url_split(obj)
        quoted_url_components = (python_utils.url_quote(component)
                                 for component in url_components)
        raw = python_utils.url_unsplit(quoted_url_components)

        acceptable = html_cleaner.filter_a('a', 'href', obj)
        assert acceptable, ('Invalid URL: Sanitized URL should start with '
                            '\'http://\' or \'https://\'; received %s' % raw)
        return raw
Пример #3
0
    def test_convert_png_data_url_to_binary_raises_if_prefix_is_missing(self):
        # type: () -> None
        image_data_url = python_utils.url_quote(base64.b64encode('test123')) # type: ignore[no-untyped-call]

        self.assertRaisesRegexp( # type: ignore[no-untyped-call]
            Exception, 'The given string does not represent a PNG data URL.',
            lambda: utils.convert_png_data_url_to_binary(image_data_url))
Пример #4
0
def convert_png_binary_to_data_url(content):
    """Converts a png image string (represented by 'content') to a data URL.

    Args:
        content: str. PNG binary file content.

    Returns:
        *. Data url created from the binary content of the PNG.

    Raises:
        Exception: If the given binary string is not of a PNG image.
    """
    if imghdr.what(None, h=content) == 'png':
        return 'data:image/png;base64,%s' % python_utils.url_quote(
            base64.b64encode(content))
    else:
        raise Exception('The given string does not represent a PNG image.')
Пример #5
0
def convert_png_binary_to_data_url(content):
    """Converts a PNG image string (represented by 'content') to a data URL.

    Args:
        content: str. PNG binary file content.

    Returns:
        str. Data URL created from the binary content of the PNG.

    Raises:
        Exception. The given binary string does not represent a PNG image.
    """
    if imghdr.what(None, h=content) == 'png':
        return '%s%s' % (PNG_DATA_URL_PREFIX,
                         python_utils.url_quote(base64.b64encode(content)))
    else:
        raise Exception('The given string does not represent a PNG image.')
Пример #6
0
def convert_png_binary_to_data_url(content: Union[str, bytes]) -> str:
    """Converts a PNG image string (represented by 'content') to a data URL.

    Args:
        content: str. PNG binary file content.

    Returns:
        str. Data URL created from the binary content of the PNG.

    Raises:
        Exception. The given binary string does not represent a PNG image.
    """
    # We accept unicode but imghdr.what(file, h) accepts 'h' of type bytes.
    # So we have casted content to be bytes.
    content = python_utils.convert_to_bytes(content)
    if imghdr.what(None, h=content) == 'png':
        return '%s%s' % (
            PNG_DATA_URL_PREFIX,
            python_utils.url_quote(base64.b64encode(content))  # type: ignore[no-untyped-call]
        )
    else:
        raise Exception('The given string does not represent a PNG image.')
Пример #7
0
 def test_url_quote(self):
     self.assertEqual(python_utils.url_quote(b'/~connolly/'),
                      b'/%7Econnolly/')