Skip to content
Permalink
Browse files

Make header(), version() and method() play nice with PSR7

Add tests ensuring that both setters can be read from the other reader
method.
  • Loading branch information...
markstory committed May 7, 2016
1 parent a537e9c commit 92081faf1caee87f16fdc7508737c2acafa7995b
@@ -421,7 +421,7 @@ public function buildUrl($url, $query = [], $options = [])
protected function _createRequest($method, $url, $data, $options)
{
$request = new Request();
$request->method($method)
$request = $request->withMethod($method)
->url($url)
->body($data);
@@ -141,13 +141,6 @@ class Message
*/
protected $_cookies = [];
/**
* HTTP Version being used.
*
* @var string
*/
protected $_version = '1.1';
/**
* Normalize header names to Camel-Case form.
*
@@ -191,7 +184,7 @@ public function cookies()
*/
public function version()
{
return $this->_version;
return $this->protocol;
}
/**
@@ -14,6 +14,7 @@
namespace Cake\Http\Client;
use Cake\Core\Exception\Exception;
use Psr\Http\Message\RequestInterface;
use Zend\Diactoros\MessageTrait;
use Zend\Diactoros\RequestTrait;
@@ -24,18 +25,11 @@
* for making requests.
*
*/
class Request extends Message
class Request extends Message implements RequestInterface
{
use MessageTrait;
use RequestTrait;
/**
* The HTTP method to use.
*
* @var string
*/
protected $_method = self::METHOD_GET;
/**
* Request body to send.
*
@@ -51,18 +45,30 @@ class Request extends Message
protected $_url;
/**
* Headers to be sent.
* Constructor
*
* @var array
* Provides backwards compatible defaults for some properties.
*/
protected $_headers = [
'Connection' => 'close',
'User-Agent' => 'CakePHP'
];
public function __construct()
{
$this->method = static::METHOD_GET;
$this->headerNames = [
'connection' => 'Connection',
'user-agent' => 'User-Agent',
];
$this->headers = [
'Connection' => 'close',
'User-Agent' => 'CakePHP'
];
}
/**
* Get/Set the HTTP method.
*
* *Warning* This method mutates the request in-place for backwards
* compatibility issues, and is not part of the PSR7 interface.
*
* @param string|null $method The method for the request.
* @return $this|string Either this or the current method.
* @throws \Cake\Core\Exception\Exception On invalid methods.
@@ -71,13 +77,13 @@ class Request extends Message
public function method($method = null)
{
if ($method === null) {
return $this->_method;
return $this->method;
}
$name = get_called_class() . '::METHOD_' . strtoupper($method);
if (!defined($name)) {
throw new Exception('Invalid method type');
}
$this->_method = $method;
$this->method = $method;
return $this;
}
@@ -120,22 +126,31 @@ public function url($url = null)
* $request->header(['Connection' => 'close', 'User-Agent' => 'CakePHP']);
* ```
*
* *Warning* This method mutates the request in-place for backwards
* compatibility issues, and is not part of the PSR7 interface.
*
* @param string|array|null $name The name to get, or array of multiple values to set.
* @param string|null $value The value to set for the header.
* @return mixed Either $this when setting or header value when getting.
* @deprecated 3.3.0 Use withHeader() and getHeaderLine() instead.
*/
public function header($name = null, $value = null)
{
if ($value === null && is_string($name)) {
$name = $this->_normalizeHeader($name);
return isset($this->_headers[$name]) ? $this->_headers[$name] : null;
$val = $this->getHeaderLine($name);
if ($val === '') {
return null;
}
return $val;
}
if ($value !== null && !is_array($name)) {
$name = [$name => $value];
}
foreach ($name as $key => $val) {
$key = $this->_normalizeHeader($key);
$this->_headers[$key] = $val;
$normalized = strtolower($key);
$this->headers[$key] = (array)$val;
$this->headerNames[$normalized] = $key;
}
return $this;
}
@@ -182,17 +197,20 @@ public function cookie($name, $value = null)
/**
* Get/Set HTTP version.
*
* *Warning* This method mutates the request in-place for backwards
* compatibility issues, and is not part of the PSR7 interface.
*
* @param string|null $version The HTTP version.
* @return $this|string Either $this or the HTTP version.
* @deprecated 3.3.0 Use getProtocolVersion() and withProtocolVersion() instead.
*/
public function version($version = null)
{
if ($version === null) {
return parent::version();
return $this->protocol;
}
$this->_version = $version;
$this->protocol = $version;
return $this;
}
}
@@ -172,7 +172,7 @@ public function testGetSimpleWithHeadersAndCookies()
->method('send')
->with($this->logicalAnd(
$this->isInstanceOf('Cake\Network\Http\Request'),
$this->attributeEqualTo('_method', Request::METHOD_GET),
$this->attributeEqualTo('method', Request::METHOD_GET),
$this->attributeEqualTo('_url', 'http://cakephp.org/test.html'),
$this->attributeEqualTo('_headers', $headers),
$this->attributeEqualTo('_cookies', $cookies)
@@ -201,7 +201,7 @@ public function testGetQuerystring()
->method('send')
->with($this->logicalAnd(
$this->isInstanceOf('Cake\Network\Http\Request'),
$this->attributeEqualTo('_method', Request::METHOD_GET),
$this->attributeEqualTo('method', Request::METHOD_GET),
$this->attributeEqualTo('_url', 'http://cakephp.org/search?q=hi+there&Category%5Bid%5D%5B0%5D=2&Category%5Bid%5D%5B1%5D=3')
))
->will($this->returnValue([$response]));
@@ -262,7 +262,7 @@ public function testGetWithContent()
->method('send')
->with($this->logicalAnd(
$this->isInstanceOf('Cake\Network\Http\Request'),
$this->attributeEqualTo('_method', Request::METHOD_GET),
$this->attributeEqualTo('method', Request::METHOD_GET),
$this->attributeEqualTo('_url', 'http://cakephp.org/search'),
$this->attributeEqualTo('_body', 'some data')
))
@@ -319,7 +319,7 @@ public function testGetWithAuthenticationAndProxy()
->method('send')
->with($this->logicalAnd(
$this->isInstanceOf('Cake\Network\Http\Request'),
$this->attributeEqualTo('_method', Request::METHOD_GET),
$this->attributeEqualTo('method', Request::METHOD_GET),
$this->attributeEqualTo('_url', 'http://cakephp.org/'),
$this->attributeEqualTo('_headers', $headers)
))
@@ -368,7 +368,7 @@ public function testMethodsSimple($method)
->method('send')
->with($this->logicalAnd(
$this->isInstanceOf('Cake\Network\Http\Request'),
$this->attributeEqualTo('_method', $method),
$this->attributeEqualTo('method', $method),
$this->attributeEqualTo('_url', 'http://cakephp.org/projects/add')
))
->will($this->returnValue([$response]));
@@ -417,7 +417,7 @@ public function testPostWithTypeKey($type, $mime)
$mock->expects($this->once())
->method('send')
->with($this->logicalAnd(
$this->attributeEqualTo('_method', Request::METHOD_POST),
$this->attributeEqualTo('method', Request::METHOD_POST),
$this->attributeEqualTo('_body', $data),
$this->attributeEqualTo('_headers', $headers)
))
@@ -538,7 +538,7 @@ public function testHeadQuerystring()
->method('send')
->with($this->logicalAnd(
$this->isInstanceOf('Cake\Network\Http\Request'),
$this->attributeEqualTo('_method', Request::METHOD_HEAD),
$this->attributeEqualTo('method', Request::METHOD_HEAD),
$this->attributeEqualTo('_url', 'http://cakephp.org/search?q=hi+there')
))
->will($this->returnValue([$response]));
@@ -48,6 +48,23 @@ public function testMethod()
$this->assertEquals(Request::METHOD_GET, $request->method());
}
/**
* test method interop.
*
* @return void
*/
public function testMethodInteroperability()
{
$request = new Request();
$this->assertSame($request, $request->method(Request::METHOD_GET));
$this->assertEquals(Request::METHOD_GET, $request->method());
$this->assertEquals(Request::METHOD_GET, $request->getMethod());
$request = $request->withMethod(Request::METHOD_GET);
$this->assertEquals(Request::METHOD_GET, $request->method());
$this->assertEquals(Request::METHOD_GET, $request->getMethod());
}
/**
* test invalid method.
*
@@ -103,6 +120,35 @@ public function testHeader()
$this->assertNull($request->header('not set'));
}
/**
* Test the default headers
*
* @return void
*/
public function testDefaultHeaders()
{
$request = new Request();
$this->assertEquals('CakePHP', $request->getHeaderLine('User-Agent'));
$this->assertEquals('close', $request->getHeaderLine('Connection'));
}
/**
* Test that header() and PSR7 methods play nice.
*
* @return void
*/
public function testHeaderMethodInteroperability()
{
$request = new Request();
$request->header('Content-Type', 'application/json');
$this->assertEquals('application/json', $request->header('Content-Type'), 'Old getter should work');
$this->assertEquals('application/json', $request->getHeaderLine('Content-Type'), 'getHeaderLine works');
$this->assertEquals('application/json', $request->getHeaderLine('content-type'), 'getHeaderLine works');
$this->assertEquals(['application/json'], $request->getHeader('Content-Type'), 'getHeader works');
$this->assertEquals(['application/json'], $request->getHeader('content-type'), 'getHeader works');
}
/**
* test cookie method.
*
@@ -133,4 +179,20 @@ public function testVersion()
$this->assertSame('1.0', $request->version());
}
/**
* test version interop.
*
* @return void
*/
public function testVersionInteroperability()
{
$request = new Request();
$this->assertEquals('1.1', $request->version());
$this->assertEquals('1.1', $request->getProtocolVersion());
$request = $request->withProtocolVersion('1.0');
$this->assertEquals('1.0', $request->version());
$this->assertEquals('1.0', $request->getProtocolVersion());
}
}

0 comments on commit 92081fa

Please sign in to comment.
You can’t perform that action at this time.