2017 © Pedro Peláez
 

library http-replay

Library to replay and compare HTTP requets and responses

image

kagency/http-replay

Library to replay and compare HTTP requets and responses

  • Wednesday, January 28, 2015
  • by kore
  • Repository
  • 4 Watchers
  • 11 Stars
  • 43 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 2 % Grown

The README.md

HTTP Replay Test Helper

Travis Build Status, (*1)

This is a libaray to replay requests and responses in tests. It allows to filter the responses to remove values, one does not want to assert on., (*2)

Supported Tools

Supported recordings (Reader):, (*3)

Supported frameworks (MessageHandler):, (*4)

Workflow

The workflow for using this test helper could be:, (*5)

First record some HTTP interaction using MitmDump, executing something like this:, (*6)

mitmdump -P http://my-website:80/ -p 8080 --anticache -z -w recordFile.tns

The stored file can then be replayed against your application – in my case a Symfony2 stack – by implementing an integration test like this:, (*7)

$messageHandler = new MessageHandler\Symfony2();
$filter = new ResponseFilter\Dispatcher(array(
    new ResponseFilter\Json(),
    new ResponseFilter\Headers(array(
        'date',
        'etag',
    )),
));

$reader = new Reader\MitmDump($messageHandler);
$interactions = $reader->readInteractions('recordFile.tns');

foreach ($interactions as $interaction) {
    $actualResponse = $app->runRequest($interaction->request);

    $this->assertEquals(
        $filter->filterResponse($messageHandler->simplifyResponse($interaction->request, $interaction->response)),
        $filter->filterResponse($messageHandler->simplifyResponse($interaction->request, $actualResponse))
    );
}

This example only implements a very basic set of response filters. You can implement conditional response filters, which only act for certain request URLs and filter, for example, certain JSON properties out of the response., (*8)

This example assumes $app is some Symfony2 app, where the method runRequest() recieves a Request object and returns a Response object., (*9)

Response Filters

A list of currently implemented response filters, (*10)

Dispatcher

Takes an array of response filters to apply to the responses. You usually want to use this one to be able to easily apply a set of filters., (*11)

Json

Tries to parse JSON to make it easier to compare in tests. Also makes other response filters possible, like the JsonFilter filter., (*12)

Takes no arguments., (*13)

JsonFilter

Filters out a defined set of properties from a JSON response. Especially sensible, if your JSON response contains fields like dates or revisions., (*14)

Receives a list of properties to remove as a constructor argument., (*15)

Headers

Implements a blacklist for HTTP headers. Removes those headers from the response. This is especially useful to remove Date and Etag headers, for example., (*16)

Receives a list of headers to remove as a constructor argument., (*17)

MultipartMixed

Replaces the random boundary string in multipart/mixed responses, to make it possible to comapre them., (*18)

Takes no arguments., (*19)

ConditionalPathRegexp

Takes to arguments:, (*20)

  • Regular expression to match the requested path
  • Aggregate filter

Only applies the aggregate filter, if the regular expression matches the requested path., (*21)

Verify the Build

You can verify the build by running ant:, (*22)

ant

This installs all required tools using composer and then runs the tests and static source code verification. You might also run all the tools manually, like:, (*23)

vendor/bin/phpunit

The Versions