mockstack 0.2.0__tar.gz → 0.3.0__tar.gz
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-0.3.0/.env.example +20 -0
- {mockstack-0.2.0/mockstack.egg-info → mockstack-0.3.0}/PKG-INFO +1 -1
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/config.py +22 -1
- mockstack-0.3.0/mockstack/intent.py +53 -0
- mockstack-0.3.0/mockstack/strategies/create_mixin.py +81 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/strategies/filefixtures.py +11 -101
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/strategies/proxyrules.py +26 -5
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/telemetry.py +0 -1
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/tests/conftest.py +1 -0
- {mockstack-0.2.0 → mockstack-0.3.0/mockstack.egg-info}/PKG-INFO +1 -1
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack.egg-info/SOURCES.txt +2 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/pyproject.toml +1 -1
- mockstack-0.2.0/.env.example +0 -7
- {mockstack-0.2.0 → mockstack-0.3.0}/.github/workflows/ci.yml +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/.github/workflows/publish-docs.yml +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/.github/workflows/publish-to-pypi.yml +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/.gitignore +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/.pre-commit-config.yaml +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/CODE_OF_CONDUCT.md +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/LICENSE +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/README.md +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/SECURITY.md +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/docs/assets/favicon.ico +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/docs/assets/logo.png +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/docs/assets/mockstack.png +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mkdocs.yml +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/__init__.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/constants.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/display.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/identifiers.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/lifespan.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/main.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/middleware.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/routers/__init__.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/routers/catchall.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/routers/homepage.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/strategies/__init__.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/strategies/base.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/strategies/factory.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/templating.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/tests/__init__.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/tests/fixtures/proxyrules.yml +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/tests/fixtures/templates/__init__.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/tests/fixtures/templates/example-template.j2 +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/tests/routers/__init__.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/tests/routers/test_catchall.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/tests/routers/test_homepage.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/tests/strategies/test_filefixtures.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/tests/strategies/test_proxyrules.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/tests/test_display.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/tests/test_identifiers.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/tests/test_middleware.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack/tests/test_templating.py +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack.egg-info/dependency_links.txt +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack.egg-info/entry_points.txt +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack.egg-info/requires.txt +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/mockstack.egg-info/top_level.txt +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/setup.cfg +0 -0
- {mockstack-0.2.0 → mockstack-0.3.0}/uv.lock +0 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Example environment variables for mockstack
|
|
2
|
+
|
|
3
|
+
# the strategy to use
|
|
4
|
+
MOCKSTACK__STRATEGY = filefixtures
|
|
5
|
+
|
|
6
|
+
# strategy-specific settings
|
|
7
|
+
|
|
8
|
+
# base dir for all templates. Used by 'filefixtures' strategy
|
|
9
|
+
# and other hybrid strategies utilizing templates.
|
|
10
|
+
MOCKSTACK__TEMPLATES_DIR = "/some/path/templates/"
|
|
11
|
+
|
|
12
|
+
# rules table for the proxyrules strategy.
|
|
13
|
+
# see .mockstack/tests/fixtures/ for examples.
|
|
14
|
+
MOCKSTACK__PROXYRULES_RULES_FILENAME="/some/path/proxyrules.yml"
|
|
15
|
+
|
|
16
|
+
MOCKSTACK__PROXYRULES_SIMULATE_CREATE_ON_MISSING = true
|
|
17
|
+
|
|
18
|
+
# OpenTelemetry settings
|
|
19
|
+
MOCKSTACK__OPENTELEMETRY__ENABLED = true
|
|
20
|
+
MOCKSTACK__OPENTELEMETRY__CAPTURE_RESPONSE_BODY = true
|
|
@@ -49,6 +49,10 @@ class Settings(BaseSettings):
|
|
|
49
49
|
# or reverse proxy the request to the target URL "silently".
|
|
50
50
|
proxyrules_redirect_via: ProxyRulesRedirectVia = ProxyRulesRedirectVia.REVERSE_PROXY
|
|
51
51
|
|
|
52
|
+
# controls behavior of proxying. Whether to simulate creation of resources
|
|
53
|
+
# when a POST request is made to a resource that doesn't match any rules..
|
|
54
|
+
proxyrules_simulate_create_on_missing: bool = False
|
|
55
|
+
|
|
52
56
|
# metadata fields to inject into created resources.
|
|
53
57
|
# A few template fields are available. See documentation for more details.
|
|
54
58
|
created_resource_metadata: dict[str, Any] = {
|
|
@@ -74,7 +78,8 @@ class Settings(BaseSettings):
|
|
|
74
78
|
"disable_existing_loggers": False,
|
|
75
79
|
"formatters": {
|
|
76
80
|
"standard": {
|
|
77
|
-
"
|
|
81
|
+
"()": "uvicorn.logging.DefaultFormatter",
|
|
82
|
+
"fmt": "%(levelprefix)s %(message)s",
|
|
78
83
|
},
|
|
79
84
|
},
|
|
80
85
|
"handlers": {
|
|
@@ -86,10 +91,26 @@ class Settings(BaseSettings):
|
|
|
86
91
|
},
|
|
87
92
|
},
|
|
88
93
|
"loggers": {
|
|
94
|
+
"uvicorn": {
|
|
95
|
+
"handlers": ["console"],
|
|
96
|
+
"level": "INFO",
|
|
97
|
+
"propagate": False,
|
|
98
|
+
},
|
|
89
99
|
"FileFixturesStrategy": {
|
|
90
100
|
"handlers": ["console"],
|
|
91
101
|
"level": "INFO",
|
|
102
|
+
"propagate": False,
|
|
92
103
|
},
|
|
104
|
+
"ProxyRulesStrategy": {
|
|
105
|
+
"handlers": ["console"],
|
|
106
|
+
"level": "INFO",
|
|
107
|
+
"propagate": False,
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
"root": {
|
|
111
|
+
"handlers": ["console"],
|
|
112
|
+
"level": "INFO",
|
|
113
|
+
"propagate": False,
|
|
93
114
|
},
|
|
94
115
|
}
|
|
95
116
|
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""Helpers for deducing user intent from a request."""
|
|
2
|
+
|
|
3
|
+
from fastapi import Request
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def wants_json(request: Request) -> bool:
|
|
7
|
+
"""Check if the request wants JSON response."""
|
|
8
|
+
content_type = request.headers.get("Content-Type", "")
|
|
9
|
+
return any(
|
|
10
|
+
(
|
|
11
|
+
content_type.startswith("application/json"),
|
|
12
|
+
content_type.startswith("text/json"),
|
|
13
|
+
request.url.path.endswith(".json"),
|
|
14
|
+
)
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def looks_like_a_search(request: Request) -> bool:
|
|
19
|
+
"""Check if the request looks like a search.
|
|
20
|
+
|
|
21
|
+
This is a heuristic to try and identify cases where a POST
|
|
22
|
+
request is used for issuing a search rather than for creating
|
|
23
|
+
a new resource.
|
|
24
|
+
|
|
25
|
+
"""
|
|
26
|
+
return any(
|
|
27
|
+
(
|
|
28
|
+
request.url.path.endswith("_search"),
|
|
29
|
+
request.url.path.endswith("/search"),
|
|
30
|
+
request.url.path.endswith("_query"),
|
|
31
|
+
)
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def looks_like_a_command(request: Request) -> bool:
|
|
36
|
+
"""Check if the request looks like a command.
|
|
37
|
+
|
|
38
|
+
This is a heuristic to try and identify cases where a POST
|
|
39
|
+
request is used for issuing a command rather than for creating
|
|
40
|
+
a new resource.
|
|
41
|
+
"""
|
|
42
|
+
return any(
|
|
43
|
+
(
|
|
44
|
+
request.url.path.endswith("_command"),
|
|
45
|
+
request.url.path.endswith("/command"),
|
|
46
|
+
request.url.path.endswith("_request"),
|
|
47
|
+
request.url.path.endswith("/request"),
|
|
48
|
+
request.url.path.endswith("_run"),
|
|
49
|
+
request.url.path.endswith("/run"),
|
|
50
|
+
request.url.path.endswith("_execute"),
|
|
51
|
+
request.url.path.endswith("/execute"),
|
|
52
|
+
)
|
|
53
|
+
)
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""Create mixin class."""
|
|
2
|
+
|
|
3
|
+
from datetime import datetime, timezone
|
|
4
|
+
from uuid import uuid4
|
|
5
|
+
|
|
6
|
+
from fastapi import Request, Response, status
|
|
7
|
+
from fastapi.responses import JSONResponse
|
|
8
|
+
from jinja2 import Environment
|
|
9
|
+
|
|
10
|
+
from mockstack.intent import wants_json
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class CreateMixin:
|
|
14
|
+
"""A mixin for strategies that need to simulate creation of resources."""
|
|
15
|
+
|
|
16
|
+
async def _create(
|
|
17
|
+
self, request: Request, *, env: Environment, created_resource_metadata: dict
|
|
18
|
+
) -> Response:
|
|
19
|
+
if wants_json(request):
|
|
20
|
+
# We return a 201 CREATED response with the resource as the body,
|
|
21
|
+
# potentially injecting the resource ID into the response.
|
|
22
|
+
resource = await request.json()
|
|
23
|
+
|
|
24
|
+
return JSONResponse(
|
|
25
|
+
status_code=status.HTTP_201_CREATED,
|
|
26
|
+
content=self._content(
|
|
27
|
+
resource,
|
|
28
|
+
request=request,
|
|
29
|
+
env=env,
|
|
30
|
+
created_resource_metadata=created_resource_metadata,
|
|
31
|
+
),
|
|
32
|
+
)
|
|
33
|
+
else:
|
|
34
|
+
# We return a 201 CREATED response with an empty body.
|
|
35
|
+
return Response(
|
|
36
|
+
status_code=status.HTTP_201_CREATED,
|
|
37
|
+
content=None,
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
def _content(
|
|
41
|
+
self,
|
|
42
|
+
resource: dict,
|
|
43
|
+
*,
|
|
44
|
+
env: Environment,
|
|
45
|
+
request: Request,
|
|
46
|
+
created_resource_metadata: dict,
|
|
47
|
+
) -> dict:
|
|
48
|
+
"""Create a new resource given a request resource.
|
|
49
|
+
|
|
50
|
+
We use the request resource as the basis for the new resource.
|
|
51
|
+
We then inject an identifier into the resource if it doesn't already have one,
|
|
52
|
+
as well as any other metadata fields that are configured for the strategy.
|
|
53
|
+
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
def with_metadata(resource: dict, copy=True) -> dict:
|
|
57
|
+
"""Inject metadata fields into the resource."""
|
|
58
|
+
_resource = resource.copy() if copy else resource
|
|
59
|
+
for key, value in created_resource_metadata.items():
|
|
60
|
+
if isinstance(value, str):
|
|
61
|
+
_resource[key] = env.from_string(value).render(
|
|
62
|
+
self._metadata_context(request)
|
|
63
|
+
)
|
|
64
|
+
else:
|
|
65
|
+
_resource[key] = value
|
|
66
|
+
return _resource
|
|
67
|
+
|
|
68
|
+
return with_metadata(resource)
|
|
69
|
+
|
|
70
|
+
def _metadata_context(self, request: Request) -> dict:
|
|
71
|
+
"""Context for injecting metadata fields into resources.
|
|
72
|
+
|
|
73
|
+
Some care is needed to ensure that we only expose the minimum amount
|
|
74
|
+
of information here since templates are user-defined.
|
|
75
|
+
|
|
76
|
+
"""
|
|
77
|
+
return {
|
|
78
|
+
"utcnow": lambda: datetime.now(timezone.utc),
|
|
79
|
+
"uuid4": uuid4,
|
|
80
|
+
"request": request,
|
|
81
|
+
}
|
|
@@ -2,67 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
import logging
|
|
4
4
|
import os
|
|
5
|
-
from datetime import datetime, timezone
|
|
6
5
|
from pathlib import Path
|
|
7
|
-
from uuid import uuid4
|
|
8
6
|
|
|
9
7
|
from fastapi import HTTPException, Request, Response, status
|
|
10
|
-
from fastapi.responses import JSONResponse
|
|
11
8
|
from jinja2 import Environment, FileSystemLoader
|
|
12
9
|
|
|
13
10
|
from mockstack.config import Settings
|
|
14
11
|
from mockstack.display import ANSIColors
|
|
12
|
+
from mockstack.intent import (
|
|
13
|
+
looks_like_a_command,
|
|
14
|
+
looks_like_a_search,
|
|
15
|
+
)
|
|
15
16
|
from mockstack.strategies.base import BaseStrategy
|
|
17
|
+
from mockstack.strategies.create_mixin import CreateMixin
|
|
16
18
|
from mockstack.templating import (
|
|
17
19
|
iter_possible_template_arguments,
|
|
18
20
|
missing_template_detail,
|
|
19
21
|
)
|
|
20
22
|
|
|
21
23
|
|
|
22
|
-
|
|
23
|
-
"""Check if the media type is JSON."""
|
|
24
|
-
return media_type in ("application/json", "text/json")
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
def looks_like_a_search(request: Request) -> bool:
|
|
28
|
-
"""Check if the request looks like a search.
|
|
29
|
-
|
|
30
|
-
This is a heuristic to try and identify cases where a POST
|
|
31
|
-
request is used for issuing a search rather than for creating
|
|
32
|
-
a new resource.
|
|
33
|
-
|
|
34
|
-
"""
|
|
35
|
-
return any(
|
|
36
|
-
(
|
|
37
|
-
request.url.path.endswith("_search"),
|
|
38
|
-
request.url.path.endswith("/search"),
|
|
39
|
-
request.url.path.endswith("_query"),
|
|
40
|
-
)
|
|
41
|
-
)
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
def looks_like_a_command(request: Request) -> bool:
|
|
45
|
-
"""Check if the request looks like a command.
|
|
46
|
-
|
|
47
|
-
This is a heuristic to try and identify cases where a POST
|
|
48
|
-
request is used for issuing a command rather than for creating
|
|
49
|
-
a new resource.
|
|
50
|
-
"""
|
|
51
|
-
return any(
|
|
52
|
-
(
|
|
53
|
-
request.url.path.endswith("_command"),
|
|
54
|
-
request.url.path.endswith("/command"),
|
|
55
|
-
request.url.path.endswith("_request"),
|
|
56
|
-
request.url.path.endswith("/request"),
|
|
57
|
-
request.url.path.endswith("_run"),
|
|
58
|
-
request.url.path.endswith("/run"),
|
|
59
|
-
request.url.path.endswith("_execute"),
|
|
60
|
-
request.url.path.endswith("/execute"),
|
|
61
|
-
)
|
|
62
|
-
)
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
class FileFixturesStrategy(BaseStrategy):
|
|
24
|
+
class FileFixturesStrategy(BaseStrategy, CreateMixin):
|
|
66
25
|
"""Strategy for using file-based fixtures."""
|
|
67
26
|
|
|
68
27
|
logger = logging.getLogger("FileFixturesStrategy")
|
|
@@ -126,24 +85,11 @@ class FileFixturesStrategy(BaseStrategy):
|
|
|
126
85
|
request, status_code=status.HTTP_201_CREATED
|
|
127
86
|
)
|
|
128
87
|
else:
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
# potentially injecting the resource ID into the response.
|
|
135
|
-
resource = await request.json()
|
|
136
|
-
|
|
137
|
-
return JSONResponse(
|
|
138
|
-
status_code=status.HTTP_201_CREATED,
|
|
139
|
-
content=self._created(resource, request=request),
|
|
140
|
-
)
|
|
141
|
-
else:
|
|
142
|
-
# We return a 201 CREATEDresponse with an empty body.
|
|
143
|
-
return Response(
|
|
144
|
-
status_code=status.HTTP_201_CREATED,
|
|
145
|
-
content=None,
|
|
146
|
-
)
|
|
88
|
+
return await self._create(
|
|
89
|
+
request,
|
|
90
|
+
env=self.env,
|
|
91
|
+
created_resource_metadata=self.created_resource_metadata,
|
|
92
|
+
)
|
|
147
93
|
|
|
148
94
|
async def _get(self, request: Request) -> Response:
|
|
149
95
|
"""Apply the strategy for GET requests.
|
|
@@ -176,42 +122,6 @@ class FileFixturesStrategy(BaseStrategy):
|
|
|
176
122
|
"""Apply the strategy for PUT requests."""
|
|
177
123
|
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
|
178
124
|
|
|
179
|
-
def _created(self, resource: dict, *, request: Request) -> dict:
|
|
180
|
-
"""Create a new resource given a request resource.
|
|
181
|
-
|
|
182
|
-
We use the request resource as the basis for the new resource.
|
|
183
|
-
We then inject an identifier into the resource if it doesn't already have one,
|
|
184
|
-
as well as any other metadata fields that are configured for the strategy.
|
|
185
|
-
|
|
186
|
-
"""
|
|
187
|
-
|
|
188
|
-
def with_metadata(resource: dict, copy=True) -> dict:
|
|
189
|
-
"""Inject metadata fields into the resource."""
|
|
190
|
-
_resource = resource.copy() if copy else resource
|
|
191
|
-
for key, value in self.created_resource_metadata.items():
|
|
192
|
-
if isinstance(value, str):
|
|
193
|
-
_resource[key] = self.env.from_string(value).render(
|
|
194
|
-
self._metadata_context(request)
|
|
195
|
-
)
|
|
196
|
-
else:
|
|
197
|
-
_resource[key] = value
|
|
198
|
-
return _resource
|
|
199
|
-
|
|
200
|
-
return with_metadata(resource)
|
|
201
|
-
|
|
202
|
-
def _metadata_context(self, request: Request) -> dict:
|
|
203
|
-
"""Context for injecting metadata fields into resources.
|
|
204
|
-
|
|
205
|
-
Some care is needed to ensure that we only expose the minimum amount
|
|
206
|
-
of information here since templates are user-defined.
|
|
207
|
-
|
|
208
|
-
"""
|
|
209
|
-
return {
|
|
210
|
-
"utcnow": lambda: datetime.now(timezone.utc),
|
|
211
|
-
"uuid4": uuid4,
|
|
212
|
-
"request": request,
|
|
213
|
-
}
|
|
214
|
-
|
|
215
125
|
def _response_from_template(
|
|
216
126
|
self, request: Request, status_code: int = status.HTTP_200_OK
|
|
217
127
|
) -> Response:
|
|
@@ -10,12 +10,14 @@ import httpx
|
|
|
10
10
|
import yaml
|
|
11
11
|
from fastapi import Request, Response, status
|
|
12
12
|
from fastapi.responses import RedirectResponse
|
|
13
|
+
from jinja2 import Environment
|
|
13
14
|
from starlette.datastructures import Headers
|
|
14
15
|
|
|
15
16
|
from mockstack.config import Settings
|
|
16
17
|
from mockstack.constants import ProxyRulesRedirectVia
|
|
17
18
|
from mockstack.display import ANSIColors
|
|
18
19
|
from mockstack.strategies.base import BaseStrategy
|
|
20
|
+
from mockstack.strategies.create_mixin import CreateMixin
|
|
19
21
|
|
|
20
22
|
|
|
21
23
|
class Rule:
|
|
@@ -50,7 +52,7 @@ class Rule:
|
|
|
50
52
|
return re.sub(self.pattern, self.replacement, path)
|
|
51
53
|
|
|
52
54
|
|
|
53
|
-
class ProxyRulesStrategy(BaseStrategy):
|
|
55
|
+
class ProxyRulesStrategy(BaseStrategy, CreateMixin):
|
|
54
56
|
"""Strategy for using proxy rules."""
|
|
55
57
|
|
|
56
58
|
logger = logging.getLogger("ProxyRulesStrategy")
|
|
@@ -59,6 +61,12 @@ class ProxyRulesStrategy(BaseStrategy):
|
|
|
59
61
|
super().__init__(settings, *args, **kwargs)
|
|
60
62
|
self.rules_filename = settings.proxyrules_rules_filename
|
|
61
63
|
self.proxyrules_redirect_via = settings.proxyrules_redirect_via
|
|
64
|
+
self.proxyrules_simulate_create_on_missing = (
|
|
65
|
+
settings.proxyrules_simulate_create_on_missing
|
|
66
|
+
)
|
|
67
|
+
self.created_resource_metadata = settings.created_resource_metadata
|
|
68
|
+
|
|
69
|
+
self.env = Environment()
|
|
62
70
|
|
|
63
71
|
def __str__(self) -> str:
|
|
64
72
|
HIGHLIGHT = ANSIColors.HEADER
|
|
@@ -67,7 +75,8 @@ class ProxyRulesStrategy(BaseStrategy):
|
|
|
67
75
|
return (
|
|
68
76
|
f"{HIGHLIGHT}[proxyrules]{ENDC} "
|
|
69
77
|
f"rules_filename: {HIGHLIGHT}{self.rules_filename}{ENDC}. "
|
|
70
|
-
f"redirect_via: {HIGHLIGHT}{self.proxyrules_redirect_via}{ENDC}"
|
|
78
|
+
f"redirect_via: {HIGHLIGHT}{self.proxyrules_redirect_via}{ENDC}. "
|
|
79
|
+
f"simulate_create_on_missing: {HIGHLIGHT}{self.proxyrules_simulate_create_on_missing}{ENDC}"
|
|
71
80
|
)
|
|
72
81
|
|
|
73
82
|
@cached_property
|
|
@@ -91,8 +100,20 @@ class ProxyRulesStrategy(BaseStrategy):
|
|
|
91
100
|
async def apply(self, request: Request) -> Response:
|
|
92
101
|
rule = self.rule_for(request)
|
|
93
102
|
if rule is None:
|
|
94
|
-
self.logger.warning(
|
|
95
|
-
|
|
103
|
+
self.logger.warning(
|
|
104
|
+
f"No rule found for request: {request.method} {request.url.path}"
|
|
105
|
+
)
|
|
106
|
+
if self.proxyrules_simulate_create_on_missing:
|
|
107
|
+
self.logger.info(
|
|
108
|
+
"Simulating resource creation for missing rule for {request.method} {request.url.path}"
|
|
109
|
+
)
|
|
110
|
+
return await self._create(
|
|
111
|
+
request,
|
|
112
|
+
env=self.env,
|
|
113
|
+
created_resource_metadata=self.created_resource_metadata,
|
|
114
|
+
)
|
|
115
|
+
else:
|
|
116
|
+
return Response(status_code=status.HTTP_404_NOT_FOUND)
|
|
96
117
|
|
|
97
118
|
url = rule.apply(request)
|
|
98
119
|
self.logger.info(f"Redirecting to: {url}")
|
|
@@ -114,7 +135,7 @@ class ProxyRulesStrategy(BaseStrategy):
|
|
|
114
135
|
|
|
115
136
|
case _:
|
|
116
137
|
raise ValueError(
|
|
117
|
-
f"Invalid redirect via: {self.proxyrules_redirect_via}"
|
|
138
|
+
f"Invalid redirect via value: {self.proxyrules_redirect_via=}"
|
|
118
139
|
)
|
|
119
140
|
|
|
120
141
|
async def reverse_proxy(self, request: Request, url: str) -> Response:
|
|
@@ -5,7 +5,6 @@ from importlib import metadata
|
|
|
5
5
|
from fastapi import FastAPI, Request
|
|
6
6
|
from opentelemetry import trace
|
|
7
7
|
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
|
|
8
|
-
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
|
|
9
8
|
from opentelemetry.sdk.resources import Resource
|
|
10
9
|
from opentelemetry.sdk.trace import TracerProvider
|
|
11
10
|
from opentelemetry.sdk.trace.export import BatchSpanProcessor
|
|
@@ -37,6 +37,7 @@ def settings(templates_dir, proxyrules_rules_filename):
|
|
|
37
37
|
templates_dir=templates_dir,
|
|
38
38
|
proxyrules_rules_filename=proxyrules_rules_filename,
|
|
39
39
|
proxyrules_redirect_via=ProxyRulesRedirectVia.HTTP_TEMPORARY_REDIRECT,
|
|
40
|
+
proxyrules_simulate_create_on_missing=False,
|
|
40
41
|
opentelemetry=OpenTelemetrySettings(enabled=False),
|
|
41
42
|
)
|
|
42
43
|
|
|
@@ -19,6 +19,7 @@ mockstack/config.py
|
|
|
19
19
|
mockstack/constants.py
|
|
20
20
|
mockstack/display.py
|
|
21
21
|
mockstack/identifiers.py
|
|
22
|
+
mockstack/intent.py
|
|
22
23
|
mockstack/lifespan.py
|
|
23
24
|
mockstack/main.py
|
|
24
25
|
mockstack/middleware.py
|
|
@@ -35,6 +36,7 @@ mockstack/routers/catchall.py
|
|
|
35
36
|
mockstack/routers/homepage.py
|
|
36
37
|
mockstack/strategies/__init__.py
|
|
37
38
|
mockstack/strategies/base.py
|
|
39
|
+
mockstack/strategies/create_mixin.py
|
|
38
40
|
mockstack/strategies/factory.py
|
|
39
41
|
mockstack/strategies/filefixtures.py
|
|
40
42
|
mockstack/strategies/proxyrules.py
|
mockstack-0.2.0/.env.example
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|