|
1 | 1 | """Tests for tilebench.""" |
2 | 2 |
|
| 3 | +import rasterio |
3 | 4 | from fastapi import FastAPI |
4 | 5 | from rio_tiler.io import Reader |
5 | 6 | from starlette.testclient import TestClient |
| 7 | +from vsifile.rasterio import opener |
6 | 8 |
|
7 | 9 | from tilebench.middleware import NoCacheMiddleware, VSIStatsMiddleware |
8 | 10 |
|
@@ -33,26 +35,77 @@ def tile(): |
33 | 35 | def skip(): |
34 | 36 | return "I've been skipped" |
35 | 37 |
|
36 | | - client = TestClient(app) |
37 | | - |
38 | | - response = client.get("/info") |
39 | | - assert response.status_code == 200 |
40 | | - assert response.headers["content-type"] == "application/json" |
41 | | - assert response.headers["Cache-Control"] == "no-cache" |
42 | | - assert response.headers["VSI-Stats"] |
43 | | - stats = response.headers["VSI-Stats"] |
44 | | - assert "head;count=" in stats |
45 | | - assert "get;count=" in stats |
46 | | - |
47 | | - response = client.get("/tile") |
48 | | - assert response.status_code == 200 |
49 | | - assert response.headers["content-type"] == "application/json" |
50 | | - assert response.headers["VSI-Stats"] |
51 | | - stats = response.headers["VSI-Stats"] |
52 | | - assert "head;count=" in stats |
53 | | - assert "get;count=" in stats |
54 | | - |
55 | | - response = client.get("/skip") |
56 | | - assert response.status_code == 200 |
57 | | - assert response.headers["content-type"] == "application/json" |
58 | | - assert "VSI-Stats" not in response.headers |
| 38 | + with TestClient(app) as client: |
| 39 | + response = client.get("/info") |
| 40 | + assert response.status_code == 200 |
| 41 | + assert response.headers["content-type"] == "application/json" |
| 42 | + assert response.headers["Cache-Control"] == "no-cache" |
| 43 | + assert response.headers["VSI-Stats"] |
| 44 | + stats = response.headers["VSI-Stats"] |
| 45 | + assert "head;count=" in stats |
| 46 | + assert "get;count=" in stats |
| 47 | + |
| 48 | + response = client.get("/tile") |
| 49 | + assert response.status_code == 200 |
| 50 | + assert response.headers["content-type"] == "application/json" |
| 51 | + assert response.headers["VSI-Stats"] |
| 52 | + stats = response.headers["VSI-Stats"] |
| 53 | + assert "head;count=" in stats |
| 54 | + assert "get;count=" in stats |
| 55 | + |
| 56 | + response = client.get("/skip") |
| 57 | + assert response.status_code == 200 |
| 58 | + assert response.headers["content-type"] == "application/json" |
| 59 | + assert "VSI-Stats" not in response.headers |
| 60 | + |
| 61 | + |
| 62 | +def test_middleware_vsifile(): |
| 63 | + """Simple test.""" |
| 64 | + app = FastAPI() |
| 65 | + app.add_middleware(NoCacheMiddleware) |
| 66 | + app.add_middleware( |
| 67 | + VSIStatsMiddleware, config={}, exclude_paths=["/skip"], io="vsifile" |
| 68 | + ) |
| 69 | + |
| 70 | + @app.get("/info") |
| 71 | + def head(): |
| 72 | + """Get info.""" |
| 73 | + with rasterio.open(COG_PATH, opener=opener) as src: |
| 74 | + with Reader(None, dataset=src) as cog: |
| 75 | + cog.info() |
| 76 | + return "I got info" |
| 77 | + |
| 78 | + @app.get("/tile") |
| 79 | + def tile(): |
| 80 | + """Read tile.""" |
| 81 | + with rasterio.open(COG_PATH, opener=opener) as src: |
| 82 | + with Reader(None, dataset=src) as cog: |
| 83 | + cog.tile(36460, 52866, 17) |
| 84 | + return "I got tile" |
| 85 | + |
| 86 | + @app.get("/skip") |
| 87 | + def skip(): |
| 88 | + return "I've been skipped" |
| 89 | + |
| 90 | + with TestClient(app) as client: |
| 91 | + response = client.get("/info") |
| 92 | + assert response.status_code == 200 |
| 93 | + assert response.headers["content-type"] == "application/json" |
| 94 | + assert response.headers["Cache-Control"] == "no-cache" |
| 95 | + assert response.headers["VSI-Stats"] |
| 96 | + stats = response.headers["VSI-Stats"] |
| 97 | + assert "head;count=" in stats |
| 98 | + assert "get;count=" in stats |
| 99 | + |
| 100 | + response = client.get("/tile") |
| 101 | + assert response.status_code == 200 |
| 102 | + assert response.headers["content-type"] == "application/json" |
| 103 | + assert response.headers["VSI-Stats"] |
| 104 | + stats = response.headers["VSI-Stats"] |
| 105 | + assert "head;count=" in stats |
| 106 | + assert "get;count=" in stats |
| 107 | + |
| 108 | + response = client.get("/skip") |
| 109 | + assert response.status_code == 200 |
| 110 | + assert response.headers["content-type"] == "application/json" |
| 111 | + assert "VSI-Stats" not in response.headers |
0 commit comments