Skip to content
Permalink
Browse files

Implement withNotModified()

  • Loading branch information...
markstory committed Dec 2, 2016
1 parent 46fe19a commit 9d7c22f0af3978d83b219976aafe52c8b3ea85ca
Showing with 50 additions and 1 deletion.
  1. +30 −0 src/Network/Response.php
  2. +20 −1 tests/TestCase/Network/ResponseTest.php
@@ -1431,6 +1431,35 @@ public function notModified()
}
}
/**
* Create a new instance as 'not modified'
*
* This will remove any body contents set the status code
* to "304" and removing headers that describe
* a response body.
*
* @return static
*/
public function withNotModified()
{
$new = $this->withStatus(304);
$new->_createStream();
$remove = [
'Allow',
'Content-Encoding',
'Content-Language',
'Content-Length',
'Content-MD5',
'Content-Type',
'Last-Modified'
];
foreach ($remove as $header) {
$new = $new->withoutHeader($header);
}
return $new;
}
/**
* Sets the Vary header for the response, if an array is passed,
* values will be imploded into a comma separated string. If no
@@ -1531,6 +1560,7 @@ public function etag($hash = null, $weak = false)
public function withEtag($hash, $weak = false)
{
$hash = sprintf('%s"%s"', ($weak) ? 'W/' : null, $hash);
return $this->withHeader('Etag', $hash);
}
@@ -1013,7 +1013,26 @@ public function testNotModified()
*/
public function testWithNotModified()
{
$this->markTestIncomplete();
$response = new Response(['body' => 'something']);
$response = $response->withLength(100)
->withStatus(200)
->withHeader('Last-Modified', 'value')
->withHeader('Content-Language', 'en-EN')
->withHeader('X-things', 'things')
->withType('application/json');
$new = $response->withNotModified();
$this->assertTrue($response->hasHeader('Content-Language'), 'old instance not changed');
$this->assertTrue($response->hasHeader('Content-Length'), 'old instance not changed');
$this->assertFalse($new->hasHeader('Content-Type'));
$this->assertFalse($new->hasHeader('Content-Length'));
$this->assertFalse($new->hasHeader('Content-Language'));
$this->assertFalse($new->hasHeader('Last-Modified'));
$this->assertSame('things', $new->getHeaderLine('X-things'), 'Other headers are retained');
$this->assertSame(304, $new->getStatusCode());
$this->assertSame('', $new->getBody()->getContents());
}
/**

0 comments on commit 9d7c22f

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