exequiellares / mock-api
为添加模拟API功能而设计的Magento 2模块
1.1.0
2023-05-04 15:04 UTC
Requires
- php: ~7.4.0||~8.1.0
- ext-json: *
- magento/framework: 103.0.*
This package is auto-updated.
Last update: 2024-09-24 23:02:01 UTC
README
此模块将为当前Magento商店添加一个前端控制器,用于模拟API。
这对于测试需要外部API连接的新功能非常有用。
该模块添加了两个公共控制器
- POST {domain}/mockapi/
- GET {domain}/mockapi/
控制器响应应返回可配置的JSON数据。
控制器请求应接收一个Bearer token以验证请求。此token是可配置的。
该模块允许定义一组字段,请求体必须包含这些字段以验证请求并模拟Bad Request响应。
允许将接收到的请求记录到自定义文件。
安装
composer require exequiellares/magento2-mock-api
php bin/magento module:enable ExequielLares_MockApi
php bin/magento setup:upgrade
设置
模块配置可在以下位置找到
商店 > 配置 > 高级 > 开发者 > 模拟API
选项
- 启用:启用或禁用模块
- 启用日志记录:启用或禁用接收请求的日志记录
- 验证令牌:启用或禁用令牌验证。如果禁用,则忽略令牌。
- 令牌:用于验证请求的令牌。如果为空,则忽略令牌。
- GET 显示响应中的请求:如果启用,则GET请求将在响应中返回请求体。
- POST 显示响应中的请求:如果启用,则POST请求将在响应中返回请求体。
- 验证请求字段:如果启用,则请求体将根据下一个选项中定义的字段进行验证。
- 要验证的字段:要验证的字段列表。字段必须由逗号分隔。例如:id,currency,discount,total,shipping,name,address,city,state,postcode,country,amount,method
- 强制错误响应:如果启用,则响应将是一个错误响应。
- 错误消息:返回响应的错误消息。如果为空,则使用默认消息。
- 成功消息:返回响应的成功消息。如果为空,则使用默认消息。
示例用法
GET请求
curl --location --request GET 'https://local.magento2.com/mockapi/?XDEBUG_SESSION_START=PHPSTORM' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer tokenFalso1234' \
--header 'Cookie: PHPSESSID=c357851ccf74ce451fbd5457b81eb636; XDEBUG_SESSION=PHPSTORM; mage-messages=%5B%7B%22type%22%3A%22error%22%2C%22text%22%3A%22Invalid%20Form%20Key.%20Please%20refresh%20the%20page.%22%7D%2C%7B%22type%22%3A%22error%22%2C%22text%22%3A%22Invalid%20Form%20Key.%20Please%20refresh%20the%20page.%22%7D%2C%7B%22type%22%3A%22error%22%2C%22text%22%3A%22Invalid%20Form%20Key.%20Please%20refresh%20the%20page.%22%7D%2C%7B%22type%22%3A%22error%22%2C%22text%22%3A%22Invalid%20Form%20Key.%20Please%20refresh%20the%20page.%22%7D%5D; private_content_version=5918e325802932538802aac4b4c892eb' \
--data '{
"id": "000000001",
"currency": "USD",
"discount": "0.0000",
"total": "36.3900",
"shipping": {
"name": "Veronica Costello",
"address": "6146 Honey Bluff Parkway",
"city": "Calder",
"state": "Michigan",
"postcode": "49628-7978",
"country": "US",
"amount": "5.0000",
"method": "Flat Rate - Fixed",
}
}'
POST请求
curl --location 'https://local.magento2.com/mock_erp_api?XDEBUG_SESSION_START=PHPSTORM' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer tokenFalso1234' \
--header 'Cookie: PHPSESSID=c357851ccf74ce451fbd5457b81eb636; XDEBUG_SESSION=PHPSTORM; mage-messages=%5B%7B%22type%22%3A%22error%22%2C%22text%22%3A%22Invalid%20Form%20Key.%20Please%20refresh%20the%20page.%22%7D%2C%7B%22type%22%3A%22error%22%2C%22text%22%3A%22Invalid%20Form%20Key.%20Please%20refresh%20the%20page.%22%7D%2C%7B%22type%22%3A%22error%22%2C%22text%22%3A%22Invalid%20Form%20Key.%20Please%20refresh%20the%20page.%22%7D%2C%7B%22type%22%3A%22error%22%2C%22text%22%3A%22Invalid%20Form%20Key.%20Please%20refresh%20the%20page.%22%7D%5D; private_content_version=cea3ca278c04ca29a7d9ab339a2b572c' \
--data '{
"id": "000000001",
"currency": "USD",
"discount": "0.0000",
"total": "36.3900",
"shipping": {
"name": "Veronica Costello",
"address": "6146 Honey Bluff Parkway",
"city": "Calder",
"state": "Michigan",
"postcode": "49628-7978",
"country": "US",
"amount": "5.0000",
"method": "Flat Rate - Fixed",
}
}'
日志记录器
接收到的请求记录在:var/log/mock_api.log
下一个功能
- 允许在GET请求中配置响应体
- 允许在POST请求中配置响应体