1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-24 18:13:00 +01:00
guzzle/tests/Server.php

108 lines
2.8 KiB
PHP
Raw Normal View History

<?php
namespace GuzzleHttp\Tests;
use GuzzleHttp\Client;
use GuzzleHttp\Message\MessageFactory;
use GuzzleHttp\Message\Response;
2014-03-24 21:10:05 -07:00
use GuzzleHttp\Message\ResponseInterface;
use GuzzleHttp\RingBridge;
use GuzzleHttp\Tests\Ring\Client\Server as TestServer;
/**
* Placeholder for the Guzzle-Ring-Client server that makes it easier to use.
*/
class Server
{
public static $url = 'http://127.0.0.1:8125/';
public static $port = 8125;
/**
* Queue an array of responses or a single response on the server.
*
* Any currently queued responses will be overwritten. Subsequent requests
* on the server will return queued responses in FIFO order.
*
2014-09-07 15:11:17 -07:00
* @param array $responses Responses to queue.
* @throws \Exception
*/
2014-09-07 15:11:17 -07:00
public static function enqueue(array $responses)
{
static $factory;
if (!$factory) {
$factory = new MessageFactory();
}
$data = [];
2014-09-07 15:11:17 -07:00
foreach ($responses as $response) {
// Create the response object from a string
if (is_string($response)) {
$response = $factory->fromMessage($response);
2014-03-24 21:10:05 -07:00
} elseif (!($response instanceof ResponseInterface)) {
throw new \Exception('Responses must be strings or Responses');
}
$data[] = self::convertResponse($response);
}
2014-09-07 15:11:17 -07:00
TestServer::enqueue($data);
}
/**
* Get all of the received requests
*
2012-05-16 00:55:45 -07:00
* @param bool $hydrate Set to TRUE to turn the messages into
* actual {@see RequestInterface} objects. If $hydrate is FALSE,
* requests will be returned as strings.
*
* @return array
2013-09-22 20:55:24 -07:00
* @throws \RuntimeException
*/
public static function received($hydrate = false)
{
$response = TestServer::received();
if ($hydrate) {
$c = new Client();
$factory = new MessageFactory();
$response = array_map(function($message) use ($factory, $c) {
return RingBridge::fromRingRequest($message);
}, $response);
}
return $response;
}
public static function flush()
{
TestServer::flush();
}
public static function stop()
2014-03-23 10:53:34 -07:00
{
TestServer::stop();
2014-03-23 10:53:34 -07:00
}
public static function wait($maxTries = 5)
{
TestServer::wait($maxTries);
}
public static function start()
{
TestServer::start();
}
2014-09-07 15:11:17 -07:00
private static function convertResponse(Response $response)
{
$headers = array_map(function ($h) {
return implode(', ', $h);
}, $response->getHeaders());
return [
'status' => $response->getStatusCode(),
'reason' => $response->getReasonPhrase(),
'headers' => $headers,
'body' => base64_encode((string) $response->getBody())
];
}
Breaking / Potentially breaking changes: 1. Adopting a marker interface for Guzzle exceptions. A. All exceptions emitted from Guzzle are now wrapped with a Guzzle namespaced exception. B. Guzzle\Common\GuzzleExceptionInterface was renamed to Guzzle\Common\GuzzleException C. Guzzle\Common\ExceptionCollection was renamed to Guzzle\Common\Exception\ExceptionCollection 2. Using Header objects for Request and Response objects A. When you call $request->getHeader('X'), you will get back a Guzzle\Http\Message\Header object that contains all of the headers that case insensitively match. This object can be cast to a string or iterated like an array. You can pass true in the second argument to retrieve the header as a string. B. Removing the old Guzzle\Common\Collection based searching arguments from most of the request and response header methods. All retrievals are case-insensitive and return Header objects. 3. Changing the two headers added by the cache plugin to just one header with key and ttl. 4. Changing Guzzle\Http\Message\Response::factory() to fromMessage(). 5. Removing the NullObject return value from ServiceDescriptions and instead simply returning null New Features / enhancements: 1. Adding Guzzle\Http\Message\AbstractMessage::addHeaders() 2. Making it simpler to create service descriptions using a unified factory method that delegates to other factories. 3. Better handling of ports and hosts in Guzzle\Http\Url Note: This is a noisy diff because I'm removing trailing whitespace and adding a new line at the end of each source file.
2012-04-21 00:23:07 -07:00
}