Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/PhpEnvironment/Response.php
Expand Up @@ -21,6 +21,14 @@
*/
class Response extends HttpResponse
{
/**
* The current used version
* (The value will be detected on getVersion)
*
* @var null|string
*/
protected $version;

/**
* @var bool
*/
Expand All @@ -31,6 +39,37 @@ class Response extends HttpResponse
*/
protected $contentSent = false;

/**
* Return the HTTP version for this response
*
* @return string
* @see \Zend\Http\AbstractMessage::getVersion()
*/
public function getVersion()
{
if (!$this->version) {
$this->version = $this->detectVersion();
}
return $this->version;
}

/**
* Detect the current used protocol version.
* If detection failed it falls back to version 1.0.
*
* @return string
*/
protected function detectVersion()
{
if (isset($_SERVER['SERVER_PROTOCOL'])) {
if ($_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.1') {
return self::VERSION_11;
}
}

return self::VERSION_10;
}

/**
* @return bool
*/
Expand Down
105 changes: 105 additions & 0 deletions test/PhpEnvironment/ResponseTest.php
@@ -0,0 +1,105 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Http
* @subpackage UnitTest
*/

namespace ZendTest\Http\PhpEnvironment;

use PHPUnit_Framework_TestCase as TestCase;
use Zend\Http\PhpEnvironment\Response;

class ResponseTest extends TestCase
{
/**
* Original environemnt
*
* @var array
*/
protected $originalEnvironment;

/**
* Save the original environment and set up a clean one.
*/
public function setUp()
{
$this->originalEnvironment = array(
'post' => $_POST,
'get' => $_GET,
'cookie' => $_COOKIE,
'server' => $_SERVER,
'env' => $_ENV,
'files' => $_FILES,
);

$_POST = array();
$_GET = array();
$_COOKIE = array();
$_SERVER = array();
$_ENV = array();
$_FILES = array();
}

/**
* Restore the original environment
*/
public function tearDown()
{
$_POST = $this->originalEnvironment['post'];
$_GET = $this->originalEnvironment['get'];
$_COOKIE = $this->originalEnvironment['cookie'];
$_SERVER = $this->originalEnvironment['server'];
$_ENV = $this->originalEnvironment['env'];
$_FILES = $this->originalEnvironment['files'];
}

public function testReturnsOneOhVersionWhenDetectedInServerSuperglobal()
{
// HTTP/1.0
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.0';
$response = new Response();
$this->assertSame(Response::VERSION_10, $response->getVersion());
}

public function testReturnsOneOneVersionWhenDetectedInServerSuperglobal()
{
// HTTP/1.1
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
$response = new Response();
$this->assertSame(Response::VERSION_11, $response->getVersion());
}

public function testFallsBackToVersionOneOhWhenServerSuperglobalVersionIsNotRecognized()
{
// unknown protocol or version -> fallback to HTTP/1.0
$_SERVER['SERVER_PROTOCOL'] = 'zf/2.0';
$response = new Response();
$this->assertSame(Response::VERSION_10, $response->getVersion());
}

public function testFallsBackToVersionOneOhWhenNoVersionDetectedInServerSuperglobal()
{
// undefined -> fallback to HTTP/1.0
unset($_SERVER['SERVER_PROTOCOL']);
$response = new Response();
$this->assertSame(Response::VERSION_10, $response->getVersion());
}

public function testCanExplicitlySetVersion()
{
$response = new Response();
$response->setVersion(Response::VERSION_11);
$this->assertSame(Response::VERSION_11, $response->getVersion());

$response->setVersion(Response::VERSION_10);
$this->assertSame(Response::VERSION_10, $response->getVersion());

$this->setExpectedException('Zend\Http\Exception\InvalidArgumentException');
$response->setVersion('zf/2.0');
}
}

0 comments on commit f1f6e0d

Please sign in to comment.