Пример #1
0
 public function testRedirectsRequests()
 {
     $mock = new Mock();
     $history = new History();
     $mock->addMultiple(["HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect1\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect2\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"]);
     $client = new Client(['base_url' => 'http://test.com']);
     $client->getEmitter()->attach($history);
     $client->getEmitter()->attach($mock);
     $request = $client->createRequest('GET', '/foo');
     // Ensure "end" is called only once
     $called = 0;
     $request->getEmitter()->on('end', function () use(&$called) {
         $called++;
     });
     $response = $client->send($request);
     $this->assertEquals(200, $response->getStatusCode());
     $this->assertContains('/redirect2', $response->getEffectiveUrl());
     // Ensure that two requests were sent
     $requests = $history->getRequests(true);
     $this->assertEquals('/foo', $requests[0]->getPath());
     $this->assertEquals('GET', $requests[0]->getMethod());
     $this->assertEquals('/redirect1', $requests[1]->getPath());
     $this->assertEquals('GET', $requests[1]->getMethod());
     $this->assertEquals('/redirect2', $requests[2]->getPath());
     $this->assertEquals('GET', $requests[2]->getMethod());
     $this->assertEquals(1, $called);
 }
Пример #2
0
 public function testCookiesAreExtractedFromRedirectResponses()
 {
     $jar = new CookieJar();
     $cookie = new Cookie($jar);
     $history = new History();
     $mock = new Mock(["HTTP/1.1 302 Moved Temporarily\r\n" . "Set-Cookie: test=583551; Domain=www.foo.com; Expires=Wednesday, 23-Mar-2050 19:49:45 GMT; Path=/\r\n" . "Location: /redirect\r\n\r\n", "HTTP/1.1 200 OK\r\n" . "Content-Length: 0\r\n\r\n", "HTTP/1.1 200 OK\r\n" . "Content-Length: 0\r\n\r\n"]);
     $client = new Client(['base_url' => 'http://www.foo.com']);
     $client->getEmitter()->attach($cookie);
     $client->getEmitter()->attach($mock);
     $client->getEmitter()->attach($history);
     $client->get();
     $request = $client->createRequest('GET', '/');
     $client->send($request);
     $this->assertEquals('test=583551', $request->getHeader('Cookie'));
     $requests = $history->getRequests();
     // Confirm subsequent requests have the cookie.
     $this->assertEquals('test=583551', $requests[2]->getHeader('Cookie'));
     // Confirm the redirected request has the cookie.
     $this->assertEquals('test=583551', $requests[1]->getHeader('Cookie'));
 }
Пример #3
0
 public function testCanMockFailedFutureResponses()
 {
     $client = new Client(['base_url' => 'http://test.com']);
     $request = $client->createRequest('GET', '/', ['future' => true]);
     // The first mock will be a mocked future response.
     $future = self::createFuture(function () use($client) {
         // When dereferenced, we will set a mocked response and send
         // another request.
         $client->get('http://httpbin.org', ['events' => ['before' => function (BeforeEvent $e) {
             $e->intercept(new Response(404));
         }]]);
     });
     $mock = new Mock([$future]);
     $request->getEmitter()->attach($mock);
     $response = $client->send($request);
     $this->assertSame($future, $response);
     $this->assertFalse($this->readAttribute($response, 'isRealized'));
     try {
         $response->wait();
         $this->fail('Did not throw');
     } catch (RequestException $e) {
         $this->assertEquals(404, $e->getResponse()->getStatusCode());
     }
 }
Пример #4
0
 public function testCanCastToString()
 {
     $client = new Client(['base_url' => 'http://localhost/']);
     $h = new History();
     $client->getEmitter()->attach($h);
     $mock = new Mock(array(new Response(301, array('Location' => '/redirect1', 'Content-Length' => 0)), new Response(307, array('Location' => '/redirect2', 'Content-Length' => 0)), new Response(200, array('Content-Length' => '2'), Stream::factory('HI'))));
     $client->getEmitter()->attach($mock);
     $request = $client->createRequest('GET', '/');
     $client->send($request);
     $this->assertEquals(3, count($h));
     $h = str_replace("\r", '', $h);
     $this->assertContains("> GET / HTTP/1.1\nHost: localhost\nUser-Agent:", $h);
     $this->assertContains("< HTTP/1.1 301 Moved Permanently\nLocation: /redirect1", $h);
     $this->assertContains("< HTTP/1.1 307 Temporary Redirect\nLocation: /redirect2", $h);
     $this->assertContains("< HTTP/1.1 200 OK\nContent-Length: 2\n\nHI", $h);
 }
Пример #5
0
 /**
  * @expectedException \GuzzleHttp5Legacy\Exception\RequestException
  * @expectedExceptionMessage Too many state transitions
  */
 public function testDetectsInfiniteLoops()
 {
     $client = new Client(['fsm' => $fsm = new RequestFsm(function () {
         return new CompletedFutureArray(['status' => 200]);
     }, new MessageFactory(), 3)]);
     $request = $client->createRequest('GET', 'http://foo.com:123');
     $request->getEmitter()->on('before', function () {
         throw new \Exception('foo');
     });
     $request->getEmitter()->on('error', function ($e) {
         $e->retry();
     });
     $client->send($request);
 }
Пример #6
0
 public function testReturnsFutureForResponseWhenRequested()
 {
     $client = new Client(['handler' => new MockHandler(['status' => 200])]);
     $request = $client->createRequest('GET', 'http://localhost:123/foo', ['future' => true]);
     $res = $client->send($request);
     $this->assertInstanceOf('GuzzleHttp5Legacy\\Message\\FutureResponse', $res);
     $this->assertEquals(200, $res->getStatusCode());
 }
 public function testCanAddEventsOnce()
 {
     $foo = 0;
     $client = new Client();
     $client->getEmitter()->attach(new Mock([new Response(200), new Response(200)]));
     $fn = function () use(&$foo) {
         ++$foo;
     };
     $request = $client->createRequest('GET', 'http://test.com', ['events' => ['before' => ['fn' => $fn, 'once' => true]]]);
     $client->send($request);
     $this->assertEquals(1, $foo);
     $client->send($request);
     $this->assertEquals(1, $foo);
 }
Пример #8
0
 public function testCanInterceptException()
 {
     $client = new Client(['base_url' => 'http://127.0.0.1:123']);
     $request = $client->createRequest('GET');
     $called = false;
     $request->getEmitter()->on('error', function (ErrorEvent $e) use(&$called) {
         $called = true;
         $e->intercept(new Response(200));
     });
     $request->getConfig()['timeout'] = 0.001;
     $request->getConfig()['connect_timeout'] = 0.001;
     $this->assertEquals(200, $client->send($request)->getStatusCode());
     $this->assertTrue($called);
 }