2017 © Pedro Peláez
 

symfony-bundle request-log-bundle

An HTTP requests mock generator symfony bundle.

image

mroca/request-log-bundle

An HTTP requests mock generator symfony bundle.

  • Monday, April 4, 2016
  • by mRoca
  • Repository
  • 0 Watchers
  • 2 Stars
  • 3,046 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 8 Versions
  • 1 % Grown

The README.md

MrocaRequestLogBundle

An HTTP requests mock generator symfony bundle., (*1)

Build Status SensioLabsInsight, (*2)

Description

This bundle allows to log HTTP requests and associated responses as json files. This generated json files can be used as API mock in order to test a front app without running the api., (*3)

How it works ?

After each request (Kernel::TERMINATE event) containing the x-generate-response-mock header, a json file is created containing the request and the response., (*4)

Examples :, (*5)

GET /categories, (*6)

app/log/mocks/categories/GET__.json, (*7)

{
    "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"}, (*8)

app/log/mocks/categories/PUT__1-a5e74.json, (*9)

{
    "request": {
        "uri": "/categories/1",
        "method": "PUT",
        "parameters": [],
        "content": {
            "foo": "bar"
        }
    },
    "response": {
        "statusCode": 204,
        "contentType": "application/json",
        "content": ""
    }
}

File naming strategy, (*10)

All files are created with the following convention :, (*11)

uri/METHOD__segments{--sorted-query=string&others}{__<sha1_substr5(sortedJsonContent)>}{__<sha1_substr5(sortedPostParameters)>}.json, (*12)

Examples :, (*13)

URL Filename
GET / GET__.json
GET /categories categories/GET__.json
GET /categories/1 categories/GET__1.json
GET /categories?search[category][]=foo categories/GET__--search%5Bcategory%5D%5B%5D=foo.json
GET /categories?order[foo]=asc&order[bar]=desc categories/GET__--order%5Bbar%5D=desc&order%5Bfoo%5D=asc.json
GET /categories?parent=/my/iri categories/GET__--parent=%2Fmy%2Firi.json
POST /categories PARAMS: foo1=bar1; foo2=bar2 categories/POST____3e038.json
POST /categories CONTENT: {"foo1":"bar1", "foo2":"bar2"} categories/POST____3e038.json
PUT /categories/1 CONTENT: {"foo2":"bar2", "foo1":"bar1"} categories/POST__1__3e038.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`.

See the ResponseLoggerTest file for more examples., (*14)

Installation

You can use Composer to install the bundle to your project as a dev dependency :, (*15)

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

Then, enable the bundle by updating your app/config/AppKernel.php file to enable the bundle:, (*16)

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

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

    return $bundles;
}

If necessary, configure the bundle to your needs (example with default values):, (*17)

# app/config/config_dev.yml

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

If your are using the NelmioCorsBundle or another CORS protection, you must add the header in the allowed ones :, (*18)

nelmio_cors:
    defaults:
        allow_headers: ['x-generate-response-mock']

Usage

The request & response logger is not always activated. To log a request, add the x-generate-response-mock header into your request :, (*19)

GET /categories HTTP/1.1
Host: api.my.domain
x-generate-response-mock: true

Commands

Some useful commands are available to manager your mocks :, (*20)

Clear all mocks

app/console mroca:request-log:clear 

Save mocks in a target directory

app/console mroca:request-log:dump /tmp/mocksdirtarget

Dev

composer install

Php-cs-fixer, (*21)

vendor/bin/php-cs-fixer fix

Testing, (*22)

vendor/bin/phpunit

TODO

  • A Guzzle client using this files for functionnals tests
  • A Protractor client using this files for AngularJS e2e tests

The Versions

04/04 2016

dev-master

9999999-dev https://github.com/mRoca/MrocaRequestLogBundle

An HTTP requests mock generator symfony bundle.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michel Roca

api mock bundle symfony request tests responses logs

04/04 2016

v0.1.6

0.1.6.0 https://github.com/mRoca/MrocaRequestLogBundle

An HTTP requests mock generator symfony bundle.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michel Roca

api mock bundle symfony request tests responses logs

10/03 2016

v0.1.5

0.1.5.0 https://github.com/mRoca/MrocaRequestLogBundle

An HTTP requests mock generator symfony bundle.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michel Roca

api mock bundle symfony request tests responses logs

10/03 2016

v0.1.4

0.1.4.0 https://github.com/mRoca/MrocaRequestLogBundle

An HTTP requests mock generator symfony bundle.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michel Roca

api mock bundle symfony request tests responses logs

10/03 2016

v0.1.3

0.1.3.0 https://github.com/mRoca/MrocaRequestLogBundle

An HTTP requests mock generator symfony bundle.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michel Roca

api mock bundle symfony request tests responses logs

09/03 2016

v0.1.2

0.1.2.0 https://github.com/mRoca/MrocaRequestLogBundle

An HTTP requests mock generator symfony bundle.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michel Roca

api mock bundle symfony request tests responses logs

05/01 2016

v0.1.1

0.1.1.0 https://github.com/mRoca/MrocaRequestLogBundle

An HTTP requests mock generator symfony bundle.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michel Roca

api mock bundle symfony request tests responses logs

05/01 2016

v0.1.0

0.1.0.0 https://github.com/mRoca/MrocaRequestLogBundle

An HTTP requests mock generator symfony bundle.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michel Roca

api mock bundle symfony request tests responses logs