1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-25 02:22:57 +01:00
guzzle/tests/Message/RequestTest.php

133 lines
4.4 KiB
PHP
Raw Normal View History

<?php
namespace GuzzleHttp\Tests\Message;
2013-10-07 13:24:53 -07:00
use GuzzleHttp\Event\Emitter;
use GuzzleHttp\Message\Request;
use GuzzleHttp\Stream\Stream;
use GuzzleHttp\Query;
/**
* @covers GuzzleHttp\Message\Request
*/
class RequestTest extends \PHPUnit_Framework_TestCase
{
2013-09-29 18:15:01 -07:00
public function testConstructorInitializesMessage()
{
2013-09-29 18:15:01 -07:00
$r = new Request('PUT', '/test', ['test' => '123'], Stream::factory('foo'));
$this->assertEquals('PUT', $r->getMethod());
$this->assertEquals('/test', $r->getUrl());
$this->assertEquals('123', $r->getHeader('test'));
$this->assertEquals('foo', $r->getBody());
}
2013-09-29 18:15:01 -07:00
public function testConstructorInitializesMessageWithProtocolVersion()
{
2013-09-29 18:15:01 -07:00
$r = new Request('GET', '', [], null, ['protocol_version' => 10]);
$this->assertEquals(10, $r->getProtocolVersion());
}
public function testConstructorInitializesMessageWithEmitter()
{
$e = new Emitter();
$r = new Request('GET', '', [], null, ['emitter' => $e]);
$this->assertSame($r->getEmitter(), $e);
}
2013-09-29 18:15:01 -07:00
public function testCloneIsDeep()
{
2013-09-29 18:15:01 -07:00
$r = new Request('GET', '/test', ['foo' => 'baz'], Stream::factory('foo'));
$r2 = clone $r;
$this->assertNotSame($r->getEmitter(), $r2->getEmitter());
2013-09-29 18:15:01 -07:00
$this->assertEquals('foo', $r2->getBody());
2013-09-29 18:15:01 -07:00
$r->getConfig()->set('test', 123);
$this->assertFalse($r2->getConfig()->hasKey('test'));
2013-09-29 18:15:01 -07:00
$r->setPath('/abc');
$this->assertEquals('/test', $r2->getPath());
}
2013-09-29 18:15:01 -07:00
public function testCastsToString()
{
2013-09-29 18:15:01 -07:00
$r = new Request('GET', 'http://test.com/test', ['foo' => 'baz'], Stream::factory('body'));
$s = explode("\r\n", (string) $r);
$this->assertEquals("GET /test HTTP/1.1", $s[0]);
$this->assertContains('Host: test.com', $s);
$this->assertContains('foo: baz', $s);
$this->assertContains('', $s);
$this->assertContains('body', $s);
}
2013-09-29 18:15:01 -07:00
public function testSettingUrlOverridesHostHeaders()
{
2013-09-29 18:15:01 -07:00
$r = new Request('GET', 'http://test.com/test');
$r->setUrl('https://baz.com/bar');
$this->assertEquals('baz.com', $r->getHost());
$this->assertEquals('baz.com', $r->getHeader('Host'));
$this->assertEquals('/bar', $r->getPath());
$this->assertEquals('https', $r->getScheme());
}
2013-09-29 18:15:01 -07:00
public function testQueryIsMutable()
{
2013-09-29 18:15:01 -07:00
$r = new Request('GET', 'http://www.foo.com?baz=bar');
$this->assertEquals('baz=bar', $r->getQuery());
$this->assertInstanceOf('GuzzleHttp\Query', $r->getQuery());
2013-09-29 18:15:01 -07:00
$r->getQuery()->set('hi', 'there');
$this->assertEquals('/?baz=bar&hi=there', $r->getResource());
}
public function testQueryCanChange()
{
$r = new Request('GET', 'http://www.foo.com?baz=bar');
$r->setQuery(new Query(['foo' => 'bar']));
$this->assertEquals('foo=bar', $r->getQuery());
}
2013-09-29 18:15:01 -07:00
public function testCanChangeMethod()
{
2013-09-29 18:15:01 -07:00
$r = new Request('GET', 'http://www.foo.com');
$r->setMethod('put');
$this->assertEquals('PUT', $r->getMethod());
}
public function testCanChangeSchemeWithPort()
{
$r = new Request('GET', 'http://www.foo.com:80');
$r->setScheme('https');
$this->assertEquals('https://www.foo.com', $r->getUrl());
}
2013-09-29 18:15:01 -07:00
public function testCanChangeScheme()
{
2013-09-29 18:15:01 -07:00
$r = new Request('GET', 'http://www.foo.com');
$r->setScheme('https');
$this->assertEquals('https://www.foo.com', $r->getUrl());
}
2013-09-29 18:15:01 -07:00
public function testCanChangeHost()
2013-04-30 11:26:22 -07:00
{
2013-09-29 18:15:01 -07:00
$r = new Request('GET', 'http://www.foo.com:222');
$r->setHost('goo');
$this->assertEquals('http://goo:222', $r->getUrl());
$this->assertEquals('goo:222', $r->getHeader('host'));
$r->setHost('goo:80');
$this->assertEquals('http://goo', $r->getUrl());
$this->assertEquals('goo', $r->getHeader('host'));
2013-04-30 11:26:22 -07:00
}
public function testCanChangePort()
{
$r = new Request('GET', 'http://www.foo.com:222');
$this->assertSame(222, $r->getPort());
$this->assertEquals('www.foo.com', $r->getHost());
$this->assertEquals('www.foo.com:222', $r->getHeader('host'));
$r->setPort(80);
$this->assertSame(80, $r->getPort());
$this->assertEquals('www.foo.com', $r->getHost());
$this->assertEquals('www.foo.com', $r->getHeader('host'));
}
Breaking / Potentially breaking changes: 1. Adopting a marker interface for Guzzle exceptions. A. All exceptions emitted from Guzzle are now wrapped with a Guzzle namespaced exception. B. Guzzle\Common\GuzzleExceptionInterface was renamed to Guzzle\Common\GuzzleException C. Guzzle\Common\ExceptionCollection was renamed to Guzzle\Common\Exception\ExceptionCollection 2. Using Header objects for Request and Response objects A. When you call $request->getHeader('X'), you will get back a Guzzle\Http\Message\Header object that contains all of the headers that case insensitively match. This object can be cast to a string or iterated like an array. You can pass true in the second argument to retrieve the header as a string. B. Removing the old Guzzle\Common\Collection based searching arguments from most of the request and response header methods. All retrievals are case-insensitive and return Header objects. 3. Changing the two headers added by the cache plugin to just one header with key and ttl. 4. Changing Guzzle\Http\Message\Response::factory() to fromMessage(). 5. Removing the NullObject return value from ServiceDescriptions and instead simply returning null New Features / enhancements: 1. Adding Guzzle\Http\Message\AbstractMessage::addHeaders() 2. Making it simpler to create service descriptions using a unified factory method that delegates to other factories. 3. Better handling of ports and hosts in Guzzle\Http\Url Note: This is a noisy diff because I'm removing trailing whitespace and adding a new line at the end of each source file.
2012-04-21 00:23:07 -07:00
}