How to use whitenoise - 10 common examples

To help you get started, we’ve selected a few whitenoise examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github evansd / whitenoise / whitenoise / gzip.py View on Github external
def compress(path, log=null_log):
    gzip_path = path + '.gz'
    with open(path, 'rb') as in_file:
        # Explicitly set mtime to 0 so gzip content is fully determined
        # by file content (0 = "no timestamp" according to gzip spec)
        with gzip.GzipFile(gzip_path, 'wb', compresslevel=9, mtime=0) as out_file:
            for chunk in iter(lambda: in_file.read(CHUNK_SIZE), b''):
                out_file.write(chunk)
    # If gzipped file isn't actually any smaller then get rid of it
    orig_size = os.path.getsize(path)
    gzip_size = os.path.getsize(gzip_path)
    if not is_worth_gzipping(orig_size, gzip_size):
        log('Skipping {0} (Gzip not effective)'.format(path))
        os.unlink(gzip_path)
    else:
        log('Gzipping {0} ({1}K -> {2}K)'.format(
            path, orig_size // 1024, gzip_size // 1024))
github evansd / whitenoise / tests / test_whitenoise.py View on Github external
def _init_application(directory, **kwargs):
    def custom_headers(headers, path, url):
        if url.endswith(".css"):
            headers["X-Is-Css-File"] = "True"

    return WhiteNoise(
        demo_app,
        root=directory,
        max_age=1000,
        mimetypes={".foobar": "application/x-foo-bar"},
        add_headers_function=custom_headers,
        index_file=True,
        **kwargs
    )
github evansd / whitenoise / tests / test_compress.py View on Github external
def test_compress():
    compressor = Compressor(use_brotli=False, use_gzip=False)
    assert [] == list(compressor.compress("tests/test_files/static/styles.css"))
github evansd / whitenoise / tests / test_compress.py View on Github external
def test_compressed_effectively_no_orig_size():
    compressor = Compressor(quiet=True)
    assert not compressor.is_compressed_effectively(
        "test_encoding", "test_path", 0, "test_data"
    )
github evansd / whitenoise / tests / test_compress.py View on Github external
def test_with_custom_extensions():
    compressor = Compressor(extensions=["jpg"], quiet=True)
    assert compressor.extension_re == re.compile(r"\.(jpg)$", re.IGNORECASE)
github evansd / whitenoise / tests / test_compress.py View on Github external
def test_custom_log():
    compressor = Compressor(log="test")
    assert compressor.log == "test"
github evansd / whitenoise / tests / test_django_whitenoise.py View on Github external
def test_unversioned_file_not_cached_forever(server, static_files, _collect_static):
    url = settings.STATIC_URL + static_files.js_path
    response = server.get(url)
    assert response.content == static_files.js_content
    assert response.headers.get("Cache-Control") == "max-age={}, public".format(
        WhiteNoiseMiddleware.max_age
    )
github evansd / whitenoise / tests / test_django_whitenoise.py View on Github external
def test_versioned_file_cached_forever(server, static_files, _collect_static):
    url = storage.staticfiles_storage.url(static_files.js_path)
    response = server.get(url)
    assert response.content == static_files.js_content
    assert response.headers.get(
        "Cache-Control"
    ) == "max-age={}, public, immutable".format(WhiteNoiseMiddleware.FOREVER)
github evansd / whitenoise / tests / test_storage.py View on Github external
def test_make_helpful_exception(_compressed_manifest_storage):
    class TriggerException(HashedFilesMixin):
        def exists(self, path):
            return False

    exception = None
    try:
        TriggerException().hashed_name("/missing/file.png")
    except ValueError as e:
        exception = e
    helpful_exception = HelpfulExceptionMixin().make_helpful_exception(
        exception, "styles/app.css"
    )
    assert isinstance(helpful_exception, MissingFileError)
github evansd / whitenoise / tests / test_django_whitenoise.py View on Github external
def test_whitenoise_file_response_has_only_one_header():
    response = WhiteNoiseFileResponse(open(__file__, "rb"))
    response.close()
    headers = {key.lower() for key, value in response.items()}
    # This subclass should have none of the default headers that FileReponse
    # sets
    assert headers == {"content-type"}