mroca/request-log-bundle

一个HTTP请求模拟生成symfony插件。

安装次数: 3,890

依赖项: 0

建议者: 0

安全: 0

星标: 3

关注者: 0

分支: 2

开放问题: 0

类型:symfony-bundle

v0.1.6 2016-04-04 12:48 UTC

This package is auto-updated.

Last update: 2024-09-12 19:27:32 UTC


README

一个HTTP请求模拟生成symfony插件。

Build Status SensioLabsInsight

描述

此插件允许将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`.

查看ResponseLoggerTest文件以获取更多示例。

安装

您可以使用Composer将插件作为开发依赖项安装到项目中

composer require --dev mroca/request-log-bundle

然后,通过更新您的app/config/AppKernel.php文件以启用插件

<?php
// app/config/AppKernel.php

public function registerBundles()
{
    //...
    if (in_array($this->getEnvironment(), ['dev', 'test'])) {
        //...
        $bundles[] = new Mroca\RequestLogBundle\RequestLogBundle();
    }

    return $bundles;
}

如有必要,根据您的需求配置插件(示例使用默认值)

# app/config/config_dev.yml

mroca_request_log:
    mocks_dir: %kernel.logs_dir%/mocks/
    hash_query_params: false
    use_indexed_associative_array: false

如果您使用的是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

开发

composer install

Php-cs-fixer

vendor/bin/php-cs-fixer fix

测试

vendor/bin/phpunit

待办事项

  • 使用此文件进行功能测试的Guzzle客户端
  • 使用此文件进行AngularJS端到端测试的Protractor客户端