<?php namespace Tests\Unit;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Exception\RequestException;
use Tests\TestCase;
class HttpTest extends TestCase
{
public function test_http()
{
// tell Guzzle not to send requests, but to instead return our mock responses, one for each
// request that we make
$this->fakesHttp([
new Response(200, ['X-Foo' => 'Bar'], 'Hello, World'),
new Response(202, ['Content-Length' => 0]),
new RequestException('Error Communicating with Server', new Request('GET', 'test'))
]);
// our third response generates an exception
$this->expectException(RequestException::class);
// execute some code which sends an Http request
$response1 = $this->app()->http()->client()->get('/');
$response2 = $this->app()->http()->client()->get('/foo');
$response3 = $this->app()->http()->client()->get('/bar');
// assert something about the requests that were sent
$this->assertHttpRequestSent(function ($request) {
return strval($request->getUri()) == '/' OR strval($request->getUri()) == '/foo';
});
// assert something about our responses
$this->assertEquals(200, $response1->getStatusCode());
$this->assertEquals(202, $response2->getStatusCode());
}
}