Skip to content

Loading…

Fix for issue #5629 #5732

Closed
wants to merge 3 commits into from

3 participants

@Martin-P

My first pull request, so I hope I did everything correctly :smiley:

  • Reverted #5013
  • Created new method to set custom status codes
  • Fixed "Notice: Undefined offset" (#5629)
@samsonasik samsonasik commented on an outdated diff
tests/ZendTest/Http/ResponseTest.php
@@ -5,6 +5,7 @@
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Http

no need @package here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@samsonasik samsonasik commented on an outdated diff
library/Zend/Http/Response.php
@@ -261,6 +262,28 @@ public function getStatusCode()
}
/**
+ * Set custom HTTP status code
+ *
+ * @param int $code
+ * @throws Exception\InvalidArgumentException
+ * @return Response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@weierophinney weierophinney added this to the 2.3.0 milestone
@weierophinney weierophinney commented on the diff
tests/ZendTest/Http/ResponseTest.php
((6 lines not shown))
- $this->assertEquals(550, $response->getStatusCode());
- }
-
- /**
- * @group 5253
- */
- public function testMultilineHeaderNoSpaces()
- {
- $response = Response::fromString($this->readResponse('response_multiline_header_nospace'));
-
- // Make sure we got the corrent no. of headers
- $this->assertEquals(6, count($response->getHeaders()), 'Header count is expected to be 6');
-
- // Check header integrity
- $this->assertEquals('timeout=15,max=100', $response->getHeaders()->get('keep-alive')->getFieldValue());
- $this->assertEquals('text/html;charset=iso-8859-1', $response->getHeaders()->get('content-type')->getFieldValue());
}
@weierophinney Zend Framework member

Please do not remove tests.

@Martin-P
Martin-P added a note

Sorry, I must have accidently removed that test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@weierophinney weierophinney added a commit that referenced this pull request
@weierophinney weierophinney Merge branch 'feature/5732' into develop
Close #5732
Fixes #5629
6e2bf93
@weierophinney
Zend Framework member

Merged to develop for release with 2.3.0.

@Martin-P Martin-P deleted the Martin-P:issue-5629 branch
@weierophinney weierophinney referenced this pull request
Commit has since been removed from the repository and is no longer available.
@weierophinney weierophinney referenced this pull request
Commit has since been removed from the repository and is no longer available.
@weierophinney weierophinney added a commit to zendframework/zend-http that referenced this pull request
@weierophinney weierophinney Merge pull request zendframework/zf2#5732 from Martin-P/issue-5629
Fix for issue zendframework/zf2#5629

Conflicts:
	tests/ZendTest/Http/ResponseTest.php
26df3d9
@weierophinney weierophinney added a commit to zendframework/zend-http that referenced this pull request
@weierophinney weierophinney Merge branch 'feature/5732' into develop 7aa497d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 20, 2014
  1. @Martin-P

    Fix for issue #5629

    Martin-P committed
  2. @Martin-P

    Removed phpunit.xml

    Martin-P committed
  3. @Martin-P
This page is out of date. Refresh to see the latest.
Showing with 49 additions and 26 deletions.
  1. +28 −5 library/Zend/Http/Response.php
  2. +21 −21 tests/ZendTest/Http/ResponseTest.php
View
33 library/Zend/Http/Response.php
@@ -164,7 +164,7 @@ class Response extends AbstractMessage implements ResponseInterface
* Populate object from string
*
* @param string $string
- * @return Response
+ * @return self
* @throws Exception\InvalidArgumentException
*/
public static function fromString($string)
@@ -235,11 +235,12 @@ public function getCookie()
*
* @param int $code
* @throws Exception\InvalidArgumentException
- * @return Response
+ * @return self
*/
public function setStatusCode($code)
{
- if (!is_numeric($code)) {
+ $const = get_class($this) . '::STATUS_CODE_' . $code;
+ if (!is_numeric($code) || !defined($const)) {
$code = is_scalar($code) ? $code : gettype($code);
throw new Exception\InvalidArgumentException(sprintf(
'Invalid status code provided: "%s"',
@@ -261,8 +262,30 @@ public function getStatusCode()
}
/**
+ * Set custom HTTP status code
+ *
+ * @param int $code
+ * @throws Exception\InvalidArgumentException
+ * @return self
+ */
+ public function setCustomStatusCode($code)
+ {
+ if (!is_numeric($code)) {
+ $code = is_scalar($code) ? $code : gettype($code);
+ throw new Exception\InvalidArgumentException(sprintf(
+ 'Invalid status code provided: "%s"',
+ $code
+ ));
+ }
+
+ $this->statusCode = (int) $code;
+ return $this;
+
+ }
+
+ /**
* @param string $reasonPhrase
- * @return Response
+ * @return self
*/
public function setReasonPhrase($reasonPhrase)
{
@@ -277,7 +300,7 @@ public function setReasonPhrase($reasonPhrase)
*/
public function getReasonPhrase()
{
- if ($this->reasonPhrase == null) {
+ if (null == $this->reasonPhrase and isset($this->recommendedReasonPhrases[$this->statusCode])) {
return $this->recommendedReasonPhrases[$this->statusCode];
}
return $this->reasonPhrase;
View
42 tests/ZendTest/Http/ResponseTest.php
@@ -57,14 +57,29 @@ public function testResponseCanSetStatusCode()
$this->assertEquals(303, $response->getStatusCode());
}
- public function testResponseSetStatusCodeWithUnknownCode()
+ public function testResponseSetStatusCodeThrowsExceptionOnInvalidCode()
{
$response = new Response;
+ $this->setExpectedException('Zend\Http\Exception\InvalidArgumentException', 'Invalid status code');
$response->setStatusCode(606);
- $this->assertEquals(606, $response->getStatusCode());
}
- public function testResponseSetStatusCodeThrowsExceptionOnInvalidCode()
+ public function testResponseGetReasonPhraseWillReturnEmptyPhraseAsDefault()
+ {
+ $response = new Response;
+ $response->setCustomStatusCode(998);
+ $this->assertSame('HTTP/1.1 998' . "\r\n\r\n", (string) $response);
+ }
+
+ public function testResponseCanSetCustomStatusCode()
+ {
+ $response = new Response;
+ $this->assertEquals(200, $response->getStatusCode());
+ $response->setCustomStatusCode('999');
+ $this->assertEquals(999, $response->getStatusCode());
+ }
+
+ public function testResponseSetCustomStatusCodeThrowsExceptionOnInvalidCode()
{
$response = new Response;
$this->setExpectedException(
@@ -281,7 +296,7 @@ public function testToString()
$response = Response::fromString($response_str);
$this->assertEquals(strtolower(str_replace("\n", "\r\n", $response_str)), strtolower($response->toString()), 'Response convertion to string does not match original string');
- $this->assertEquals(strtolower(str_replace("\n", "\r\n", $response_str)), strtolower((string) $response), 'Response convertion to string does not match original string');
+ $this->assertEquals(strtolower(str_replace("\n", "\r\n", $response_str)), strtolower((string)$response), 'Response convertion to string does not match original string');
}
public function testToStringGzip()
@@ -290,7 +305,7 @@ public function testToStringGzip()
$response = Response::fromString($response_str);
$this->assertEquals(strtolower($response_str), strtolower($response->toString()), 'Response convertion to string does not match original string');
- $this->assertEquals(strtolower($response_str), strtolower((string) $response), 'Response convertion to string does not match original string');
+ $this->assertEquals(strtolower($response_str), strtolower((string)$response), 'Response convertion to string does not match original string');
}
public function testGetHeaders()
@@ -312,23 +327,8 @@ public function testGetVersion()
public function testUnknownCode()
{
$response_str = $this->readResponse('response_unknown');
+ $this->setExpectedException('InvalidArgumentException', 'Invalid status code provided: "550"');
$response = Response::fromString($response_str);
- $this->assertEquals(550, $response->getStatusCode());
- }
-
- /**
- * @group 5253
- */
- public function testMultilineHeaderNoSpaces()
- {
- $response = Response::fromString($this->readResponse('response_multiline_header_nospace'));
-
- // Make sure we got the corrent no. of headers
- $this->assertEquals(6, count($response->getHeaders()), 'Header count is expected to be 6');
-
- // Check header integrity
- $this->assertEquals('timeout=15,max=100', $response->getHeaders()->get('keep-alive')->getFieldValue());
- $this->assertEquals('text/html;charset=iso-8859-1', $response->getHeaders()->get('content-type')->getFieldValue());
}
@weierophinney Zend Framework member

Please do not remove tests.

@Martin-P
Martin-P added a note

Sorry, I must have accidently removed that test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
public function testMultilineHeader()
Something went wrong with that request. Please try again.