Skip to content

Commit

Permalink
Merge pull request #1010 from odiel/1.3.0
Browse files Browse the repository at this point in the history
1.3.0
  • Loading branch information
Phalcon committed Aug 5, 2013
2 parents e7d066d + 017f087 commit a4ff1cd
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ext/http/response/headers.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ PHP_METHOD(Phalcon_Http_Response_Headers, reset){
}

/**
* Returns the lastest dispatched controller
* Returns the current headers as an array
*
* @return Phalcon\Mvc\ControllerInterface
* @return array
*/
PHP_METHOD(Phalcon_Http_Response_Headers, toArray){

Expand Down
7 changes: 7 additions & 0 deletions ext/http/response/headersinterface.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ PHALCON_DOC_METHOD(Phalcon_Http_Response_HeadersInterface, send);
*/
PHALCON_DOC_METHOD(Phalcon_Http_Response_HeadersInterface, reset);

/**
* Returns the current headers as an array
*
* @return array
*/
PHALCON_DOC_METHOD(Phalcon_Http_Response_HeadersInterface, toArray);

/**
* Restore a Phalcon\Http\Response\Headers object
*
Expand Down
1 change: 1 addition & 0 deletions ext/http/response/headersinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ PHALCON_INIT_FUNCS(phalcon_http_response_headersinterface_method_entry){
PHP_ABSTRACT_ME(Phalcon_Http_Response_HeadersInterface, setRaw, arginfo_phalcon_http_response_headersinterface_setraw)
PHP_ABSTRACT_ME(Phalcon_Http_Response_HeadersInterface, send, NULL)
PHP_ABSTRACT_ME(Phalcon_Http_Response_HeadersInterface, reset, NULL)
PHP_ABSTRACT_ME(Phalcon_Http_Response_HeadersInterface, toArray, NULL)
ZEND_FENTRY(__set_state, NULL, arginfo_phalcon_http_response_headersinterface___set_state, ZEND_ACC_STATIC|ZEND_ACC_ABSTRACT|ZEND_ACC_PUBLIC)
PHP_FE_END
};
Expand Down
222 changes: 222 additions & 0 deletions php-tests/tests/Phalcon/Http/Response/ResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
<?php

/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2012 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to license@phalconphp.com so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <andres@phalconphp.com> |
| Eduar Carvajal <eduar@phalconphp.com> |
+------------------------------------------------------------------------+
*/

class ResponseTest extends PHPUnit_Framework_TestCase
{

private $_response;

public function setUp()
{
$di = new Phalcon\DI();

$di->set('url', function(){
$url = new Phalcon\Mvc\Url();
$url->setBaseUri('/');
return $url;
});

$this->_response = new Phalcon\Http\Response();

$this->_response->setDI($di);
}

public function testSetHeader()
{

$this->_response->resetHeaders();

$this->_response->setHeader('Content-Type', 'text/html');

$this->assertEquals(Phalcon\Http\Response\Headers::__set_state(array(
'_headers' => array(
'Content-Type' => 'text/html',
)
)), $this->_response->getHeaders());

$this->_response->setHeader('Content-Length', '1234');

$this->assertEquals(Phalcon\Http\Response\Headers::__set_state(array(
'_headers' => array(
'Content-Type' => 'text/html',
'Content-Length' => '1234'
)
)), $this->_response->getHeaders());

}

public function testSetStatusHeader()
{

$this->_response->resetHeaders();

$this->_response->setStatusCode(404, "Not Found");

$this->assertEquals(Phalcon\Http\Response\Headers::__set_state(array(
'_headers' => array(
'HTTP/1.1 404 Not Found' => '',
'Status' => '404 Not Found'
)
)), $this->_response->getHeaders());

}

public function testSetRawHeader()
{

$this->_response->resetHeaders();

$this->_response->setRawHeader("HTTP/1.1 404 Not Found");

$this->assertEquals(Phalcon\Http\Response\Headers::__set_state(array(
'_headers' => array(
'HTTP/1.1 404 Not Found' => '',
)
)), $this->_response->getHeaders());

}

public function testToArrayHeader()
{

$this->_response->resetHeaders();

$this->_response->setHeader('Content-Type', 'text/html');
$this->_response->setHeader('Content-Length', '1234');

$headers = $this->_response->getHeaders();
$headersArray = $headers->toArray();

$this->assertArrayHasKey('Content-Type', $headersArray);
$this->assertArrayHasKey('Content-Length', $headersArray);
$this->assertEquals('text/html', $headersArray['Content-Type']);
$this->assertEquals('1234', $headersArray['Content-Length']);
}

public function testSetContentType()
{

//Without charset
$this->_response->resetHeaders();

$this->_response->setContentType('application/json');

$this->assertEquals(Phalcon\Http\Response\Headers::__set_state(array(
'_headers' => array(
'Content-Type' => 'application/json'
)
)), $this->_response->getHeaders());

//With charset
$this->_response->resetHeaders();

$this->_response->setContentType('application/json', 'utf-8');

$this->assertEquals(Phalcon\Http\Response\Headers::__set_state(array(
'_headers' => array(
'Content-Type' => 'application/json; charset=utf-8'
)
)), $this->_response->getHeaders());
}

public function testNotModifiedHeader()
{

$this->_response->resetHeaders();

$this->_response->setNotModified();

$this->assertEquals(Phalcon\Http\Response\Headers::__set_state(array(
'_headers' => array(
'HTTP/1.1 304 Not modified' => false,
'Status' => '304 Not modified'
)
)), $this->_response->getHeaders());

}

public function testRedirect()
{


//local URI
$this->_response->resetHeaders();

$this->_response->redirect("some/local/uri");

$this->assertEquals(Phalcon\Http\Response\Headers::__set_state(array(
'_headers' => array(
'HTTP/1.1 302 Redirect' => false,
'Status' => '302 Redirect',
'Location' => '/some/local/uri'
)
)), $this->_response->getHeaders());

//Full URL
$this->_response->resetHeaders();

$this->_response->redirect("http://google.com", true);

$this->assertEquals(Phalcon\Http\Response\Headers::__set_state(array(
'_headers' => array(
'HTTP/1.1 302 Redirect' => false,
'Status' => '302 Redirect',
'Location' => 'http://google.com'
)
)), $this->_response->getHeaders());

//HTTP code
$this->_response->resetHeaders();

$this->_response->redirect("http://google.com", true, 301);

$this->assertEquals(Phalcon\Http\Response\Headers::__set_state(array(
'_headers' => array(
'HTTP/1.1 301 Redirect' => false,
'Status' => '301 Redirect',
'Location' => 'http://google.com'
)
)), $this->_response->getHeaders());

}

public function testContent()
{

$this->_response->setContent('<h1>Hello');
$this->assertEquals($this->_response->getContent(), '<h1>Hello');

$this->_response->appendContent('</h1>');
$this->assertEquals($this->_response->getContent(), '<h1>Hello</h1>');

}

public function testSetFileToSend()
{
$filename = __FILE__;
$this->_response->setFileToSend($filename);
ob_start();
$this->_response->send();
$actual = ob_get_clean();
$expected = file_get_contents($filename);
$this->assertEquals($actual, $expected);
$this->assertEquals($this->_response->isSent(), true);
}
}

0 comments on commit a4ff1cd

Please sign in to comment.