Ejemplo n.º 1
0
    def test_serves(self):
        whitenoise = static.WhiteNoise(None, autorefresh=True)
        whitenoise.add_files(
            static.resolver.resolve("warehouse:/static/dist/").abspath(),
            prefix="/static/",
        )

        path, headers = (whitenoise.find_file("/static/manifest.json")
                                   .get_path_and_headers({}))
        headers = dict(headers)

        response = pretend.stub()
        handler = pretend.call_recorder(lambda request: response)
        registry = pretend.stub(whitenoise=whitenoise)

        request = pretend.stub(
            method="GET",
            environ={},
            path_info="/static/manifest.json",
            registry=registry,
        )

        tween = static.whitenoise_tween_factory(handler, registry)
        resp = tween(request)

        assert resp.status_code == 200
        assert resp.headers["Content-Type"] == "application/json"
        assert (
            set(i.strip() for i in resp.headers["Cache-Control"].split(","))
            == {"public", "max-age=60"}
        )
        assert resp.headers["Vary"] == "Accept-Encoding"

        with open(path, "rb") as fp:
            assert resp.body == fp.read()
Ejemplo n.º 2
0
    def test_is_immutable_file_in_manifest(self):
        whitenoise = static.WhiteNoise(None, manifest="/path/to/manifest.json")
        whitenoise._manifest = {"the/file.txt"}

        assert whitenoise.is_immutable_file(
            "/path/to/the/file.txt",
            "static/the/file.txt",
        )
Ejemplo n.º 3
0
    def test_loads_manifest(self, tmpdir):
        manifest_path = str(tmpdir.join("manifest.json"))
        with open(manifest_path, "w", encoding="utf8") as fp:
            json.dump({"file.txt": "file.hash.txt"}, fp)

        whitenoise = static.WhiteNoise(None, manifest=manifest_path)

        assert whitenoise.manifest_path == manifest_path
        assert whitenoise.manifest == {"file.hash.txt"}
Ejemplo n.º 4
0
    def test_resolves_manifest_path(self, monkeypatch):
        resolver = pretend.stub(resolve=pretend.call_recorder(
            lambda p: pretend.stub(abspath=lambda: "/path/to/manifest.json")))
        monkeypatch.setattr(static, "resolver", resolver)

        whitenoise = static.WhiteNoise(None,
                                       manifest="warehouse:manifest.json")

        assert whitenoise.manifest_path == "/path/to/manifest.json"
        assert resolver.resolve.calls == [
            pretend.call("warehouse:manifest.json")
        ]
Ejemplo n.º 5
0
    def test_bypasses(self, autorefresh):
        whitenoise = static.WhiteNoise(None, autorefresh=autorefresh)
        whitenoise.add_files(
            static.resolver.resolve("warehouse:/static/dist/").abspath(),
            prefix="/static/",
        )

        response = pretend.stub()
        handler = pretend.call_recorder(lambda request: response)
        registry = pretend.stub(whitenoise=whitenoise)

        request = pretend.stub(path_info="/other/", registry=registry)

        tween = static.whitenoise_tween_factory(handler, registry)
        resp = tween(request)

        assert resp is response
Ejemplo n.º 6
0
    def test_caches_manifest(self, tmpdir, autorefresh):
        manifest_path = str(tmpdir.join("manifest.json"))
        with open(manifest_path, "w", encoding="utf8") as fp:
            json.dump({"file.txt": "file.hash.txt"}, fp)

        whitenoise = static.WhiteNoise(None,
                                       manifest=manifest_path,
                                       autorefresh=autorefresh)

        assert whitenoise.manifest_path == manifest_path
        assert whitenoise.manifest == {"file.hash.txt"}

        with open(manifest_path, "w", encoding="utf8") as fp:
            json.dump({"file.txt": "file.newhash.txt"}, fp)

        assert whitenoise.manifest == ({"file.newhash.txt"}
                                       if autorefresh else {"file.hash.txt"})
Ejemplo n.º 7
0
    def test_method_not_allowed(self, autorefresh):
        whitenoise = static.WhiteNoise(None, autorefresh=autorefresh)
        whitenoise.add_files(
            static.resolver.resolve("warehouse:/static/dist/").abspath(),
            prefix="/static/",
        )

        response = pretend.stub()
        handler = pretend.call_recorder(lambda request: response)
        registry = pretend.stub(whitenoise=whitenoise)

        request = pretend.stub(
            method="POST",
            environ={"HTTP_ACCEPT_ENCODING": "gzip"},
            path_info="/static/manifest.json",
            registry=registry,
        )

        tween = static.whitenoise_tween_factory(handler, registry)
        resp = tween(request)

        assert resp.status_code == 405
Ejemplo n.º 8
0
 def test_is_immutable_file_wrong_path(self):
     whitenoise = static.WhiteNoise(None, manifest="/path/to/manifest.json")
     assert not whitenoise.is_immutable_file(
         "/path/in/another/dir",
         "/static/another/dir",
     )
Ejemplo n.º 9
0
 def test_is_immutable_file_no_manifest(self):
     whitenoise = static.WhiteNoise(None)
     assert not whitenoise.is_immutable_file(None, None)
Ejemplo n.º 10
0
 def test_empty_manifest_when_no_manifest_provided(self):
     whitenoise = static.WhiteNoise(None)
     assert whitenoise.manifest == set()