-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for staticfiles and uploaded files middlewares
- Loading branch information
1 parent
a11fe5b
commit 58ba602
Showing
18 changed files
with
220 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,5 +23,4 @@ def initialize(self): | |
app.di.register(Service) | ||
|
||
await app._lifespan_startup() | ||
|
||
assert Service.startup_called |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
body { display: none } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"message": "lorem ipsum"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Lorem ipsum dolor sit amet. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Lorem ipsum dolor sit amet. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
from http import HTTPStatus | ||
from pathlib import Path | ||
|
||
import pytest | ||
from httpx import ASGITransport, AsyncClient | ||
|
||
from selva.configuration import Settings | ||
from selva.configuration.defaults import default_settings | ||
from selva.web.application import Selva | ||
from selva.web.middleware.files import StaticFilesMiddleware | ||
|
||
MIDDLEWARE = [f"{StaticFilesMiddleware.__module__}.{StaticFilesMiddleware.__name__}"] | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def chdir_fixture(monkeypatch): | ||
monkeypatch.chdir(Path(__file__).parent) | ||
|
||
|
||
async def test_static_file(): | ||
settings = Settings( | ||
default_settings | ||
| { | ||
"application": f"{__package__}.application", | ||
"middleware": MIDDLEWARE, | ||
} | ||
) | ||
app = Selva(settings) | ||
await app._lifespan_startup() | ||
|
||
client = AsyncClient(transport=ASGITransport(app=app)) | ||
response = await client.get("http://localhost:8000/static/lorem-ipsum.txt") | ||
|
||
assert response.status_code == HTTPStatus.OK | ||
assert "text/plain" in response.headers["Content-Type"] | ||
assert response.text == "Lorem ipsum dolor sit amet." | ||
|
||
|
||
async def test_static_file_mapping(): | ||
settings = Settings( | ||
default_settings | ||
| { | ||
"application": f"{__package__}.application", | ||
"middleware": MIDDLEWARE, | ||
"staticfiles": default_settings["staticfiles"] | ||
| { | ||
"mappings": { | ||
"text-file": "lorem-ipsum.txt", | ||
}, | ||
}, | ||
} | ||
) | ||
app = Selva(settings) | ||
await app._lifespan_startup() | ||
|
||
client = AsyncClient(transport=ASGITransport(app=app)) | ||
response = await client.get("http://localhost:8000/text-file") | ||
|
||
assert response.request.url.path == "/text-file" | ||
assert response.status_code == HTTPStatus.OK | ||
assert "text/plain" in response.headers["Content-Type"] | ||
assert response.text == "Lorem ipsum dolor sit amet." | ||
|
||
|
||
async def test_static_files_path(): | ||
settings = Settings( | ||
default_settings | ||
| { | ||
"application": f"{__package__}.application", | ||
"middleware": MIDDLEWARE, | ||
"staticfiles": default_settings["staticfiles"] | ||
| { | ||
"path": "assets", | ||
}, | ||
} | ||
) | ||
app = Selva(settings) | ||
await app._lifespan_startup() | ||
|
||
client = AsyncClient(transport=ASGITransport(app=app)) | ||
response = await client.get("http://localhost:8000/static/lorem-ipsum.txt") | ||
assert response.status_code == HTTPStatus.NOT_FOUND | ||
|
||
response = await client.get("http://localhost:8000/assets/lorem-ipsum.txt") | ||
assert response.status_code == HTTPStatus.OK | ||
assert "text/plain" in response.headers["Content-Type"] | ||
assert response.text == "Lorem ipsum dolor sit amet." | ||
|
||
|
||
async def test_static_files_root(): | ||
settings = Settings( | ||
default_settings | ||
| { | ||
"application": f"{__package__}.application", | ||
"middleware": MIDDLEWARE, | ||
"staticfiles": default_settings["staticfiles"] | ||
| { | ||
"root": Path(__file__).parent / "resources" / "assets", | ||
}, | ||
} | ||
) | ||
app = Selva(settings) | ||
await app._lifespan_startup() | ||
|
||
client = AsyncClient(transport=ASGITransport(app=app)) | ||
response = await client.get("http://localhost:8000/static/style.css") | ||
assert response.status_code == HTTPStatus.OK | ||
assert "text/css" in response.headers["Content-Type"] | ||
assert response.text == "body { display: none }" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from http import HTTPStatus | ||
from pathlib import Path | ||
|
||
import pytest | ||
from httpx import ASGITransport, AsyncClient | ||
|
||
from selva.configuration import Settings | ||
from selva.configuration.defaults import default_settings | ||
from selva.web.application import Selva | ||
from selva.web.middleware.files import UploadedFilesMiddleware | ||
|
||
MIDDLEWARE = [f"{UploadedFilesMiddleware.__module__}.{UploadedFilesMiddleware.__name__}"] | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def chdir_fixture(monkeypatch): | ||
monkeypatch.chdir(Path(__file__).parent) | ||
|
||
|
||
async def test_uploaded_file(): | ||
settings = Settings( | ||
default_settings | ||
| { | ||
"application": f"{__package__}.application", | ||
"middleware": MIDDLEWARE, | ||
} | ||
) | ||
app = Selva(settings) | ||
await app._lifespan_startup() | ||
|
||
client = AsyncClient(transport=ASGITransport(app=app)) | ||
response = await client.get("http://localhost:8000/uploads/lorem-ipsum.txt") | ||
|
||
assert response.status_code == HTTPStatus.OK | ||
assert "text/plain" in response.headers["Content-Type"] | ||
assert response.text == "Lorem ipsum dolor sit amet." | ||
|
||
|
||
async def test_uploaded_files_path(): | ||
settings = Settings( | ||
default_settings | ||
| { | ||
"application": f"{__package__}.application", | ||
"middleware": MIDDLEWARE, | ||
"uploadedfiles": default_settings["uploadedfiles"] | ||
| { | ||
"path": "media", | ||
}, | ||
} | ||
) | ||
app = Selva(settings) | ||
await app._lifespan_startup() | ||
|
||
client = AsyncClient(transport=ASGITransport(app=app)) | ||
response = await client.get("http://localhost:8000/uploads/lorem-ipsum.txt") | ||
assert response.status_code == HTTPStatus.NOT_FOUND | ||
|
||
response = await client.get("http://localhost:8000/media/lorem-ipsum.txt") | ||
assert response.status_code == HTTPStatus.OK | ||
assert "text/plain" in response.headers["Content-Type"] | ||
assert response.text == "Lorem ipsum dolor sit amet." | ||
|
||
|
||
async def test_uploaded_files_root(): | ||
settings = Settings( | ||
default_settings | ||
| { | ||
"application": f"{__package__}.application", | ||
"middleware": MIDDLEWARE, | ||
"uploadedfiles": default_settings["uploadedfiles"] | ||
| { | ||
"root": Path(__file__).parent / "resources" / "media", | ||
}, | ||
} | ||
) | ||
app = Selva(settings) | ||
await app._lifespan_startup() | ||
|
||
client = AsyncClient(transport=ASGITransport(app=app)) | ||
response = await client.get("http://localhost:8000/uploads/data.json") | ||
assert response.status_code == HTTPStatus.OK | ||
assert "application/json" in response.headers["Content-Type"] | ||
assert response.text == '{"message": "lorem ipsum"}' |