mirror of
https://github.com/guzzle/guzzle.git
synced 2025-02-25 18:43:22 +01:00
Adopting composer for dependency management Updating LICENSE, travis build file, making better use of git ignores, and remove unused build target Removing @author tags. Use the commit history for a changelog. Moving files from build folder to / Adding min build target to product a Guzzle only phar with no autoloader [Common] Accepting ZF1 or ZF2 cache in ZendCacheAdapter [Common] Optimizing Stream wrapper and EntityBody abstractions. [Common] [Http] Migrating from Guzzle event system to the Symfony2 event dispatcher [Common] Moved Inflector and Inspector to Service namespace [Http] Simplifying Guzzle\Guzzle curl detection [Http] Removing Guzzle\Http\Pool and now using Guzzle\Http\Curl\CurlMulti [Http] The helper methods from Guzzle\Http\Message\RequestFactory have been removed to prevent confusion and encourage developers to use Guzzle\Http\Client to create requests. [Http] Clients can now send one or more requests in an array using the send() method, so the batch() method was removed. [Http] Updating curl multi to allow blocking calls while sending other transfers [Http] Making the Request::hasHeader method more intuitive. Guzzle\Http\Message\AbstractMessage::hasHeader() now returns true if the header is found using exact matching. If the header is found using a regex or case-insensitive match, then it will return the name of the found header. [Http] Removing content-type guessing from EntityBody based on file extension and solely using finfo. [Http] Adding basic auth plugin [Http] Cleaning up CookieJar and CurlMulti [Http] Removing custom rawurlencode from QueryString because PHP 5.3 now properly deals with tilde characters. [Http] Minor optimization to parsing messages in RequestFactory [Http] Adding Guzzle\Http\Client for developers that don't need commands or service descriptions [Http] Making it easier to set a global User-Agent header for a Guzzle\Http\Client [Http] Fixing the discrepancies between the ClientInterface and Guzzle\Http\Client [Http] Adding the ability to set and retrieve tokenized headers from Requests and Responses [Service] Ditching NIH filters and using the Symfony2 validator [Service] Moving most service building logic to the ServiceBuilder::factory method so that it is easier to build custom config readers. [Service] Allowing deep nested command inheritance. [Service] Cleaning up Inflector caching. [Service] Getting rid of concept of can_batch because everything is now sent in parallel. [Service] Adding a JSON description builder. [Service] Cleaning up ResourceIteratorApplyBatched. [Service] Removing caching stuff from ServiceBuilder because the data being cached is extremely fast to generate. [Service] Added a method to serialize the ServiceDescription in case a ServiceDescription needs to be cached in an application. [Service] Making description builders use static methods. [Service] Adding support to include other description files for XML and JSON description builders. [Service] Adding support for filters to ApiCommands [Service] Using {{}} instead of $. to reference other services as a dependency for another service
275 lines
12 KiB
PHP
275 lines
12 KiB
PHP
<?php
|
|
|
|
namespace Guzzle\Tests\Http\Message;
|
|
|
|
use Guzzle\Common\Collection;
|
|
use Guzzle\Http\Client;
|
|
use Guzzle\Http\Url;
|
|
use Guzzle\Http\EntityBody;
|
|
use Guzzle\Http\Message\RequestFactory;
|
|
use Guzzle\Http\QueryString;
|
|
|
|
/**
|
|
* @group server
|
|
*/
|
|
class HttpRequestFactoryTest extends \Guzzle\Tests\GuzzleTestCase
|
|
{
|
|
/**
|
|
* @covers Guzzle\Http\Message\RequestFactory::create
|
|
*/
|
|
public function testCreatesNewGetRequests()
|
|
{
|
|
$request = RequestFactory::create('GET', 'http://www.google.com/');
|
|
$this->assertInstanceOf('Guzzle\\Http\\Message\\MessageInterface', $request);
|
|
$this->assertInstanceOf('Guzzle\\Http\\Message\\RequestInterface', $request);
|
|
$this->assertInstanceOf('Guzzle\\Http\\Message\\Request', $request);
|
|
$this->assertEquals('GET', $request->getMethod());
|
|
$this->assertEquals('http', $request->getScheme());
|
|
$this->assertEquals('http://www.google.com/', $request->getUrl());
|
|
$this->assertEquals('www.google.com', $request->getHost());
|
|
$this->assertEquals('/', $request->getPath());
|
|
$this->assertEquals('/', $request->getResourceUri());
|
|
|
|
// Create a GET request with a custom receiving body
|
|
$this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
|
|
$b = EntityBody::factory();
|
|
$request = RequestFactory::create('GET', $this->getServer()->getUrl(), null, $b);
|
|
$request->setClient(new Client());
|
|
$response = $request->send();
|
|
$this->assertSame($b, $response->getBody());
|
|
}
|
|
|
|
/**
|
|
* @covers Guzzle\Http\Message\RequestFactory::create
|
|
*/
|
|
public function testCreatesPutRequests()
|
|
{
|
|
// Test using a string
|
|
$request = RequestFactory::create('PUT', 'http://www.google.com/path?q=1&v=2', null, 'Data');
|
|
$this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request);
|
|
$this->assertEquals('PUT', $request->getMethod());
|
|
$this->assertEquals('http', $request->getScheme());
|
|
$this->assertEquals('http://www.google.com/path?q=1&v=2', $request->getUrl());
|
|
$this->assertEquals('www.google.com', $request->getHost());
|
|
$this->assertEquals('/path', $request->getPath());
|
|
$this->assertEquals('/path?q=1&v=2', $request->getResourceUri());
|
|
$this->assertInstanceOf('Guzzle\\Http\\EntityBody', $request->getBody());
|
|
$this->assertEquals('Data', (string) $request->getBody());
|
|
unset($request);
|
|
|
|
// Test using an EntityBody
|
|
$request = RequestFactory::create('PUT', 'http://www.google.com/path?q=1&v=2', null, EntityBody::factory('Data'));
|
|
$this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request);
|
|
$this->assertEquals('Data', (string) $request->getBody());
|
|
|
|
// Test using a resource
|
|
$resource = fopen('php://temp', 'w+');
|
|
fwrite($resource, 'Data');
|
|
$request = RequestFactory::create('PUT', 'http://www.google.com/path?q=1&v=2', null, $resource);
|
|
$this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request);
|
|
$this->assertEquals('Data', (string) $request->getBody());
|
|
|
|
// Test using an object that can be cast as a string
|
|
$request = RequestFactory::create('PUT', 'http://www.google.com/path?q=1&v=2', null, Url::factory('http://www.example.com/'));
|
|
$this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request);
|
|
$this->assertEquals('http://www.example.com/', (string) $request->getBody());
|
|
}
|
|
|
|
/**
|
|
* @covers Guzzle\Http\Message\RequestFactory::create
|
|
*/
|
|
public function testCreatesHeadAndDeleteRequests()
|
|
{
|
|
$request = RequestFactory::create('DELETE', 'http://www.test.com/');
|
|
$this->assertEquals('DELETE', $request->getMethod());
|
|
$request = RequestFactory::create('HEAD', 'http://www.test.com/');
|
|
$this->assertEquals('HEAD', $request->getMethod());
|
|
}
|
|
|
|
/**
|
|
* @covers Guzzle\Http\Message\RequestFactory::create
|
|
*/
|
|
public function testCreatesOptionsRequests()
|
|
{
|
|
$request = RequestFactory::create('OPTIONS', 'http://www.example.com/');
|
|
$this->assertEquals('OPTIONS', $request->getMethod());
|
|
$this->assertInstanceOf('Guzzle\\Http\\Message\\Request', $request);
|
|
}
|
|
|
|
/**
|
|
* @covers Guzzle\Http\Message\RequestFactory::create
|
|
*/
|
|
public function testCreatesNewPutRequestWithBody()
|
|
{
|
|
$request = RequestFactory::create('PUT', 'http://www.google.com/path?q=1&v=2', null, 'Data');
|
|
$this->assertEquals('Data', (string) $request->getBody());
|
|
}
|
|
|
|
/**
|
|
* @covers Guzzle\Http\Message\RequestFactory::create
|
|
*/
|
|
public function testCreatesNewPostRequestWithFields()
|
|
{
|
|
// Use an array
|
|
$request = RequestFactory::create('POST', 'http://www.google.com/path?q=1&v=2', null, array(
|
|
'a' => 'b'
|
|
));
|
|
$this->assertEquals(array('a' => 'b'), $request->getPostFields());
|
|
unset($request);
|
|
|
|
// Use a collection
|
|
$request = RequestFactory::create('POST', 'http://www.google.com/path?q=1&v=2', null, new Collection(array(
|
|
'a' => 'b'
|
|
)));
|
|
$this->assertEquals(array('a' => 'b'), $request->getPostFields());
|
|
|
|
// Use a QueryString
|
|
$request = RequestFactory::create('POST', 'http://www.google.com/path?q=1&v=2', null, new QueryString(array(
|
|
'a' => 'b'
|
|
)));
|
|
$this->assertEquals(array('a' => 'b'), $request->getPostFields());
|
|
|
|
$request = RequestFactory::create('POST', 'http://www.test.com/', null, array(
|
|
'a' => 'b',
|
|
'file' => '@' . __FILE__
|
|
));
|
|
|
|
$this->assertEquals(array(
|
|
'a' => 'b',
|
|
'file' => '@' . __FILE__
|
|
), $request->getPostFields());
|
|
|
|
$this->assertEquals(array(
|
|
'file' => __FILE__
|
|
), $request->getPostFiles());
|
|
}
|
|
|
|
/**
|
|
* @covers Guzzle\Http\Message\RequestFactory::fromParts
|
|
* @covers Guzzle\Http\Message\RequestFactory::create
|
|
*/
|
|
public function testCreatesFromParts()
|
|
{
|
|
$parts = parse_url('http://michael:123@www.google.com:8080/path?q=1&v=2');
|
|
|
|
$request = RequestFactory::fromParts('PUT', $parts, null, 'Data');
|
|
$this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request);
|
|
$this->assertEquals('PUT', $request->getMethod());
|
|
$this->assertEquals('http', $request->getScheme());
|
|
$this->assertEquals('http://www.google.com:8080/path?q=1&v=2', $request->getUrl());
|
|
$this->assertEquals('www.google.com', $request->getHost());
|
|
$this->assertEquals('www.google.com:8080', $request->getHeader('Host'));
|
|
$this->assertEquals('/path', $request->getPath());
|
|
$this->assertEquals('/path?q=1&v=2', $request->getResourceUri());
|
|
$this->assertInstanceOf('Guzzle\\Http\\EntityBody', $request->getBody());
|
|
$this->assertEquals('Data', (string)$request->getBody());
|
|
$this->assertEquals('michael', $request->getUsername());
|
|
$this->assertEquals('123', $request->getPassword());
|
|
$this->assertEquals('8080', $request->getPort());
|
|
$this->assertEquals(array(
|
|
'scheme' => 'http',
|
|
'host' => 'www.google.com',
|
|
'port' => 8080,
|
|
'path' => '/path',
|
|
'query' => 'q=1&v=2',
|
|
), parse_url($request->getUrl()));
|
|
}
|
|
|
|
/**
|
|
* @covers Guzzle\Http\Message\RequestFactory::parseMessage
|
|
* @covers Guzzle\Http\Message\RequestFactory::fromMessage
|
|
* @covers Guzzle\Http\Message\RequestFactory::create
|
|
*/
|
|
public function testCreatesFromMessage()
|
|
{
|
|
$auth = base64_encode('michael:123');
|
|
$message = "PUT /path?q=1&v=2 HTTP/1.1\r\nHost: www.google.com:8080\r\nContent-Length: 4\r\nAuthorization: Basic {$auth}\r\n\r\nData";
|
|
$request = RequestFactory::fromMessage($message);
|
|
$this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request);
|
|
$this->assertEquals('PUT', $request->getMethod());
|
|
$this->assertEquals('http', $request->getScheme());
|
|
$this->assertEquals('http://www.google.com:8080/path?q=1&v=2', $request->getUrl());
|
|
$this->assertEquals('www.google.com', $request->getHost());
|
|
$this->assertEquals('www.google.com:8080', $request->getHeader('Host'));
|
|
$this->assertEquals('/path', $request->getPath());
|
|
$this->assertEquals('/path?q=1&v=2', $request->getResourceUri());
|
|
$this->assertInstanceOf('Guzzle\\Http\\EntityBody', $request->getBody());
|
|
$this->assertEquals('Data', (string)$request->getBody());
|
|
$this->assertEquals('michael', $request->getUsername());
|
|
$this->assertEquals('123', $request->getPassword());
|
|
$this->assertEquals('8080', $request->getPort());
|
|
|
|
// Test passing a blank message returns false
|
|
$this->assertFalse($request = RequestFactory::fromMessage(''));
|
|
|
|
// Test passing a url with no port
|
|
$message = "PUT /path?q=1&v=2 HTTP/1.1\r\nHost: www.google.com\r\nContent-Length: 4\r\nAuthorization: Basic {$auth}\r\n\r\nData";
|
|
$request = RequestFactory::fromMessage($message);
|
|
$this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request);
|
|
$this->assertEquals('PUT', $request->getMethod());
|
|
$this->assertEquals('http', $request->getScheme());
|
|
$this->assertEquals('http://www.google.com/path?q=1&v=2', $request->getUrl());
|
|
$this->assertEquals('www.google.com', $request->getHost());
|
|
$this->assertEquals('/path', $request->getPath());
|
|
$this->assertEquals('/path?q=1&v=2', $request->getResourceUri());
|
|
$this->assertInstanceOf('Guzzle\\Http\\EntityBody', $request->getBody());
|
|
$this->assertEquals('Data', (string)$request->getBody());
|
|
$this->assertEquals('michael', $request->getUsername());
|
|
$this->assertEquals('123', $request->getPassword());
|
|
$this->assertEquals(80, $request->getPort());
|
|
}
|
|
|
|
/**
|
|
* @covers Guzzle\Http\Message\RequestFactory::create
|
|
*/
|
|
public function testCreatesNewTraceRequest()
|
|
{
|
|
$request = RequestFactory::create('TRACE', 'http://www.google.com/');
|
|
$this->assertFalse($request instanceof \Guzzle\Http\Message\EntityEnclosingRequest);
|
|
$this->assertEquals('TRACE', $request->getMethod());
|
|
}
|
|
|
|
/**
|
|
* @covers Guzzle\Http\Message\RequestFactory::parseMessage
|
|
*/
|
|
public function testParsesMessages()
|
|
{
|
|
$parts = RequestFactory::parseMessage(
|
|
"get /testing?q=10&f=3 http/1.1\r\n" .
|
|
"host: localhost:443\n" .
|
|
"authorization: basic bWljaGFlbDoxMjM=\r\n\r\n"
|
|
);
|
|
|
|
$this->assertEquals('GET', $parts['method']);
|
|
$this->assertEquals('HTTP', $parts['protocol']);
|
|
$this->assertEquals('1.1', $parts['protocol_version']);
|
|
$this->assertEquals('https', $parts['parts']['scheme']);
|
|
$this->assertEquals('localhost', $parts['parts']['host']);
|
|
$this->assertEquals('443', $parts['parts']['port']);
|
|
$this->assertEquals('michael', $parts['parts']['user']);
|
|
$this->assertEquals('123', $parts['parts']['pass']);
|
|
$this->assertEquals('/testing', $parts['parts']['path']);
|
|
$this->assertEquals('?q=10&f=3', $parts['parts']['query']);
|
|
$this->assertEquals(array(
|
|
'Host' => 'localhost:443',
|
|
'Authorization' => 'basic bWljaGFlbDoxMjM='
|
|
), $parts['headers']);
|
|
$this->assertEquals('', $parts['body']);
|
|
|
|
$parts = RequestFactory::parseMessage(
|
|
"get / spydy/1.0\r\n"
|
|
);
|
|
$this->assertEquals('GET', $parts['method']);
|
|
$this->assertEquals('SPYDY', $parts['protocol']);
|
|
$this->assertEquals('1.0', $parts['protocol_version']);
|
|
$this->assertEquals('http', $parts['parts']['scheme']);
|
|
$this->assertEquals('', $parts['parts']['host']);
|
|
$this->assertEquals('', $parts['parts']['port']);
|
|
$this->assertEquals('', $parts['parts']['user']);
|
|
$this->assertEquals('', $parts['parts']['pass']);
|
|
$this->assertEquals('/', $parts['parts']['path']);
|
|
$this->assertEquals('', $parts['parts']['query']);
|
|
$this->assertEquals(array(), $parts['headers']);
|
|
$this->assertEquals('', $parts['body']);
|
|
}
|
|
} |