2017 © Pedro Peláez
 

library http-history-container

A container for Guzzle history middleware

image

webignition/http-history-container

A container for Guzzle history middleware

  • Tuesday, May 15, 2018
  • by webignition
  • Repository
  • 1 Watchers
  • 0 Stars
  • 713 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 294 % Grown

The README.md

Guzzle HTTP History Container

Helping you more easily test what your Guzzle HTTP client has been up to., (*1)

A container for Guzzle history middleware offering smashingly-nice access to:, (*2)

  • collections of HTTP transactions (requests plus responses)
  • requests made
  • responses received
  • request URLs

Oh also logging. Really useful when your Guzzle client under test is not executing within the same thread as your tests., (*3)

Usage

Basic Usage

<?php

use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use webignition\HttpHistoryContainer\Container;

$historyContainer = new Container();
$historyHandler = Middleware::history($historyContainer);
$handlerStack = HandlerStack::create($historyHandler);
$httpClient = new HttpClient(['handler' => $handlerStack]);

/// ... things happen ...

$historyContainer->getTransactions();
// an array of HttpTransaction

$historyContainer->getRequests();
// a Collection\RequestCollection

$historyContainer->getResponses();
// a Collection\ResponseCollection

foreach ($historyContainer as $transaction) {
    // $transaction is a HttpTransaction

    $request = $transaction->getRequest();
    // $request is a RequestInterface

    $response = $transaction->getResponse();
    // $response is a ResponseInterface
}

Details on Requests Sent

<?php

use webignition\HttpHistoryContainer\Container;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

$historyContainer = new Container();

// ... create and use Guzzle client ...

$transactions = $historyContainer->getTransactions();
// $transactions is a Collection\TransactionCollection

$requests = $transactions->getRequests();
// $requests is a Collection\RequestCollection

// RequestCollection is iterable
foreach ($requests as $request) {
    // $request is a RequestInterface
}

$urls = $requests->getUrls();
// $urls is a Collection\UrlCollection

// UrlCollection is iterable
foreach ($urls as $url) {
    // $url is a UriInterface
}

foreach ($urls->getAsStrings() as $urlString) {
    // convenient access to requested URLs as strings
}

Details on Responses Received

<?php

use webignition\HttpHistoryContainer\Container;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

$historyContainer = new Container();

// ... create and use Guzzle client ...

$transactions = $historyContainer->getTransactions();
// $transactions is a Collection\TransactionCollection

$responses = $transactions->getResponses();
// $responses is a Collection\ResponseCollection

// ResponseCollection is iterable
foreach ($responses as $response) {
    // $response is a ResponseInterface
}

Logging!

You may not be able to access your Guzzle history if the client under test is executing in a different thread from your tests., (*4)

A LoggableContainer allows you to record transactions as they happen to whatever Psr\Log\LoggerInterface instance you like., (*5)

The logging container wraps each HttpTransaction in a LoggableTransaction object which is serialized to JSON and output as a debug message., (*6)

LoggableTransaction::fromJson() lets you (in a somewhat slightly lossy manner) re-create transactions object from logged messages.`, (*7)

Logging Example

<?php

use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use webignition\HttpHistoryContainer\LoggableContainer;
use webignition\HttpHistoryContainer\Transaction\LoggableTransaction;

// Create the PSR logger
$path = '/path/to/log';
$stream = fopen($path, 'w+');

$logger = new Logger('');
$logHandler = new StreamHandler($stream);
$logHandler
    ->setFormatter(new LineFormatter('%message%' . "\n"));

$logger->pushHandler($logHandler);

// Create LoggableContainer
$container = new LoggableContainer($logger);

// ... create and use Guzzle client ...

$logContent = file_get_contents($path);
$logLines = array_filter(explode("\n", $logContent));

$loggedTransactions = [];
foreach ($logLines as $logLine) {
    $loggedTransactions[] = LoggableTransaction::fromJson($logLine);
}

// $loggedTransactions is now an array of LoggableTransaction
foreach ($loggedTransactions as $loggedTransaction) {
    $transaction = $loggedTransaction->getTransaction();    
    // $transaction is a HttpTransaction

    $request = $transaction->getRequest();
    // $request is a RequestInterface

    $response = $transaction->getResponse();
    // $response is a ResponseInterface
}

The Versions

15/05 2018

dev-master

9999999-dev https://github.com/webignition/http-history-container

A container for Guzzle history middleware

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jon Cram

middleware psr-7 container guzzle history guzzlehttp

15/05 2018

0.5

0.5.0.0 https://github.com/webignition/http-history-container

A container for Guzzle history middleware

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jon Cram

middleware psr-7 container guzzle history guzzlehttp

15/05 2018

dev-github-7

dev-github-7 https://github.com/webignition/http-history-container

A container for Guzzle history middleware

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jon Cram

middleware psr-7 container guzzle history guzzlehttp

12/05 2018

0.4

0.4.0.0 https://github.com/webignition/http-history-container

A container for Guzzle history middleware

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jon Cram

middleware psr-7 container guzzle history guzzlehttp

12/05 2018

dev-github-5

dev-github-5 https://github.com/webignition/http-history-container

A container for Guzzle history middleware

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jon Cram

middleware psr-7 container guzzle history guzzlehttp

02/04 2018

0.3

0.3.0.0 https://github.com/webignition/http-history-container

A container for Guzzle history middleware

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jon Cram

middleware psr-7 container guzzle history guzzlehttp

02/04 2018

0.2

0.2.0.0 https://github.com/webignition/http-history-container

A container for Guzzle history middleware

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jon Cram

middleware psr-7 container guzzle history guzzlehttp

29/03 2018

0.1

0.1.0.0 https://github.com/webignition/http-history-container

A container for Guzzle history middleware

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jon Cram

middleware psr-7 container guzzle history guzzlehttp