Add mockapi source files and tests
This commit is contained in:
@@ -20,15 +20,7 @@ class DelayMiddleware(BaseHTTPMiddleware):
|
|||||||
min_delay: int = 100,
|
min_delay: int = 100,
|
||||||
max_delay: int = 2000,
|
max_delay: int = 2000,
|
||||||
):
|
):
|
||||||
"""Initialize the delay middleware.
|
"""Initialize the delay middleware."""
|
||||||
|
|
||||||
Args:
|
|
||||||
app: The ASGI application
|
|
||||||
delay: Fixed delay in milliseconds
|
|
||||||
random_delay: Use random delays
|
|
||||||
min_delay: Minimum delay in ms (for random mode)
|
|
||||||
max_delay: Maximum delay in ms (for random mode)
|
|
||||||
"""
|
|
||||||
super().__init__(app)
|
super().__init__(app)
|
||||||
self.delay = delay
|
self.delay = delay
|
||||||
self.random_delay = random_delay
|
self.random_delay = random_delay
|
||||||
@@ -50,16 +42,7 @@ class DelayMiddleware(BaseHTTPMiddleware):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def wrap(app, delay: int = 0, random_delay: bool = False) -> "DelayMiddleware":
|
def wrap(app, delay: int = 0, random_delay: bool = False) -> "DelayMiddleware":
|
||||||
"""Wrap an app with delay middleware.
|
"""Wrap an app with delay middleware."""
|
||||||
|
|
||||||
Args:
|
|
||||||
app: The application to wrap
|
|
||||||
delay: Fixed delay in milliseconds
|
|
||||||
random_delay: Use random delays
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Wrapped application
|
|
||||||
"""
|
|
||||||
return DelayMiddleware(app, delay=delay, random_delay=random_delay)
|
return DelayMiddleware(app, delay=delay, random_delay=random_delay)
|
||||||
|
|
||||||
|
|
||||||
@@ -71,21 +54,12 @@ class ErrorSimulator:
|
|||||||
error_probability: float = 0.0,
|
error_probability: float = 0.0,
|
||||||
default_error_code: int = 500,
|
default_error_code: int = 500,
|
||||||
):
|
):
|
||||||
"""Initialize the error simulator.
|
"""Initialize the error simulator."""
|
||||||
|
|
||||||
Args:
|
|
||||||
error_probability: Probability of returning an error (0.0 to 1.0)
|
|
||||||
default_error_code: Default HTTP error code
|
|
||||||
"""
|
|
||||||
self.error_probability = error_probability
|
self.error_probability = error_probability
|
||||||
self.default_error_code = default_error_code
|
self.default_error_code = default_error_code
|
||||||
|
|
||||||
def should_return_error(self) -> bool:
|
def should_return_error(self) -> bool:
|
||||||
"""Determine if an error should be returned.
|
"""Determine if an error should be returned."""
|
||||||
|
|
||||||
Returns:
|
|
||||||
True if error should be returned
|
|
||||||
"""
|
|
||||||
return random.random() < self.error_probability
|
return random.random() < self.error_probability
|
||||||
|
|
||||||
def get_error_response(
|
def get_error_response(
|
||||||
@@ -93,15 +67,7 @@ class ErrorSimulator:
|
|||||||
status_code: Optional[int] = None,
|
status_code: Optional[int] = None,
|
||||||
message: Optional[str] = None,
|
message: Optional[str] = None,
|
||||||
) -> tuple:
|
) -> tuple:
|
||||||
"""Get an error response tuple.
|
"""Get an error response tuple."""
|
||||||
|
|
||||||
Args:
|
|
||||||
status_code: HTTP status code
|
|
||||||
message: Error message
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Tuple of (body_dict, status_code)
|
|
||||||
"""
|
|
||||||
return (
|
return (
|
||||||
{"error": message or "Simulated error"},
|
{"error": message or "Simulated error"},
|
||||||
status_code or self.default_error_code,
|
status_code or self.default_error_code,
|
||||||
|
|||||||
Reference in New Issue
Block a user