/** * @covers Guzzle\Guzzle */ public function testGetDefaultUserAgent() { Guzzle::reset(); $version = curl_version(); $agent = sprintf('Guzzle/%s (PHP=%s; curl=%s; openssl=%s)', Guzzle::VERSION, \PHP_VERSION, $version['version'], $version['ssl_version']); $this->assertEquals($agent, Guzzle::getDefaultUserAgent()); // Get it from cache this time $this->assertEquals($agent, Guzzle::getDefaultUserAgent()); }
/** * @covers Guzzle\Service\Command\DynamicCommand */ public function testUsesDifferentLocations() { $client = new Client('http://www.tazmania.com/'); $command = $this->factory->factory('body', array('b' => 'my-data', 'q' => 'abc', 'h' => 'haha')); $request = $command->setClient($client)->prepare(); $this->assertEquals("PUT /?test=abc&i=test HTTP/1.1\r\n" . "Host: www.tazmania.com\r\n" . "User-Agent: " . Guzzle::getDefaultUserAgent() . "\r\n" . "Expect: 100-Continue\r\n" . "Content-Length: 29\r\n" . "X-Custom: haha\r\n" . "\r\n" . "begin_body::my-data::end_body", (string) $request); unset($command); unset($request); $command = $this->factory->factory('body', array('b' => 'my-data', 'q' => 'abc', 'h' => 'haha', 'i' => 'does not change the value because it\'s static')); $request = $command->setClient($client)->prepare(); $this->assertEquals("PUT /?test=abc&i=test HTTP/1.1\r\n" . "Host: www.tazmania.com\r\n" . "User-Agent: " . Guzzle::getDefaultUserAgent() . "\r\n" . "Expect: 100-Continue\r\n" . "Content-Length: 29\r\n" . "X-Custom: haha\r\n" . "\r\n" . "begin_body::my-data::end_body", (string) $request); }
/** * @covers Guzzle\Aws\S3\Command\Object\PutObject * @covers Guzzle\Aws\S3\Command\Object\AbstractRequestObject * @covers Guzzle\Aws\S3\Command\Object\AbstractRequestObjectPut */ public function testPutObject() { $command = new PutObject(); $command->setBucket('test')->setKey('key'); $command->setBody('data'); $command->getRequestHeaders()->set('x-amz-test', '123'); $command->setAcl(S3Client::ACL_PUBLIC_READ); $command->setStorageClass('STANDARD'); $client = $this->getServiceBuilder()->get('test.s3'); $this->setMockResponse($client, 'PutObjectResponse'); $client->execute($command); $request = (string) $command->getRequest(); $this->assertEquals('http://test.s3.amazonaws.com/key', $command->getRequest()->getUrl()); $this->assertEquals('PUT', $command->getRequest()->getMethod()); $this->assertFalse($this->compareHttpHeaders(array('Host' => 'test.s3.amazonaws.com', 'Date' => '*', 'Content-Length' => '4', 'Content-MD5' => '8d777f385d3dfec8815d20f7496026dc', 'Authorization' => '*', 'x-amz-test' => '123', 'x-amz-acl' => 'public-read', 'x-amz-storage-class' => 'STANDARD', 'User-Agent' => Guzzle::getDefaultUserAgent(), 'Expect' => '100-Continue'), $command->getRequestHeaders()->getAll())); $this->assertEquals('data', (string) $command->getRequest()->getBody()); }
/** * @covers Guzzle\Aws\S3\Command\Bucket\DeleteBucketPolicy */ public function testDeleteBucketPolicy() { $command = new DeleteBucketPolicy(); $this->assertSame($command, $command->setBucket('test')); $client = $this->getServiceBuilder()->get('test.s3'); $this->setMockResponse($client, 'DeleteBucketPolicyResponse'); $client->execute($command); // Ensure that the DELETE request was sent to the policy sub resource $this->assertEquals('http://test.s3.amazonaws.com/?policy', $command->getRequest()->getUrl()); $this->assertEquals('DELETE', $command->getRequest()->getMethod()); // Check the raw HTTP request message $request = explode("\r\n", (string) $command->getRequest()); $this->assertEquals('DELETE /?policy HTTP/1.1', $request[0]); $this->assertEquals('User-Agent: ' . Guzzle::getDefaultUserAgent(), $request[1]); $this->assertEquals('Host: test.s3.amazonaws.com', $request[2]); $this->assertContains("Date: ", $request[3]); $this->assertContains("Authorization: ", $request[4]); }
/** * @covers Guzzle\Aws\S3\Command\Object\HeadObject * @covers Guzzle\Aws\S3\Command\Object\AbstractRequestObject */ public function testHeadObject() { $command = new \Guzzle\Aws\S3\Command\Object\HeadObject(); $command->setBucket('test')->setKey('key'); $command->setRange('bytes=500-999'); $command->setIfMatch('abcd'); $command->setIfNoneMatch('efghi'); $command->setIfModifiedSince('Sat, 29 Oct 1994 19:43:31 GMT'); $command->setIfUnmodifiedSince('Sat, 29 Oct 1994 19:43:31 GMT'); $command->getRequestHeaders()->set('x-amz-test', '123'); $command->setVersionId('123'); $client = $this->getServiceBuilder()->get('test.s3'); $this->setMockResponse($client, 'HeadObjectResponse'); $client->execute($command); $this->assertEquals('http://test.s3.amazonaws.com/key?versionId=123', $command->getRequest()->getUrl()); $this->assertEquals('HEAD', $command->getRequest()->getMethod()); $this->assertFalse($this->compareHttpHeaders(array('Host' => 'test.s3.amazonaws.com', 'User-Agent' => Guzzle::getDefaultUserAgent(), 'Date' => '*', 'Authorization' => '*', 'Range' => 'bytes=500-999', 'If-Match' => 'abcd', 'If-None-Match' => 'efghi', 'If-Unmodified-Since' => 'Sat, 29 Oct 1994 19:43:31 GMT', 'If-Modified-Since' => 'Sat, 29 Oct 1994 19:43:31 GMT', 'x-amz-test' => '123'), $command->getRequest()->getHeaders()->getAll())); }
/** * @covers Guzzle\Aws\S3\Command\Object\InitiateMultipartUpload * @covers Guzzle\Aws\S3\Command\Object\AbstractRequestObject * @covers Guzzle\Aws\S3\Command\Object\AbstractRequestObjectPut */ public function testInitiate() { $command = new InitiateMultipartUpload(); $command->setBucket('example-bucket')->setKey('example-object'); $command->getRequestHeaders()->set('x-amz-test', '123'); $command->setAcl(S3Client::ACL_PUBLIC_READ); $command->setStorageClass('STANDARD'); $client = $this->getServiceBuilder()->get('test.s3'); $this->setMockResponse($client, 'InitiateMultipartUploadResponse'); $client->execute($command); $request = (string) $command->getRequest(); $this->assertEquals('http://example-bucket.s3.amazonaws.com/example-object?uploads', $command->getRequest()->getUrl()); $this->assertEquals('POST', $command->getRequest()->getMethod()); $this->assertFalse($this->compareHttpHeaders(array('Host' => 'example-bucket.s3.amazonaws.com', 'Date' => '*', 'Authorization' => '*', 'x-amz-test' => '123', 'x-amz-acl' => 'public-read', 'x-amz-storage-class' => 'STANDARD', 'User-Agent' => Guzzle::getDefaultUserAgent()), $command->getRequestHeaders()->getAll())); $this->assertInstanceOf('SimpleXMLElement', $command->getResult()); $this->assertEquals('example-bucket', (string) $command->getResult()->Bucket); $this->assertEquals('example-object', (string) $command->getResult()->Key); $this->assertEquals('VXBsb2FkIElEIGZvciA2aWWpbmcncyBteS1tb3ZpZS5tMnRzIHVwbG9hZA', (string) $command->getResult()->UploadId); }
/** * @covers Guzzle\Aws\S3\Command\Object\CopyObject */ public function testCopyObject() { $command = new \Guzzle\Aws\S3\Command\Object\CopyObject(); $command->setBucket('test')->setKey('key'); $this->assertSame($command, $command->setCopySource('source_bucket', 'source_key')); $this->assertSame($command, $command->setAcl(\Guzzle\Aws\S3\S3Client::ACL_PUBLIC_READ)); $this->assertSame($command, $command->setStorageClass('STANDARD')); $this->assertSame($command, $command->setMetadataDirective('COPY')); $this->assertSame($command, $command->setCopySourceIfMatch('match_etag')); $this->assertSame($command, $command->setCopySourceIfNoneMatch('none_match_etag')); $this->assertSame($command, $command->setCopySourceIfModifiedSince('now')); $this->assertSame($command, $command->setCopySourceIfUnmodifiedSince('now')); $client = $this->getServiceBuilder()->get('test.s3'); $this->setMockResponse($client, 'CopyObjectResponse'); $client->execute($command); $request = (string) $command->getRequest(); $this->assertEquals('http://test.s3.amazonaws.com/key', $command->getRequest()->getUrl()); $this->assertEquals('PUT', $command->getRequest()->getMethod()); $this->assertFalse($this->compareHttpHeaders(array('Host' => 'test.s3.amazonaws.com', 'Date' => '*', 'Content-Length' => '4', 'Content-MD5' => '8d777f385d3dfec8815d20f7496026dc', 'Authorization' => '*', 'x-amz-test' => '123', 'x-amz-acl' => 'public-read', 'x-amz-storage-class' => 'STANDARD', 'x-amz-copy-source' => '/source_bucket/source_key', 'x-amz-metadata-directive' => 'COPY', 'x-amz-copy-source-if-match' => 'match_etag', 'x-amz-copy-source-if-none-match' => 'none_match_etag', 'x-amz-copy-source-if-modified-since' => Guzzle::getHttpDate('now'), 'x-amz-copy-source-if-unmodified-since' => Guzzle::getHttpDate('now'), 'User-Agent' => Guzzle::getDefaultUserAgent()), $command->getRequestHeaders()->getAll())); }
public function testLogsWhenExceptionsAreThrown() { $client = new Client($this->getServer()->getUrl()); $plugin = new LogPlugin($this->logAdapter, LogPlugin::LOG_VERBOSE); $client->getEventDispatcher()->addSubscriber($plugin); $request = $client->get(); $this->getServer()->enqueue("HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"); ob_start(); try { $request->send(); $this->fail('Exception for 404 was not thrown'); } catch (\Exception $e) { } $message = ob_get_clean(); $this->assertContains('127.0.0.1 - "GET / HTTP/1.1" - 404 0 - ', $message); $this->assertContains("GET / HTTP/1.1\r\n", $message); $this->assertContains("User-Agent: " . Guzzle::getDefaultUserAgent(), $message); $this->assertContains("\n< HTTP/1.1 404 Not Found\r\n< Content-Length: 0", $message); }
/** * Set the name of your application and application version that will be * appended to the User-Agent header of all reqeusts. * * @param string $userAgent User agent string * @param bool $includeDefault (optional) Set to TRUE to append the default * Guzzle user agent * * @return Client */ public function setUserAgent($userAgent, $includeDefault = false) { if ($includeDefault) { $userAgent .= ' ' . Guzzle::getDefaultUserAgent(); } $this->defaultHeaders->set('User-Agent', $userAgent); return $this; }
/** * Create a new request * * @param string $method HTTP method * @param string|Url $url HTTP URL to connect to. The URI scheme, host * header, and URI are parsed from the full URL. If query string * parameters are present they will be parsed as well. * @param array|Collection $headers (optional) HTTP headers */ public function __construct($method, $url, $headers = array()) { $this->method = strtoupper($method); $this->curlOptions = new Collection(); $this->params = new Collection(); $this->setUrl($url); if ($headers) { // Special handling for multi-value headers foreach ($headers as $key => $value) { $lkey = strtolower($key); // Deal with collisions with Host and Authorization if ($lkey == 'host' || $lkey == 'authorization') { $this->setHeader($key, $value); } else { foreach ((array) $value as $v) { $this->addHeader($key, $v); } } } } if (!$this->hasHeader('User-Agent', true)) { $this->setHeader('User-Agent', Guzzle::getDefaultUserAgent()); } $this->cookie = Cookie::factory($this->getHeader('Cookie')); $this->setState(self::STATE_NEW); }
/** * This test launches a dummy Guzzle\Http\Server\Server object that listens * for incoming requests. The server allows us to test how cURL sends * requests and receives responses. We can validate the request structure * and whether or not the response was interpreted correctly. * * @covers Guzzle\Http\Message\Request */ public function testRequestCanBeSentUsingCurl() { $this->getServer()->flush(); $this->getServer()->enqueue(array("HTTP/1.1 200 OK\r\nContent-Length: 4\r\nExpires: Thu, 01 Dec 1994 16:00:00 GMT\r\nConnection: close\r\n\r\ndata", "HTTP/1.1 200 OK\r\nContent-Length: 4\r\nExpires: Thu, 01 Dec 1994 16:00:00 GMT\r\nConnection: close\r\n\r\ndata", "HTTP/1.1 404 Not Found\r\nContent-Encoding: application/xml\r\nContent-Length: 48\r\n\r\n<error><mesage>File not found</message></error>")); $request = RequestFactory::getInstance()->create('GET', $this->getServer()->getUrl()); $request->setClient($this->client); $response = $request->send(); $this->assertEquals('data', $response->getBody(true)); $this->assertEquals(200, (int) $response->getStatusCode()); $this->assertEquals('OK', $response->getReasonPhrase()); $this->assertEquals(4, $response->getContentLength()); $this->assertEquals('Thu, 01 Dec 1994 16:00:00 GMT', $response->getExpires()); // Test that the same handle can be sent twice without setting state to new $response2 = $request->send(); $this->assertNotSame($response, $response2); try { $request = RequestFactory::getInstance()->create('GET', $this->getServer()->getUrl() . 'index.html'); $request->setClient($this->client); $response = $request->send(); $this->fail('Request did not receive a 404 response'); } catch (BadResponseException $e) { } $requests = $this->getServer()->getReceivedRequests(true); $messages = $this->getServer()->getReceivedRequests(false); $port = $this->getServer()->getPort(); $userAgent = Guzzle::getDefaultUserAgent(); $this->assertEquals('127.0.0.1:' . $port, $requests[0]->getHeader('Host')); $this->assertEquals('127.0.0.1:' . $port, $requests[1]->getHeader('Host')); $this->assertEquals('127.0.0.1:' . $port, $requests[2]->getHeader('Host')); $this->assertEquals('/', $requests[0]->getPath()); $this->assertEquals('/', $requests[1]->getPath()); $this->assertEquals('/index.html', $requests[2]->getPath()); $parts = explode("\r\n", $messages[0]); $this->assertEquals('GET / HTTP/1.1', $parts[0]); $parts = explode("\r\n", $messages[1]); $this->assertEquals('GET / HTTP/1.1', $parts[0]); $parts = explode("\r\n", $messages[2]); $this->assertEquals('GET /index.html HTTP/1.1', $parts[0]); }
/** * @covers Guzzle\Http\Client::setUserAgent * @covers Guzzle\Http\Client::createRequest * @covers Guzzle\Http\Client::prepareRequest */ public function testSetsUserAgent() { $client = new Client('http://www.test.com/', array('api' => 'v1')); $this->assertSame($client, $client->setUserAgent('Test/1.0Ab', true)); $this->assertEquals('Test/1.0Ab ' . Guzzle::getDefaultUserAgent(), $client->get()->getHeader('User-Agent')); $client->setUserAgent('Test/1.0Ab'); $this->assertEquals('Test/1.0Ab', $client->get()->getHeader('User-Agent')); }
/** * Data provider for factory tests * * @return array */ public function dataProvider() { $testFile = __DIR__ . '/../../../../../phpunit.xml'; $postBody = new QueryString(array('file' => '@' . $testFile)); $qs = new QueryString(array('x' => 'y', 'z' => 'a')); $userAgent = Guzzle::getDefaultUserAgent(); $auth = base64_encode('michael:123'); $testFileSize = filesize($testFile); return array(array('GET', 'http://www.google.com/', null, null, array(CURLOPT_RETURNTRANSFER => 0, CURLOPT_HEADER => 0, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_MAXREDIRS => 5, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_USERAGENT => $userAgent, CURLOPT_WRITEFUNCTION => 'callback', CURLOPT_HEADERFUNCTION => 'callback', CURLOPT_ENCODING => '', CURLOPT_HTTPHEADER => array('Host: www.google.com', 'User-Agent: ' . $userAgent))), array('TRACE', 'http://www.google.com/', null, null, array(CURLOPT_CUSTOMREQUEST => 'TRACE')), array('GET', 'http://127.0.0.1:8080', null, null, array(CURLOPT_RETURNTRANSFER => 0, CURLOPT_HEADER => 0, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_MAXREDIRS => 5, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_USERAGENT => $userAgent, CURLOPT_WRITEFUNCTION => 'callback', CURLOPT_HEADERFUNCTION => 'callback', CURLOPT_ENCODING => '', CURLOPT_PORT => 8080, CURLOPT_HTTPHEADER => array('Host: 127.0.0.1:8080', 'User-Agent: ' . $userAgent))), array('HEAD', 'http://www.google.com/', null, null, array(CURLOPT_RETURNTRANSFER => 0, CURLOPT_HEADER => 0, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_MAXREDIRS => 5, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_USERAGENT => $userAgent, CURLOPT_HEADERFUNCTION => 'callback', CURLOPT_ENCODING => '', CURLOPT_HTTPHEADER => array('Host: www.google.com', 'User-Agent: ' . $userAgent), CURLOPT_NOBODY => 1)), array('GET', 'https://*****:*****@localhost/index.html?q=2', null, null, array(CURLOPT_RETURNTRANSFER => 0, CURLOPT_HEADER => 0, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_MAXREDIRS => 5, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_USERAGENT => $userAgent, CURLOPT_WRITEFUNCTION => 'callback', CURLOPT_HEADERFUNCTION => 'callback', CURLOPT_ENCODING => '', CURLOPT_HTTPHEADER => array('Host: localhost', 'Authorization: Basic ' . $auth, 'User-Agent: ' . $userAgent), CURLOPT_PORT => 443)), array('GET', 'http://*****:*****@' . $testFile), CURLOPT_HTTPHEADER => array('Host: localhost:8124', 'User-Agent: ' . $userAgent, 'Expect: 100-Continue', 'Content-Type: multipart/form-data')), array('_Accept' => '*', '_Accept-Encoding' => '*', 'Host' => '*', 'User-Agent' => '*', 'Content-Length' => '*', 'Expect' => '100-Continue', 'Content-Type' => 'multipart/form-data; boundary=*', '!Transfer-Encoding' => null)), array('POST', 'http://localhost:8124/post.php', array('Content-Type' => 'application/json'), '{"hi":"there"}', array(CURLOPT_RETURNTRANSFER => 0, CURLOPT_HEADER => 0, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_MAXREDIRS => 5, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_USERAGENT => $userAgent, CURLOPT_WRITEFUNCTION => 'callback', CURLOPT_HEADERFUNCTION => 'callback', CURLOPT_ENCODING => '', CURLOPT_POST => 1, CURLOPT_HTTPHEADER => array('Host: localhost:8124', 'User-Agent: ' . $userAgent, 'Expect: 100-Continue', 'Content-Type: application/json', 'Content-Length: 14')), array('_Accept-Encoding' => '*', '_Accept' => '*', 'Host' => '*', 'User-Agent' => '*', 'Content-Type' => 'application/json', 'Expect' => '100-Continue', 'Content-Length' => '14', '!Transfer-Encoding' => null)), array('POST', 'http://localhost:8124/post.php', array('Content-Type' => 'application/json', 'Transfer-Encoding' => 'chunked'), '{"hi":"there"}', array(CURLOPT_RETURNTRANSFER => 0, CURLOPT_HEADER => 0, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_MAXREDIRS => 5, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_USERAGENT => $userAgent, CURLOPT_WRITEFUNCTION => 'callback', CURLOPT_HEADERFUNCTION => 'callback', CURLOPT_ENCODING => '', CURLOPT_POST => 1, CURLOPT_HTTPHEADER => array('Host: localhost:8124', 'User-Agent: ' . $userAgent, 'Expect: 100-Continue', 'Content-Type: application/json', 'Transfer-Encoding: chunked')), array('_Accept-Encoding' => '*', '_Accept' => '*', 'Host' => '*', 'User-Agent' => '*', 'Content-Type' => 'application/json', 'Expect' => '100-Continue', 'Transfer-Encoding' => 'chunked', '!Content-Length' => '')), array('POST', 'http://localhost:8124/foo.php', null, null, array(CURLOPT_HTTPHEADER => array('Expect:', 'Host: localhost:8124', 'User-Agent: ' . $userAgent, 'Content-Length: 0')), array('_Accept' => '*', '_Accept-Encoding' => '*', 'Host' => '*', 'User-Agent' => '*', 'Content-Length' => '0', '!Expect' => null, 'Content-Type' => 'application/x-www-form-urlencoded', '!Transfer-Encoding' => null)), array('PUT', 'http://localhost:8124/empty-put.php', null, null, array(CURLOPT_HTTPHEADER => array('Expect:', 'Host: localhost:8124', 'User-Agent: ' . $userAgent, 'Content-Length: 0')), array('_Accept' => '*', '_Accept-Encoding' => '*', 'Host' => '*', 'User-Agent' => '*', 'Content-Length' => '0', '!Expect' => null, '!Content-Type' => null, '!Transfer-Encoding' => null)), array('PATCH', 'http://localhost:8124/patch.php', null, 'body', array(CURLOPT_INFILESIZE => 4, CURLOPT_HTTPHEADER => array('Host: localhost:8124', 'User-Agent: ' . $userAgent, 'Expect: 100-Continue')))); }
/** * @covers Guzzle\Http\Message\EntityEnclosingRequest::__toString * @covers Guzzle\Http\Message\EntityEnclosingRequest::addPostFields */ public function testAddsPostFieldsAndSetsContentLength() { $request = RequestFactory::getInstance()->create('POST', 'http://www.guzzle-project.com/', null, array('data' => '123')); $this->assertEquals("POST / HTTP/1.1\r\n" . "Host: www.guzzle-project.com\r\n" . "User-Agent: " . Guzzle::getDefaultUserAgent() . "\r\n" . "Content-Type: application/x-www-form-urlencoded\r\n\r\n" . "data=123", (string) $request); }