raffaelecarelle/dynamic-api-mock-server
Composer 安装命令:
composer create-project raffaelecarelle/dynamic-api-mock-server
包简介
A PHP server that allows creating, saving, and sharing dynamic REST API mocks
README 文档
README
A PHP server that allows creating, saving, and sharing dynamic REST API mocks through a web interface and JSON.
Features
- Create mock endpoints for any HTTP method (GET, POST, PUT, DELETE, PATCH)
- Organize endpoints into projects
- Configure response status codes, headers, and body
- Create dynamic responses with randomized data
- Simulate network delays
- Share mock endpoints with others
- Import and export projects
Requirements
- PHP >= 8.1
- Composer
- Web server (Apache/Nginx) or PHP built-in server
- SQLite/MySQL/PostgreSQL
Installation
-
Clone the repository:
git clone https://github.com/raffaelecarelle/dynamic-api-mock-server.git cd dynamic-api-mock-server -
Install dependencies:
composer install
-
Configure the environment:
cp .env.dist .env
Edit the
.envfile to set your database connection and other settings. -
Run migrations:
php bin/migration.php
-
Start the server:
# Using PHP built-in server php -S localhost:8080 -t public # Or configure your Apache/Nginx to point to the public directory
-
Access the dashboard:
http://localhost:8080/dashboard
Usage
Creating a Project
- Go to the Projects page
- Click "Create Project"
- Enter a name and optional description
- Choose whether the project should be public or private
- Click "Create Project"
Creating a Mock Endpoint
- Go to the Mock Endpoints page
- Click "Create Mock Endpoint"
- Select your project
- Configure the endpoint (method, path, status code, etc.)
- Define the response body and headers
- Click "Create Mock Endpoint"
Using a Mock Endpoint
Your mock endpoint is now available at:
http://localhost:8080/mock/{project-name}/{endpoint-path}
For example, if your project is named "users-api" and your endpoint path is "/api/users", the URL would be:
http://localhost:8080/mock/users-api/api/users
Dynamic Responses
You can create dynamic responses by enabling the "Dynamic Response" option when creating or editing a mock endpoint. This allows you to:
- Generate random numbers, strings, booleans, and dates
- Use request parameters in the response
- Apply conditional logic based on request parameters
Dynamic rules are defined as an array of objects, each with a specific type and target:
Random Number
{
"type": "random_number",
"target": "data.id",
"min": 1,
"max": 1000
}
Generates a random number between min and max and sets it at the specified target path in the response body.
Random String
{
"type": "random_string",
"target": "data.token",
"length": 16,
"characters": "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
}
Generates a random string of the specified length using the provided characters.
Random Boolean
{
"type": "random_boolean",
"target": "data.active"
}
Generates a random boolean value (true or false).
Random Date
{
"type": "random_date",
"target": "data.created_at",
"format": "Y-m-d H:i:s",
"min_days": -30,
"max_days": 30
}
Generates a random date between min_days and max_days from the current date, formatted according to format.
Request Parameter
{
"type": "request_param",
"target": "data.user_id",
"param_type": "path",
"param_name": "id"
}
Uses a parameter from the request in the response. param_type can be "path", "query", or "body".
Conditional
{
"type": "conditional",
"target": "message",
"condition": {
"param_type": "query",
"param_name": "status",
"operator": "equals",
"value": "active"
},
"then": "User is active",
"else": "User is inactive"
}
Sets different values based on a condition. Operators include "equals", "not_equals", "greater_than", "less_than", and "contains".
Sharing Mock Endpoints
- Go to the Projects page
- Click the share button for the project you want to share
- Copy the share links
Shared mock endpoints can be accessed using:
http://localhost:8080/share/{token}/{endpoint-path}
API Reference
The server provides a RESTful API for managing projects and mock endpoints:
Projects API
GET /api/projects- List all projectsGET /api/projects/{id}- Get a projectPOST /api/projects- Create a projectPUT /api/projects/{id}- Update a projectDELETE /api/projects/{id}- Delete a projectPOST /api/projects/{id}/export- Export a projectPOST /api/projects/import- Import a projectGET /api/share/{token}- Get a project by share token
Mock Endpoints API
GET /api/mocks- List all mock endpointsGET /api/mocks?project_id={id}- List mock endpoints for a projectGET /api/mocks/{id}- Get a mock endpointPOST /api/mocks- Create a mock endpointPUT /api/mocks/{id}- Update a mock endpointDELETE /api/mocks/{id}- Delete a mock endpoint
Testing
The project includes a test script to verify the API functionality:
# Start the server php -S localhost:8080 -t public # In another terminal, run the tests php tests/api_test.php
The test script will:
- Create a test project
- Create a mock endpoint
- Test the mock endpoint
- Update the mock endpoint
- Test the updated mock endpoint
- Export the project
- Test the shared mock endpoint
- Delete the mock endpoint and project
- Import the project
- Clean up
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-22