Skip to content

Commit

Permalink
add tests for read error
Browse files Browse the repository at this point in the history
  • Loading branch information
Cycloctane committed Jan 15, 2025
1 parent a1a4bc5 commit b1c90b4
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions tests/test_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import pathlib
import posixpath
from unittest import mock

import pytest

Expand All @@ -14,9 +15,6 @@
import falcon.testing as testing


MTIME = (1736617934, "Sat, 11 Jan 2025 17:52:14 GMT")


def normalize_path(path):
# NOTE(vytas): On CPython 3.13, ntpath.isabs() no longer returns True for
# Unix-like absolute paths that start with a single \.
Expand Down Expand Up @@ -55,7 +53,7 @@ def create_sr(asgi, prefix, directory, **kwargs):

@pytest.fixture
def patch_open(monkeypatch):
def patch(content=None, validate=None):
def patch(content=None, validate=None, mtime=1736617934):
class FakeStat:
def __init__(self, size, mtime):
self.st_size = size
Expand All @@ -73,11 +71,16 @@ def open(path, mode):
patch.current_file = fake_file
return fake_file

def _stat(path, **kwargs):
return FakeStat(len(open(path, 'rb').getvalue()), 1736617934)
def stat(path, **kwargs):

if validate:
validate(path)

data = path.encode() if content is None else content
return FakeStat(len(data), mtime)

monkeypatch.setattr(io, 'open', open)
monkeypatch.setattr(falcon.routing.static, '_stat', _stat)
monkeypatch.setattr(falcon.routing.static, '_stat', stat)

patch.current_file = None
return patch
Expand Down Expand Up @@ -640,17 +643,19 @@ def test_options_request(client, patch_open):


def test_last_modified(client, patch_open):
patch_open()
mtime = (1736617934, "Sat, 11 Jan 2025 17:52:14 GMT")
patch_open(mtime=mtime[0])

client.app.add_static_route('/assets/', '/opt/somesite/assets')

response = client.simulate_request(path='/assets/css/main.css')
assert response.status == falcon.HTTP_200
assert response.headers['Last-Modified'] == MTIME[1]
assert response.headers['Last-Modified'] == mtime[1]


def test_304_with_if_modified_since(client, patch_open):
patch_open()
def test_if_modified_since(client, patch_open):
mtime = (1736617934, "Sat, 11 Jan 2025 17:52:14 GMT")
patch_open(mtime=mtime[0])

client.app.add_static_route('/assets/', '/opt/somesite/assets')

Expand All @@ -667,3 +672,25 @@ def test_304_with_if_modified_since(client, patch_open):
)
assert resp.status == falcon.HTTP_200
assert resp.text != ''


def test_permission_error(client, patch_open):
patch_open()
client.app.add_static_route('/assets/', '/opt/somesite/assets')

with mock.patch("io.open", mock.mock_open()) as m:
m.side_effect = PermissionError()
resp = client.simulate_request(path='/assets/css/main.css')

assert resp.status == falcon.HTTP_403


def test_read_error(client, patch_open):
patch_open()
client.app.add_static_route('/assets/', '/opt/somesite/assets')

with mock.patch("io.open", mock.mock_open()) as m:
m.side_effect = IOError()
resp = client.simulate_request(path='/assets/css/main.css')

assert resp.status == falcon.HTTP_404

0 comments on commit b1c90b4

Please sign in to comment.