Skip to content

Commit

Permalink
[HttpFoundation] Send proper charset along with the default text/html…
Browse files Browse the repository at this point in the history
… header
  • Loading branch information
Seldaek authored and fabpot committed Dec 16, 2010
1 parent b37f9b4 commit 4f46235
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
Expand Up @@ -35,6 +35,10 @@
-->
<service id="request" factory-service="http_kernel" factory-method="getRequest" shared="false" />

<service id="response" class="%response.class%" shared="false" />
<service id="response" class="%response.class%" shared="false">
<call method="setCharset">
<argument>%kernel.charset%</argument>
</call>
</service>
</services>
</container>
25 changes: 23 additions & 2 deletions src/Symfony/Component/HttpFoundation/Response.php
Expand Up @@ -27,6 +27,7 @@ class Response
protected $version;
protected $statusCode;
protected $statusText;
protected $charset = 'UTF-8';

static public $statusTexts = array(
100 => 'Continue',
Expand Down Expand Up @@ -96,7 +97,7 @@ public function __toString()
$content = '';

if (!$this->headers->has('Content-Type')) {
$this->headers->set('Content-Type', 'text/html');
$this->headers->set('Content-Type', 'text/html; charset='.$this->charset);
}

// status
Expand Down Expand Up @@ -128,7 +129,7 @@ public function __clone()
public function sendHeaders()
{
if (!$this->headers->has('Content-Type')) {
$this->headers->set('Content-Type', 'text/html');
$this->headers->set('Content-Type', 'text/html; charset='.$this->charset);
}

// status
Expand Down Expand Up @@ -227,6 +228,26 @@ public function getStatusCode()
return $this->statusCode;
}

/**
* Sets response charset.
*
* @param string $charset Character set
*/
public function setCharset($charset)
{
$this->charset = $charset;
}

/**
* Retrieves the response charset.
*
* @return string Character set
*/
public function getCharset()
{
return $this->charset;
}

/**
* Returns true if the response is worth caching under any circumstance.
*
Expand Down
22 changes: 22 additions & 0 deletions tests/Symfony/Tests/Component/HttpFoundation/ResponseTest.php
Expand Up @@ -136,6 +136,28 @@ public function testGetVary()
$this->assertEquals(array('Accept-Language', 'User-Agent', 'X-Foo'), $response->getVary(), '->getVary() parses multiple header name values separated by commas');
}

public function testDefaultContentType()
{
$headerMock = $this->getMock('Symfony\Component\HttpFoundation\ResponseHeaderBag', array('set'));
$headerMock->expects($this->at(0))
->method('set')
->with('Content-Type', 'text/html; charset=UTF-8');
$headerMock->expects($this->at(1))
->method('set')
->with('Content-Type', 'text/html; charset=Foo');

$response = new Response();
$response->headers = $headerMock;

// verify first set()
$response->sendHeaders();

$response->headers->remove('Content-Type');
$response->setCharset('Foo');
// verify second set()
$response->__toString();
}

protected function createDateTimeOneHourAgo()
{
$date = new \DateTime();
Expand Down

0 comments on commit 4f46235

Please sign in to comment.