Skip to content
Permalink
Browse files

Saving a few bytes by unsetting the content if the response status co…

…de is 204 (No Content) or 304 (Not Modified)
  • Loading branch information...
lorenzo committed Nov 4, 2011
1 parent 5b42cb8 commit 4d19d536f6998a2d1dc32564751be674298702f5
Showing with 40 additions and 0 deletions.
  1. +12 −0 lib/Cake/Network/CakeResponse.php
  2. +28 −0 lib/Cake/Test/Case/Network/CakeResponseTest.php
@@ -349,13 +349,25 @@ public function send() {
$codeMessage = $this->_statusCodes[$this->_status];
$this->_sendHeader("{$this->_protocol} {$this->_status} {$codeMessage}");
$this->_sendHeader('Content-Type', "{$this->_contentType}; charset={$this->_charset}");
$this->_setContent();
$this->_setContentLength();
foreach ($this->_headers as $header => $value) {
$this->_sendHeader($header, $value);
}
$this->_sendContent($this->_body);
}
/**
* Sets the response body to an empty text if the status code is 204 or 304
*
* @return void
*/
protected function _setContent() {
if (in_array($this->_status, array(304, 204))) {
$this->body('');
}
}
/**
* Calculates the correct Content-Length and sets it as a header in the response
* Will not set the value if already set or if the output is compressed.
@@ -535,4 +535,32 @@ public function testLength() {
->method('_sendHeader');
$response->send();
}
/**
* Tests that the response body is unset if the status code is 304 or 204
*
* @return void
*/
public function testUnmodifiedContent() {
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
$response->body('This is a body');
$response->statusCode(204);
$response->expects($this->once())
->method('_sendContent')->with('');
$response->send();
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
$response->body('This is a body');
$response->statusCode(304);
$response->expects($this->once())
->method('_sendContent')->with('');
$response->send();
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
$response->body('This is a body');
$response->statusCode(200);
$response->expects($this->once())
->method('_sendContent')->with('This is a body');
$response->send();
}
}

0 comments on commit 4d19d53

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