Skip to content
Permalink
Browse files

Add withLength() immutable helper

Add the immutable variant of length().
  • Loading branch information...
markstory committed Nov 26, 2016
1 parent a30ee07 commit f4d7d350c38629c21ce1936ede0b8e63d0434d92
Showing with 29 additions and 0 deletions.
  1. +12 −0 src/Network/Response.php
  2. +17 −0 tests/TestCase/Network/ResponseTest.php
@@ -1541,6 +1541,7 @@ public function protocol($protocol = null)
*
* @param int|null $bytes Number of bytes
* @return int|null
* @deprecated 3.4.0 Use withLength() to set length instead.
*/
public function length($bytes = null)
{
@@ -1555,6 +1556,17 @@ public function length($bytes = null)
return null;
}
/**
* Create a new response with the Content-Length header set.
*
* @param int|string $bytes Number of bytes
* @return static
*/
public function withLength($bytes)
{
return $this->withHeader('Content-Length', (string)$bytes);
}
/**
* Checks whether a response has not been modified according to the 'If-None-Match'
* (Etags) and 'If-Modified-Since' (last modification date) request
@@ -695,6 +695,23 @@ public function testLength()
$this->assertEquals('100', $response->getHeaderLine('Content-Length'));
}
/**
* Tests settings the content length
*
* @return void
*/
public function testWithLength()
{
$response = new Response();
$this->assertFalse($response->hasHeader('Content-Length'));
$new = $response->withLength(100);
$this->assertFalse($response->hasHeader('Content-Length'), 'Old instance not modified');
$this->assertSame('100', $new->getHeaderLine('Content-Length'));
$this->assertSame('100', $new->length(), 'new method is compat with old.');
}
/**
* Tests setting the expiration date
*

0 comments on commit f4d7d35

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