def test_process_url_data_dir_exists(): base = '"/static/{data_dir}/file.png"'.format(data_dir=DATA_DIRECTORY) def processor(original, prefix, quote, rest): # pylint: disable=unused-argument,missing-docstring return quote + 'test' + rest + quote assert_equals(base, process_static_urls(base, processor, data_dir=DATA_DIRECTORY))
def transform_static_paths_to_urls(self, block, html_str): """ Given an HTML string, replace any static file paths like /static/foo.png (which are really pointing to block-specific assets stored in blockstore) with working absolute URLs like https://s3.example.com/blockstore/bundle17/this-block/assets/324.png See common/djangoapps/static_replace/__init__.py This is generally done automatically for the HTML rendered by XBlocks, but if an XBlock wants to have correct URLs in data returned by its handlers, the XBlock must call this API directly. Note that the paths are only replaced if they are in "quotes" such as if they are an HTML attribute or JSON data value. Thus, to transform only a single path string on its own, you must pass html_str=f'"{path}"' """ def replace_static_url(original, prefix, quote, rest): # pylint: disable=unused-argument """ Replace a single matched url. """ original_url = prefix + rest # Don't mess with things that end in '?raw' if rest.endswith('?raw'): new_url = original_url else: new_url = self._lookup_asset_url(block, rest) or original_url return "".join([quote, new_url, quote]) return process_static_urls(html_str, replace_static_url)
def test_process_url_no_match(): def processor(__, prefix, quote, rest): # pylint: disable=missing-docstring return quote + 'test' + prefix + rest + quote assert_equals('"test/static/file.png"', process_static_urls(STATIC_SOURCE, processor))
def test_process_url_no_match(): def processor(__, prefix, quote, rest): return quote + 'test' + prefix + rest + quote assert process_static_urls(STATIC_SOURCE, processor) == '"test/static/file.png"'