proklung / request-log-bundle
用于创建和使用响应模拟的捆绑包。
Requires
- php: >=7.1
- psr/container: ^1.0
- symfony/config: ~4|~5
- symfony/console: ~4|~5
- symfony/dependency-injection: ^4.0 || ^5.0
- symfony/http-kernel: ^4.0 || ^5.0
Requires (Dev)
- proklung/phpunit-testing-tools: ^1.0
- symfony/filesystem: ~4|~5
This package is auto-updated.
Last update: 2024-09-29 05:56:34 UTC
README
该包是基于 mRocaRequestLogBundle 进行修改的,以满足个人需求,并添加了一些实验性的Bitrixon功能(结果不太稳定)。
为什么?
方便从外部API的响应生成功能测试的固定装置。
主要
- 向请求添加
x-generate-response-mock
标头 - 获取文件形式的模拟。如果模拟已存在,则返回其中的数据
已知限制
如果GET请求非常长 - 超过255个字符 - 则无法保存模拟。
Bitrixon事务
如果捆绑包不是从Bitrixon加载的,则相应的服务将从容器中删除。
想法 - 根据URL创建模拟,使用Bitrixon原生工具(或甚至静态页面)。
在捆绑包配置中(文件 /local/config/packages/request_log.yaml
)的 bitrix_uri_list
部分,指定要处理的页面的正则表达式模式。
安装
composer require proklung/request-log-bundle
原始文档
描述
此捆绑包允许将HTTP请求及其相关响应作为JSON文件记录。这些生成的JSON文件可以用作API模拟,以测试前端应用程序而无需运行API。
它是如何工作的?
在包含 x-generate-response-mock
标头的请求(Kernel::TERMINATE
事件)之后,会创建一个包含请求和响应的JSON文件。
示例
GET /categories
app/log/mocks/categories/GET__.json
{ "request": { "uri": "/categories", "method": "GET", "parameters": [], "content": "" }, "response": { "statusCode": 200, "contentType": "application/json", "content": { "@context": "/contexts/Category", "@id": "/categories", "hydra:member": [ {"name": "foo"}, {"name": "bar"} ] } } }
PUT /categories/1 {"foo": "bar"}
app/log/mocks/categories/PUT__1-a5e74.json
{ "request": { "uri": "/categories/1", "method": "PUT", "parameters": [], "content": { "foo": "bar" } }, "response": { "statusCode": 204, "contentType": "application/json", "content": "" } }
文件命名策略
所有文件都使用以下约定创建
uri/METHOD__segments{--sorted-query=string&others}{__<sha1_substr5(sortedJsonContent)>}{__<sha1_substr5(sortedPostParameters)>}.json
示例 :
The filenames query strings can be hashed by setting the `hash_query_params` option to `true`.
For example, `categories/GET__--order[bar]=desc&order[foo]=asc.json` will be `categories/GET__--b0324.json`
The filenames query strings with non-asssocitive arrays are not indexed by default : `?foo[]=bar`.
You can use the indexed format by setting the `use_indexed_associative_array` option to `true` : `?foo[0]=bar`.
如有必要,根据您的需求配置捆绑包(示例使用默认值)
# app/config/config_dev.yml request_log: mocks_dir: %kernel.logs_dir%/mocks/ hash_query_params: false use_indexed_associative_array: false # Битриксовые URL (нестабильный функционал) bitrix_uri_list: # - '/^\/clubs\/$/' # - '/^\/about\/$/' # - '/^\/xxx\/$/'
如果您正在使用NelmioCorsBundle或其他CORS保护,则必须在允许的头中添加头
nelmio_cors: defaults: allow_headers: ['x-generate-response-mock']
用法
请求和响应记录器并不总是激活。要记录请求,请将 x-generate-response-mock
标头添加到您的请求中
GET /categories HTTP/1.1
Host: api.my.domain
x-generate-response-mock: true
命令
有一些有用的命令可用于管理模拟
清除所有模拟
app/console mroca:request-log:clear
将模拟保存到目标目录
app/console mroca:request-log:dump /tmp/mocksdirtarget