received() public static method

Get all of the received requests
public static received ( ) : Psr\Http\Message\ResponseInterface[]
return Psr\Http\Message\ResponseInterface[]
Example #1
0
 /**
  * @dataProvider hasBodyProvider
  */
 public function testSendsWithBody($method)
 {
     Server::flush();
     Server::enqueue([new Response(200)]);
     call_user_func("GuzzleHttp\\{$method}", Server::$url, ['headers' => ['foo' => 'bar'], 'body' => 'test', 'query' => ['a' => '1']]);
     $sent = Server::received(true)[0];
     $this->assertEquals(strtoupper($method), $sent->getMethod());
     $this->assertEquals('/?a=1', $sent->getResource());
     $this->assertEquals('bar', $sent->getHeader('foo'));
     $this->assertEquals('test', $sent->getBody());
 }
 public function testUsesContentLengthWhenOverInMemorySize()
 {
     Server::flush();
     Server::enqueue([new Response()]);
     $stream = Psr7\stream_for(str_repeat('.', 1000000));
     $handler = new CurlHandler();
     $request = new Request('PUT', Server::$url, ['Content-Length' => 1000000], $stream);
     $handler($request, [])->wait();
     $received = Server::received()[0];
     $this->assertEquals(1000000, $received->getHeaderLine('Content-Length'));
     $this->assertFalse($received->hasHeader('Transfer-Encoding'));
 }
 public function testCanSendMagicAsyncRequests()
 {
     $client = new Client();
     Server::flush();
     Server::enqueue([new Response(200, ['Content-Length' => 2], 'hi')]);
     $p = $client->getAsync(Server::$url, ['query' => ['test' => 'foo']]);
     $this->assertInstanceOf(PromiseInterface::class, $p);
     $this->assertEquals(200, $p->wait()->getStatusCode());
     $received = Server::received(true);
     $this->assertCount(1, $received);
     $this->assertEquals('test=foo', $received[0]->getUri()->getQuery());
 }
 public function testCachesResponses()
 {
     Server::enqueue([new Response(200, ['Vary' => 'Accept-Encoding,Cookie,X-Use-HHVM', 'Date' => 'Wed, 29 Oct 2014 20:52:15 GMT', 'Cache-Control' => 'private, s-maxage=0, max-age=0, must-revalidate', 'Last-Modified' => 'Wed, 29 Oct 2014 20:30:57 GMT', 'Age' => '1277']), new Response(304, ['Content-Type' => 'text/html; charset=UTF-8', 'Vary' => 'Accept-Encoding,Cookie,X-Use-HHVM', 'Date' => 'Wed, 29 Oct 2014 20:52:16 GMT', 'Cache-Control' => 'private, s-maxage=0, max-age=0, must-revalidate', 'Last-Modified' => 'Wed, 29 Oct 2014 20:30:57 GMT', 'Age' => '1278'])]);
     $client = new Client(['base_url' => Server::$url]);
     CacheSubscriber::attach($client);
     $history = new History();
     $client->getEmitter()->attach($history);
     $response1 = $client->get('/foo');
     $this->assertEquals(200, $response1->getStatusCode());
     $response2 = $client->get('/foo');
     $this->assertEquals(200, $response2->getStatusCode());
     $this->assertCount(2, Server::received());
     $last = $history->getLastResponse();
     $this->assertEquals('HIT from GuzzleCache', $last->getHeader('X-Cache-Lookup'));
     $this->assertEquals('HIT from GuzzleCache', $last->getHeader('X-Cache'));
 }
Example #5
0
 public function testDoesNotAlwaysAddContentType()
 {
     Server::flush();
     Server::enqueue(["HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"]);
     $client = new Client();
     $client->put(Server::$url . '/foo', ['body' => 'foo']);
     $request = Server::received(true)[0];
     $this->assertEquals('', $request->getHeader('Content-Type'));
 }
 public function testDoesNotAddContentTypeByDefault()
 {
     Server::flush();
     Server::enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
     $client = new Client(['base_url' => Server::$url, 'adapter' => new StreamAdapter(new MessageFactory())]);
     $client->put('/', ['body' => 'foo']);
     $requests = Server::received(true);
     $this->assertEquals('', $requests[0]->getHeader('Content-Type'));
     $this->assertEquals(3, $requests[0]->getHeader('Content-Length'));
 }
Example #7
0
 public function testCanSendPayloadWithGet()
 {
     Server::flush();
     Server::enqueue(["HTTP/1.1 200 OK\r\n\r\n"]);
     $request = new Request('GET', Server::$url, [], Stream::factory('foo'));
     $this->emit($request);
     $t = new Transaction(new Client(), $request);
     $f = new CurlFactory();
     $h = $f($t, new MessageFactory());
     curl_exec($h);
     curl_close($h);
     $sent = Server::received(true)[0];
     $this->assertEquals('foo', (string) $sent->getBody());
     $this->assertEquals(3, (string) $sent->getHeader('Content-Length'));
 }
 public function testSendsPostWithNoBodyOrDefaultContentType()
 {
     Server::flush();
     Server::enqueue([new Psr7\Response()]);
     $handler = new Handler\CurlMultiHandler();
     $request = new Psr7\Request('POST', Server::$url);
     $response = $handler($request, []);
     $response->wait();
     $received = Server::received()[0];
     $this->assertEquals('POST', $received->getMethod());
     $this->assertFalse($received->hasHeader('content-type'));
     $this->assertSame('0', $received->getHeaderLine('content-length'));
 }
 public function testStreamAttributeKeepsStreamOpen()
 {
     Server::flush();
     Server::enqueue("HTTP/1.1 200 OK\r\nFoo: Bar\r\nContent-Length: 8\r\n\r\nhi there");
     $client = new Client(['base_url' => Server::$url, 'adapter' => new StreamAdapter(new MessageFactory())]);
     $response = $client->put('/foo', ['headers' => ['Foo' => 'Bar'], 'body' => 'test', 'stream' => true]);
     $this->assertEquals(200, $response->getStatusCode());
     $this->assertEquals('OK', $response->getReasonPhrase());
     $this->assertEquals('8', $response->getHeader('Content-Length'));
     $body = $response->getBody();
     if (defined('HHVM_VERSION')) {
         $this->markTestIncomplete('HHVM has not implemented this?');
     }
     $this->assertEquals('http', $body->getMetadata()['wrapper_type']);
     $this->assertEquals(8, $body->getMetadata()['unread_bytes']);
     $this->assertEquals(Server::$url . 'foo', $body->getMetadata()['uri']);
     $this->assertEquals('hi', $body->read(2));
     $body->close();
     $sent = Server::received(true)[0];
     $this->assertEquals('PUT', $sent->getMethod());
     $this->assertEquals('/foo', $sent->getResource());
     $this->assertEquals('127.0.0.1:8125', $sent->getHeader('host'));
     $this->assertEquals('Bar', $sent->getHeader('foo'));
     $this->assertTrue($sent->hasHeader('user-agent'));
 }
 public function testAddsContentLengthEvenWhenEmpty()
 {
     $this->queueRes();
     $handler = new StreamHandler();
     $request = new Request('PUT', Server::$url, [], '');
     $handler($request, []);
     $req = Server::received()[0];
     $this->assertEquals(0, $req->getHeaderLine('Content-Length'));
 }
 /**
  * Test the resident_time calculation (RFC7234 4.2.3)
  */
 public function testAgeIsIncremented()
 {
     Server::enqueue([new Response(200, ['Date' => $this->date(), 'Cache-Control' => 'public, max-age=60', 'Age' => '59'], Stream::factory('Age is 59!')), new Response(200, ['Date' => $this->date(), 'Cache-Control' => 'public, max-age=60', 'Age' => '0'], Stream::factory('It works!'))]);
     $client = $this->setupClient();
     // First request : the response is cached
     $response1 = $client->get('/foo');
     $this->assertEquals(200, $response1->getStatusCode());
     $this->assertEquals('MISS from GuzzleCache', $response1->getHeader('X-Cache-Lookup'));
     $this->assertEquals('Age is 59!', $this->getResponseBody($response1));
     // Second request : cache hit, age is now 60
     sleep(1);
     $response2 = $client->get('/foo');
     $this->assertEquals(200, $response1->getStatusCode());
     $this->assertEquals('HIT from GuzzleCache', $response2->getHeader('X-Cache-Lookup'));
     // This request should not be valid anymore : age is 59 + 2 = 61 which is strictly greater than 60
     sleep(1);
     $response3 = $client->get('/foo');
     $this->assertEquals(200, $response3->getStatusCode());
     $this->assertEquals('MISS from GuzzleCache', $response3->getHeader('X-Cache-Lookup'));
     $this->assertEquals('It works!', $this->getResponseBody($response3));
     $this->assertCount(2, Server::received());
 }
Example #12
0
 public function testDecodesGzippedResponses()
 {
     Server::flush();
     Server::enqueue(["HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nfoo"]);
     $request = new Request('GET', Server::$url, ['Accept-Encoding' => '']);
     $this->emit($request);
     $t = new Transaction(new Client(), $request);
     $f = new CurlFactory();
     $h = $f($t, new MessageFactory());
     curl_exec($h);
     curl_close($h);
     $this->assertEquals('foo', $t->getResponse()->getBody());
     $sent = Server::received(true)[0];
     $this->assertContains('gzip', $sent->getHeader('Accept-Encoding'));
 }
Example #13
0
 public function testContentLengthIntegrationTest()
 {
     Server::flush();
     Server::enqueue([new Response(200)]);
     $client = new Client(['base_url' => Server::$url]);
     $this->assertEquals(200, $client->put('/', ['body' => 'test'])->getStatusCode());
     $request = Server::received(true)[0];
     $this->assertEquals('PUT', $request->getMethod());
     $this->assertEquals('4', $request->getHeader('Content-Length'));
     $this->assertEquals('test', (string) $request->getBody());
 }
 /**
  * Test that expired stale responses aren't returned.
  */
 public function testOnErrorStaleResponseExpired()
 {
     // These dates are in the past, so the responses will be expired.
     Server::enqueue([new Response(200, ['Date' => 'Wed, 29 Oct 2014 20:52:15 GMT', 'Cache-Control' => 'private, max-age=0, must-revalidate, stale-if-error=10', 'Last-Modified' => 'Wed, 29 Oct 2014 20:30:57 GMT']), new Response(503, ['Date' => 'Wed, 29 Oct 2014 20:55:15 GMT', 'Cache-Control' => 'private, s-maxage=0, max-age=0, must-revalidate', 'Last-Modified' => 'Wed, 29 Oct 2014 20:30:57 GMT'])]);
     $client = $this->setupClient();
     // Prime the cache.
     $response1 = $client->get('/foo');
     $this->assertEquals(200, $response1->getStatusCode());
     $this->assertEquals('Wed, 29 Oct 2014 20:52:15 GMT', $response1->getHeader('Date'));
     try {
         $client->get('/foo');
         $this->fail('503 was not thrown with an expired cache entry.');
     } catch (ServerException $e) {
         $this->assertEquals(503, $e->getCode());
         $this->assertEquals('Wed, 29 Oct 2014 20:55:15 GMT', $e->getResponse()->getHeader('Date'));
         $this->assertCount(2, Server::received());
     }
 }