mockstack 0.0.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mockstack/__init__.py +0 -0
- mockstack/config.py +91 -0
- mockstack/display.py +35 -0
- mockstack/identifiers.py +84 -0
- mockstack/lifespan.py +29 -0
- mockstack/main.py +31 -0
- mockstack/middleware.py +19 -0
- mockstack/opentelemetry.py +45 -0
- mockstack/routers/__init__.py +0 -0
- mockstack/routers/catchall.py +33 -0
- mockstack/routers/homepage.py +18 -0
- mockstack/strategies/__init__.py +0 -0
- mockstack/strategies/base.py +14 -0
- mockstack/strategies/factory.py +21 -0
- mockstack/strategies/filefixtures.py +230 -0
- mockstack/templating.py +114 -0
- mockstack/tests/__init__.py +0 -0
- mockstack/tests/conftest.py +29 -0
- mockstack/tests/fixtures/templates/__init__.py +0 -0
- mockstack/tests/routers/__init__.py +0 -0
- mockstack/tests/routers/test_catchall.py +46 -0
- mockstack/tests/routers/test_homepage.py +27 -0
- mockstack/tests/strategies/test_filefixtures.py +312 -0
- mockstack/tests/test_display.py +33 -0
- mockstack/tests/test_identifiers.py +94 -0
- mockstack/tests/test_middleware.py +37 -0
- mockstack/tests/test_templating.py +220 -0
- mockstack-0.0.3.dist-info/METADATA +13 -0
- mockstack-0.0.3.dist-info/RECORD +32 -0
- mockstack-0.0.3.dist-info/WHEEL +5 -0
- mockstack-0.0.3.dist-info/licenses/LICENSE +21 -0
- mockstack-0.0.3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""Tests for the middleware module."""
|
|
2
|
+
|
|
3
|
+
import time
|
|
4
|
+
|
|
5
|
+
import pytest
|
|
6
|
+
from starlette.testclient import TestClient
|
|
7
|
+
|
|
8
|
+
from mockstack.middleware import middleware_provider
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@pytest.mark.asyncio
|
|
12
|
+
async def test_middleware_provider_process_time(app, settings):
|
|
13
|
+
"""Test that the middleware provider adds the process time header."""
|
|
14
|
+
# Apply the middleware provider
|
|
15
|
+
middleware_provider(app, settings)
|
|
16
|
+
|
|
17
|
+
# Create a test client
|
|
18
|
+
client = TestClient(app)
|
|
19
|
+
|
|
20
|
+
# Add a test route
|
|
21
|
+
@app.get("/test")
|
|
22
|
+
def test_route():
|
|
23
|
+
# Simulate some processing time
|
|
24
|
+
time.sleep(0.1)
|
|
25
|
+
return {"message": "test"}
|
|
26
|
+
|
|
27
|
+
# Make a request
|
|
28
|
+
response = client.get("/test")
|
|
29
|
+
|
|
30
|
+
# Verify the response
|
|
31
|
+
assert response.status_code == 200
|
|
32
|
+
assert response.json() == {"message": "test"}
|
|
33
|
+
|
|
34
|
+
# Verify the X-Process-Time header exists and is a float
|
|
35
|
+
assert "X-Process-Time" in response.headers
|
|
36
|
+
process_time = float(response.headers["X-Process-Time"])
|
|
37
|
+
assert process_time > 0 # Should be greater than 0 due to sleep
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
"""Unit tests for the templates module."""
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
from fastapi import Request
|
|
5
|
+
|
|
6
|
+
from mockstack.templating import (
|
|
7
|
+
iter_possible_template_arguments,
|
|
8
|
+
iter_possible_template_filenames,
|
|
9
|
+
parse_template_name_segments_and_context,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@pytest.mark.parametrize(
|
|
14
|
+
"path,expected_results",
|
|
15
|
+
[
|
|
16
|
+
(
|
|
17
|
+
"/api/v1/projects/1234",
|
|
18
|
+
[
|
|
19
|
+
{
|
|
20
|
+
"name": "api-v1-projects.1234.j2",
|
|
21
|
+
"context": {"projects": "1234"},
|
|
22
|
+
"media_type": "application/json",
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
"name": "api-v1-projects.j2",
|
|
26
|
+
"context": {"projects": "1234"},
|
|
27
|
+
"media_type": "application/json",
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"name": "index.j2",
|
|
31
|
+
"context": {"projects": "1234"},
|
|
32
|
+
"media_type": "application/json",
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
),
|
|
36
|
+
(
|
|
37
|
+
"/api/v1/users/3a4e5ad9-17ee-41af-972f-864dfccd4856",
|
|
38
|
+
[
|
|
39
|
+
{
|
|
40
|
+
"name": "api-v1-users.3a4e5ad9-17ee-41af-972f-864dfccd4856.j2",
|
|
41
|
+
"context": {"users": "3a4e5ad9-17ee-41af-972f-864dfccd4856"},
|
|
42
|
+
"media_type": "application/json",
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
"name": "api-v1-users.j2",
|
|
46
|
+
"context": {"users": "3a4e5ad9-17ee-41af-972f-864dfccd4856"},
|
|
47
|
+
"media_type": "application/json",
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
"name": "index.j2",
|
|
51
|
+
"context": {"users": "3a4e5ad9-17ee-41af-972f-864dfccd4856"},
|
|
52
|
+
"media_type": "application/json",
|
|
53
|
+
},
|
|
54
|
+
],
|
|
55
|
+
),
|
|
56
|
+
(
|
|
57
|
+
"/api/v1/projects",
|
|
58
|
+
[
|
|
59
|
+
{
|
|
60
|
+
"name": "api-v1-projects.j2",
|
|
61
|
+
"context": {},
|
|
62
|
+
"media_type": "application/json",
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
"name": "index.j2",
|
|
66
|
+
"context": {},
|
|
67
|
+
"media_type": "application/json",
|
|
68
|
+
},
|
|
69
|
+
],
|
|
70
|
+
),
|
|
71
|
+
(
|
|
72
|
+
"/1234",
|
|
73
|
+
[
|
|
74
|
+
{
|
|
75
|
+
"name": "index.j2",
|
|
76
|
+
"context": {"id": "1234"},
|
|
77
|
+
"media_type": "application/json",
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
),
|
|
81
|
+
],
|
|
82
|
+
)
|
|
83
|
+
def test_iter_possible_template_arguments(
|
|
84
|
+
path: str,
|
|
85
|
+
expected_results: list,
|
|
86
|
+
) -> None:
|
|
87
|
+
"""Test the iter_possible_template_arguments function with various paths."""
|
|
88
|
+
request = Request(
|
|
89
|
+
scope={
|
|
90
|
+
"type": "http",
|
|
91
|
+
"method": "GET",
|
|
92
|
+
"path": path,
|
|
93
|
+
"query_string": b"",
|
|
94
|
+
"headers": [],
|
|
95
|
+
}
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
results = list(iter_possible_template_arguments(request))
|
|
99
|
+
assert len(results) == len(expected_results)
|
|
100
|
+
|
|
101
|
+
for actual, expected in zip(results, expected_results):
|
|
102
|
+
assert actual["name"] == expected["name"]
|
|
103
|
+
assert actual["context"] == expected["context"]
|
|
104
|
+
assert actual["media_type"] == expected["media_type"]
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def test_iter_possible_template_arguments_with_custom_media_type():
|
|
108
|
+
"""Test that custom media type from headers is respected."""
|
|
109
|
+
request = Request(
|
|
110
|
+
scope={
|
|
111
|
+
"type": "http",
|
|
112
|
+
"method": "GET",
|
|
113
|
+
"path": "/api/v1/projects",
|
|
114
|
+
"query_string": b"",
|
|
115
|
+
"headers": [(b"content-type", b"application/xml")],
|
|
116
|
+
}
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
results = list(iter_possible_template_arguments(request))
|
|
120
|
+
assert len(results) == 2
|
|
121
|
+
assert results[0]["media_type"] == "application/xml"
|
|
122
|
+
assert results[1]["media_type"] == "application/xml"
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def test_parse_template_name_segments_and_context():
|
|
126
|
+
"""Test the parse_template_name_segments_and_context function."""
|
|
127
|
+
# Test with a simple path
|
|
128
|
+
name_segments, context = parse_template_name_segments_and_context(
|
|
129
|
+
"/api/v1/projects/1234", default_identifier_key="id"
|
|
130
|
+
)
|
|
131
|
+
assert name_segments == ["api", "v1", "projects"]
|
|
132
|
+
assert context == {"projects": "1234"}
|
|
133
|
+
|
|
134
|
+
# Test with a path with no identifiers
|
|
135
|
+
name_segments, context = parse_template_name_segments_and_context(
|
|
136
|
+
"/api/v1/projects", default_identifier_key="id"
|
|
137
|
+
)
|
|
138
|
+
assert name_segments == ["api", "v1", "projects"]
|
|
139
|
+
assert context == {}
|
|
140
|
+
|
|
141
|
+
# Test with a path with only an identifier
|
|
142
|
+
name_segments, context = parse_template_name_segments_and_context(
|
|
143
|
+
"/1234", default_identifier_key="id"
|
|
144
|
+
)
|
|
145
|
+
assert name_segments == []
|
|
146
|
+
assert context == {"id": "1234"}
|
|
147
|
+
|
|
148
|
+
# Test with a path with multiple identifiers
|
|
149
|
+
name_segments, context = parse_template_name_segments_and_context(
|
|
150
|
+
"/api/v1/projects/1234/tasks/5678", default_identifier_key="id"
|
|
151
|
+
)
|
|
152
|
+
assert name_segments == ["api", "v1", "projects", "tasks"]
|
|
153
|
+
assert context == {"projects": "1234", "tasks": "5678"}
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def test_iter_possible_template_filenames():
|
|
157
|
+
"""Test the iter_possible_template_filenames function."""
|
|
158
|
+
# Test with name segments and context
|
|
159
|
+
filenames = list(
|
|
160
|
+
iter_possible_template_filenames(
|
|
161
|
+
["api", "v1", "projects"],
|
|
162
|
+
context={"projects": "1234"},
|
|
163
|
+
template_file_separator="-",
|
|
164
|
+
template_file_extension=".j2",
|
|
165
|
+
default_template_name="index.j2",
|
|
166
|
+
)
|
|
167
|
+
)
|
|
168
|
+
assert filenames == ["api-v1-projects.1234.j2", "api-v1-projects.j2", "index.j2"]
|
|
169
|
+
|
|
170
|
+
# Test with name segments and no context
|
|
171
|
+
filenames = list(
|
|
172
|
+
iter_possible_template_filenames(
|
|
173
|
+
["api", "v1", "projects"],
|
|
174
|
+
context={},
|
|
175
|
+
template_file_separator="-",
|
|
176
|
+
template_file_extension=".j2",
|
|
177
|
+
default_template_name="index.j2",
|
|
178
|
+
)
|
|
179
|
+
)
|
|
180
|
+
assert filenames == ["api-v1-projects.j2", "index.j2"]
|
|
181
|
+
|
|
182
|
+
# Test with no name segments and context
|
|
183
|
+
filenames = list(
|
|
184
|
+
iter_possible_template_filenames(
|
|
185
|
+
[],
|
|
186
|
+
context={"id": "1234"},
|
|
187
|
+
template_file_separator="-",
|
|
188
|
+
template_file_extension=".j2",
|
|
189
|
+
default_template_name="index.j2",
|
|
190
|
+
)
|
|
191
|
+
)
|
|
192
|
+
assert filenames == ["index.j2"]
|
|
193
|
+
|
|
194
|
+
# Test with no name segments and no context
|
|
195
|
+
filenames = list(
|
|
196
|
+
iter_possible_template_filenames(
|
|
197
|
+
[],
|
|
198
|
+
context={},
|
|
199
|
+
template_file_separator="-",
|
|
200
|
+
template_file_extension=".j2",
|
|
201
|
+
default_template_name="index.j2",
|
|
202
|
+
)
|
|
203
|
+
)
|
|
204
|
+
assert filenames == ["index.j2"]
|
|
205
|
+
|
|
206
|
+
# Test with custom separator and extension
|
|
207
|
+
filenames = list(
|
|
208
|
+
iter_possible_template_filenames(
|
|
209
|
+
["api", "v1", "projects"],
|
|
210
|
+
context={"projects": "1234"},
|
|
211
|
+
template_file_separator="_",
|
|
212
|
+
template_file_extension=".html",
|
|
213
|
+
default_template_name="default.html",
|
|
214
|
+
)
|
|
215
|
+
)
|
|
216
|
+
assert filenames == [
|
|
217
|
+
"api_v1_projects.1234.html",
|
|
218
|
+
"api_v1_projects.html",
|
|
219
|
+
"default.html",
|
|
220
|
+
]
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mockstack
|
|
3
|
+
Version: 0.0.3
|
|
4
|
+
Requires-Python: >=3.11
|
|
5
|
+
License-File: LICENSE
|
|
6
|
+
Requires-Dist: colorama>=0.4.6
|
|
7
|
+
Requires-Dist: fastapi[standard]>=0.115.12
|
|
8
|
+
Requires-Dist: jinja2>=3.1.6
|
|
9
|
+
Requires-Dist: opentelemetry-distro[otlp]>=0.53b1
|
|
10
|
+
Requires-Dist: opentelemetry-instrumentation-fastapi>=0.53b1
|
|
11
|
+
Requires-Dist: pydantic>=2.11.3
|
|
12
|
+
Requires-Dist: pydantic-settings>=2.9.1
|
|
13
|
+
Dynamic: license-file
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
mockstack/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
mockstack/config.py,sha256=-qyD1Q69DVX0UsVWGWDbN8QjiWKup2YbrbL4peoqEQk,2860
|
|
3
|
+
mockstack/display.py,sha256=JvURZz-Ft9Mq-umzXfVKKQB8WlcxPwMYtc8DkXp-4X8,1022
|
|
4
|
+
mockstack/identifiers.py,sha256=EGR_87PRA4B-m3ZvEtTnjtH-xy_uptgDAa18ePpHijA,2284
|
|
5
|
+
mockstack/lifespan.py,sha256=MPB5jkF1440NY_JP3DuSn9Y0wF3t-8V599O327zakDk,704
|
|
6
|
+
mockstack/main.py,sha256=iP26S1LPxuRMlm9Z9wIcQl6o9zAW_IuwkDU3fot24d8,904
|
|
7
|
+
mockstack/middleware.py,sha256=Wsw80FOFLSP31UII3XNi-ujTFApgxUIRAX3B1L2BIXo,578
|
|
8
|
+
mockstack/opentelemetry.py,sha256=IFHTkOiQXT6xP2hT6eMdRt-8wf5N2obS7rDb-YvpS_w,1565
|
|
9
|
+
mockstack/templating.py,sha256=RthzYM9jRgaJw6b6yhNR_EptnDHnIMkrRiLQZuGZCgY,3851
|
|
10
|
+
mockstack/routers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
11
|
+
mockstack/routers/catchall.py,sha256=EHN_eDF39x1uEge1V8Iq-2YzE81cqraNOdZJRrTQr6Y,1340
|
|
12
|
+
mockstack/routers/homepage.py,sha256=nq00WsZ-Oobza8Llg_dtnBgAjOJl7XKl0YiOPKoeMWI,384
|
|
13
|
+
mockstack/strategies/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
14
|
+
mockstack/strategies/base.py,sha256=uxkDtbMFPmrnqbZYIHQrmDXsqg5iplV7KHulciNJ-6U,330
|
|
15
|
+
mockstack/strategies/factory.py,sha256=qxz4dUL3cheWiOMh-Z1ol-uMTjfKwuN62pkClpcLSK8,628
|
|
16
|
+
mockstack/strategies/filefixtures.py,sha256=dbyN9DomZlOpa6MDuorpjZ8wlGD1uPgnSyCnLIq1--k,8467
|
|
17
|
+
mockstack/tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
18
|
+
mockstack/tests/conftest.py,sha256=ZzOZ9vJxN30orodCENNZySkgX3Gicqw0CzWVBJsWaQo,645
|
|
19
|
+
mockstack/tests/test_display.py,sha256=Hi0tkJB66KZ-UQAOVdKBejPtSSYvj4_8L4lGhtirHwc,1146
|
|
20
|
+
mockstack/tests/test_identifiers.py,sha256=IcSu8YbFAUKkC9SlcjqjxDzIzcZqpau8eXgJxi0DffM,3462
|
|
21
|
+
mockstack/tests/test_middleware.py,sha256=OVXMhz24vnnHuJ28IvNIfxoHpayQy6FwAF4-M5TYuM0,1038
|
|
22
|
+
mockstack/tests/test_templating.py,sha256=1SrSSj4-7AXPQ8yqMoZwSBsf07y4LHfoSh3-CP2KjC8,7031
|
|
23
|
+
mockstack/tests/fixtures/templates/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
24
|
+
mockstack/tests/routers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
25
|
+
mockstack/tests/routers/test_catchall.py,sha256=KF4bADwIZ-M-OrjnyL0Ham39jTaOorbNWMyMxXwAZ7Y,1398
|
|
26
|
+
mockstack/tests/routers/test_homepage.py,sha256=PYQ7kL7Ar30clGHp4osF0EjhyGyLdueQq4kX7eeT65g,715
|
|
27
|
+
mockstack/tests/strategies/test_filefixtures.py,sha256=LhDbkqVGbunhHB77ssGl1o-vJVcVD1hk4gZ8I-9se4c,9871
|
|
28
|
+
mockstack-0.0.3.dist-info/licenses/LICENSE,sha256=ACwmltkrXIz5VsEQcrqljq-fat6ZXAMepjXGoe40KtE,1069
|
|
29
|
+
mockstack-0.0.3.dist-info/METADATA,sha256=d9yfXsT5GWxH_BuHlMjwXXcf5w_SWj4BVGImIwlbXr8,407
|
|
30
|
+
mockstack-0.0.3.dist-info/WHEEL,sha256=ck4Vq1_RXyvS4Jt6SI0Vz6fyVs4GWg7AINwpsaGEgPE,91
|
|
31
|
+
mockstack-0.0.3.dist-info/top_level.txt,sha256=76AMvtzj_OvqnLjkAwC4R3VKkepm5nY_ms4xFbl0t2A,10
|
|
32
|
+
mockstack-0.0.3.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) [year] [fullname]
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
mockstack
|