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