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

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

src/PhpEnvironment/Response.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
*/
2222
class Response extends HttpResponse
2323
{
24+
/**
25+
* The current used version
26+
* (The value will be detected on getVersion)
27+
*
28+
* @var null|string
29+
*/
30+
protected $version;
31+
2432
/**
2533
* @var bool
2634
*/
@@ -31,6 +39,37 @@ class Response extends HttpResponse
3139
*/
3240
protected $contentSent = false;
3341

42+
/**
43+
* Return the HTTP version for this response
44+
*
45+
* @return string
46+
* @see \Zend\Http\AbstractMessage::getVersion()
47+
*/
48+
public function getVersion()
49+
{
50+
if (!$this->version) {
51+
$this->version = $this->detectVersion();
52+
}
53+
return $this->version;
54+
}
55+
56+
/**
57+
* Detect the current used protocol version.
58+
* If detection failed it falls back to version 1.0.
59+
*
60+
* @return string
61+
*/
62+
protected function detectVersion()
63+
{
64+
if (isset($_SERVER['SERVER_PROTOCOL'])) {
65+
if ($_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.1') {
66+
return self::VERSION_11;
67+
}
68+
}
69+
70+
return self::VERSION_10;
71+
}
72+
3473
/**
3574
* @return bool
3675
*/

test/PhpEnvironment/ResponseTest.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
* @package Zend_Http
9+
* @subpackage UnitTest
10+
*/
11+
12+
namespace ZendTest\Http\PhpEnvironment;
13+
14+
use PHPUnit_Framework_TestCase as TestCase;
15+
use Zend\Http\PhpEnvironment\Response;
16+
17+
class ResponseTest extends TestCase
18+
{
19+
/**
20+
* Original environemnt
21+
*
22+
* @var array
23+
*/
24+
protected $originalEnvironment;
25+
26+
/**
27+
* Save the original environment and set up a clean one.
28+
*/
29+
public function setUp()
30+
{
31+
$this->originalEnvironment = array(
32+
'post' => $_POST,
33+
'get' => $_GET,
34+
'cookie' => $_COOKIE,
35+
'server' => $_SERVER,
36+
'env' => $_ENV,
37+
'files' => $_FILES,
38+
);
39+
40+
$_POST = array();
41+
$_GET = array();
42+
$_COOKIE = array();
43+
$_SERVER = array();
44+
$_ENV = array();
45+
$_FILES = array();
46+
}
47+
48+
/**
49+
* Restore the original environment
50+
*/
51+
public function tearDown()
52+
{
53+
$_POST = $this->originalEnvironment['post'];
54+
$_GET = $this->originalEnvironment['get'];
55+
$_COOKIE = $this->originalEnvironment['cookie'];
56+
$_SERVER = $this->originalEnvironment['server'];
57+
$_ENV = $this->originalEnvironment['env'];
58+
$_FILES = $this->originalEnvironment['files'];
59+
}
60+
61+
public function testReturnsOneOhVersionWhenDetectedInServerSuperglobal()
62+
{
63+
// HTTP/1.0
64+
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.0';
65+
$response = new Response();
66+
$this->assertSame(Response::VERSION_10, $response->getVersion());
67+
}
68+
69+
public function testReturnsOneOneVersionWhenDetectedInServerSuperglobal()
70+
{
71+
// HTTP/1.1
72+
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
73+
$response = new Response();
74+
$this->assertSame(Response::VERSION_11, $response->getVersion());
75+
}
76+
77+
public function testFallsBackToVersionOneOhWhenServerSuperglobalVersionIsNotRecognized()
78+
{
79+
// unknown protocol or version -> fallback to HTTP/1.0
80+
$_SERVER['SERVER_PROTOCOL'] = 'zf/2.0';
81+
$response = new Response();
82+
$this->assertSame(Response::VERSION_10, $response->getVersion());
83+
}
84+
85+
public function testFallsBackToVersionOneOhWhenNoVersionDetectedInServerSuperglobal()
86+
{
87+
// undefined -> fallback to HTTP/1.0
88+
unset($_SERVER['SERVER_PROTOCOL']);
89+
$response = new Response();
90+
$this->assertSame(Response::VERSION_10, $response->getVersion());
91+
}
92+
93+
public function testCanExplicitlySetVersion()
94+
{
95+
$response = new Response();
96+
$response->setVersion(Response::VERSION_11);
97+
$this->assertSame(Response::VERSION_11, $response->getVersion());
98+
99+
$response->setVersion(Response::VERSION_10);
100+
$this->assertSame(Response::VERSION_10, $response->getVersion());
101+
102+
$this->setExpectedException('Zend\Http\Exception\InvalidArgumentException');
103+
$response->setVersion('zf/2.0');
104+
}
105+
}

0 commit comments

Comments
 (0)