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')); }
public function testNestedFutureErrorsAreResolvedWhenSending() { $c = new Client(); $total = 3; Server::enqueue([new Response(500), new Response(501), new Response(502)]); $c->getEmitter()->on('error', function (ErrorEvent $e) use(&$total) { if (--$total) { $e->retry(); } }); try { $c->get(Server::$url); $this->fail('Did not throw!'); } catch (RequestException $e) { $this->assertEquals(502, $e->getResponse()->getStatusCode()); } }
public function testSaveToFile() { $filename = sys_get_temp_dir() . '/mock_test_' . uniqid(); $file = tmpfile(); $stream = new Stream(tmpfile()); $m = new Mock([new Response(200, [], Stream::factory('TEST FILENAME')), new Response(200, [], Stream::factory('TEST FILE')), new Response(200, [], Stream::factory('TEST STREAM'))]); $client = new Client(); $client->getEmitter()->attach($m); $client->get('/', ['save_to' => $filename]); $client->get('/', ['save_to' => $file]); $client->get('/', ['save_to' => $stream]); $this->assertFileExists($filename); $this->assertEquals('TEST FILENAME', file_get_contents($filename)); $meta = stream_get_meta_data($file); $this->assertFileExists($meta['uri']); $this->assertEquals('TEST FILE', file_get_contents($meta['uri'])); $this->assertFileExists($stream->getMetadata('uri')); $this->assertEquals('TEST STREAM', file_get_contents($stream->getMetadata('uri'))); unlink($filename); }
/** * @expectedException \GuzzleHttp5Legacy\Exception\BadResponseException * @expectedExceptionMessage Redirect URL, https://foo.com/redirect2, does not use one of the allowed redirect protocols: http */ public function testThrowsWhenRedirectingToInvalidUrlProtocol() { $mock = new Mock(["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: https://foo.com/redirect2\r\nContent-Length: 0\r\n\r\n"]); $client = new Client(); $client->getEmitter()->attach($mock); $client->get('http://www.example.com/foo', ['allow_redirects' => ['protocols' => ['http']]]); }
/** * @expectedException \GuzzleHttp5Legacy\Exception\ClientException */ public function testFullTransaction() { $client = new Client(); $client->getEmitter()->attach(new Mock([new Response(403)])); $client->get('http://httpbin.org'); }
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); }
public function testDoesNotInfinitelyRecurse() { $client = new Client(['handler' => function () { throw new \RuntimeException('No network access'); }]); $last = null; $client->getEmitter()->on('before', function (BeforeEvent $e) use(&$last) { $e->intercept(new Response(200)); if (function_exists('xdebug_get_stack_depth')) { if ($last) { $this->assertEquals($last, xdebug_get_stack_depth()); } else { $last = xdebug_get_stack_depth(); } } }); $requests = []; for ($i = 0; $i < 100; $i++) { $requests[] = $client->createRequest('GET', 'http://foo.com'); } $pool = new Pool($client, $requests); $pool->wait(); }
public function testTransitionsThroughErrorsInComplete() { $client = new Client(); $client->getEmitter()->attach(new Mock([new Response(200)])); $request = $client->createRequest('GET', 'http://ewfewwef.com'); $this->addListeners($request, $calls); $request->getEmitter()->once('complete', function (CompleteEvent $e) { throw new \Exception('foo'); }); try { $client->send($request); $this->fail('did not throw'); } catch (RequestException $e) { $this->assertContains('foo', $e->getMessage()); $this->assertEquals(['before', 'complete', 'error', 'end'], $calls); } }
public function testSendsAllInParallel() { $client = new Client(); $client->getEmitter()->attach(new Mock([new Response(200), new Response(201), new Response(202)])); $history = new History(); $client->getEmitter()->attach($history); $requests = [$client->createRequest('GET', 'http://test.com'), $client->createRequest('POST', 'http://test.com'), $client->createRequest('PUT', 'http://test.com')]; $client->sendAll($requests); $requests = array_map(function ($r) { return $r->getMethod(); }, $history->getRequests()); $this->assertContains('GET', $requests); $this->assertContains('POST', $requests); $this->assertContains('PUT', $requests); }
public function testCanForceMultipartUploadWithContentType() { $client = new Client(); $client->getEmitter()->attach(new Mock([new Response(200)])); $history = new History(); $client->getEmitter()->attach($history); $client->post('http://foo.com', ['headers' => ['Content-Type' => 'multipart/form-data'], 'body' => ['foo' => 'bar']]); $this->assertContains('multipart/form-data; boundary=', $history->getLastRequest()->getHeader('Content-Type')); $this->assertContains("Content-Disposition: form-data; name=\"foo\"\r\n\r\nbar", (string) $history->getLastRequest()->getBody()); }