Skip to content

Commit

Permalink
Implement withNotModified()
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Dec 2, 2016
1 parent 46fe19a commit 9d7c22f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/Network/Response.php
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down
21 changes: 20 additions & 1 deletion tests/TestCase/Network/ResponseTest.php
Expand Up @@ -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());
}

/**
Expand Down

0 comments on commit 9d7c22f

Please sign in to comment.