GuzzleHttp\Tests\Server::enqueue PHP Method

enqueue() public static method

Any currently queued responses will be overwritten. Subsequent requests on the server will return queued responses in FIFO order.
public static enqueue ( array | Psr\Http\Message\ResponseInterface $responses )
$responses array | Psr\Http\Message\ResponseInterface A single or array of Responses to queue.
    public static function enqueue($responses)
    {
        $data = [];
        foreach ((array) $responses as $response) {
            if (!$response instanceof ResponseInterface) {
                throw new \Exception('Invalid response given.');
            }
            $headers = array_map(function ($h) {
                return implode(' ,', $h);
            }, $response->getHeaders());
            $data[] = ['status' => (string) $response->getStatusCode(), 'reason' => $response->getReasonPhrase(), 'headers' => $headers, 'body' => base64_encode((string) $response->getBody())];
        }
        self::getClient()->request('PUT', 'guzzle-server/responses', ['json' => $data]);
    }

Usage Example

Ejemplo n.º 1
0
 public function testStripsFragmentFromHost()
 {
     Server::flush();
     Server::enqueue("HTTP/1.1 200 OK\r\n\r\nContent-Length: 0\r\n\r\n");
     // This will fail if the removal of the #fragment is not performed
     $url = Url::fromString(Server::$url)->setPath(null)->setFragment('foo');
     $client = new Client();
     $client->get($url);
 }
All Usage Examples Of GuzzleHttp\Tests\Server::enqueue