| 1 |
import pathlib |
| 2 |
import pytest |
| 3 |
from .fixtures import make_app_client |
| 4 |
|
| 5 |
TEST_TEMPLATE_DIRS = str(pathlib.Path(__file__).parent / "test_templates") |
| 6 |
|
| 7 |
|
| 8 |
@pytest.fixture(scope="session") |
| 9 |
def custom_pages_client(): |
| 10 |
with make_app_client(template_dir=TEST_TEMPLATE_DIRS) as client: |
| 11 |
yield client |
| 12 |
|
| 13 |
|
| 14 |
@pytest.fixture(scope="session") |
| 15 |
def custom_pages_client_with_base_url(): |
| 16 |
with make_app_client( |
| 17 |
template_dir=TEST_TEMPLATE_DIRS, settings={"base_url": "/prefix/"} |
| 18 |
) as client: |
| 19 |
yield client |
| 20 |
|
| 21 |
|
| 22 |
def test_custom_pages_view_name(custom_pages_client): |
| 23 |
response = custom_pages_client.get("/about") |
| 24 |
assert response.status == 200 |
| 25 |
assert response.text == "ABOUT! view_name:page" |
| 26 |
|
| 27 |
|
| 28 |
def test_request_is_available(custom_pages_client): |
| 29 |
response = custom_pages_client.get("/request") |
| 30 |
assert response.status == 200 |
| 31 |
assert response.text == "path:/request" |
| 32 |
|
| 33 |
|
| 34 |
def test_custom_pages_with_base_url(custom_pages_client_with_base_url): |
| 35 |
response = custom_pages_client_with_base_url.get("/prefix/request") |
| 36 |
assert response.status == 200 |
| 37 |
assert response.text == "path:/prefix/request" |
| 38 |
|
| 39 |
|
| 40 |
def test_custom_pages_nested(custom_pages_client): |
| 41 |
response = custom_pages_client.get("/nested/nest") |
| 42 |
assert response.status == 200 |
| 43 |
assert response.text == "Nest!" |
| 44 |
response = custom_pages_client.get("/nested/nest2") |
| 45 |
assert response.status == 404 |
| 46 |
|
| 47 |
|
| 48 |
def test_custom_status(custom_pages_client): |
| 49 |
response = custom_pages_client.get("/202") |
| 50 |
assert response.status == 202 |
| 51 |
assert response.text == "202!" |
| 52 |
|
| 53 |
|
| 54 |
def test_custom_headers(custom_pages_client): |
| 55 |
response = custom_pages_client.get("/headers") |
| 56 |
assert response.status == 200 |
| 57 |
assert response.headers["x-this-is-foo"] == "foo" |
| 58 |
assert response.headers["x-this-is-bar"] == "bar" |
| 59 |
assert response.text == "FOOBAR" |
| 60 |
|
| 61 |
|
| 62 |
def test_custom_content_type(custom_pages_client): |
| 63 |
response = custom_pages_client.get("/atom") |
| 64 |
assert response.status == 200 |
| 65 |
assert response.headers["content-type"] == "application/xml" |
| 66 |
assert response.text == "<?xml ...>" |
| 67 |
|
| 68 |
|
| 69 |
def test_redirect(custom_pages_client): |
| 70 |
response = custom_pages_client.get("/redirect") |
| 71 |
assert response.status == 302 |
| 72 |
assert response.headers["Location"] == "/example" |
| 73 |
|
| 74 |
|
| 75 |
def test_redirect2(custom_pages_client): |
| 76 |
response = custom_pages_client.get("/redirect2") |
| 77 |
assert response.status == 301 |
| 78 |
assert response.headers["Location"] == "/example" |
| 79 |
|
| 80 |
|
| 81 |
@pytest.mark.parametrize( |
| 82 |
"path,expected", |
| 83 |
[ |
| 84 |
("/route_Sally", "<p>Hello from Sally</p>"), |
| 85 |
("/topic_python", "Topic page for python"), |
| 86 |
("/topic_python/info", "Slug: info, Topic: python"), |
| 87 |
], |
| 88 |
) |
| 89 |
def test_custom_route_pattern(custom_pages_client, path, expected): |
| 90 |
response = custom_pages_client.get(path) |
| 91 |
assert response.status == 200 |
| 92 |
assert response.text.strip() == expected |
| 93 |
|
| 94 |
|
| 95 |
def test_custom_route_pattern_404(custom_pages_client): |
| 96 |
response = custom_pages_client.get("/route_OhNo") |
| 97 |
assert response.status == 404 |
| 98 |
assert "<h1>Error 404</h1>" in response.text |
| 99 |
assert ">Oh no</" in response.text |
| 100 |
|
| 101 |
|
| 102 |
def test_custom_route_pattern_with_slash_slash_302(custom_pages_client): |
| 103 |
# https://github.com/simonw/datasette/issues/2429 |
| 104 |
response = custom_pages_client.get("//example.com/") |
| 105 |
assert response.status == 302 |
| 106 |
assert response.headers["location"] == "/example.com" |