This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from franzliedke/patch-2
Add shortcut method for empty responses
- Loading branch information
Showing
3 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @see http://github.com/zendframework/zend-diactoros for the canonical source repository | ||
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace Zend\Diactoros\Response; | ||
|
||
use Zend\Diactoros\Response; | ||
use Zend\Diactoros\Stream; | ||
|
||
/** | ||
* A class representing empty HTTP responses. | ||
*/ | ||
class EmptyResponse extends Response | ||
{ | ||
/** | ||
* Create an empty response with the given status code. | ||
* | ||
* @param int $status Status code for the response, if any. | ||
* @param array $headers Headers for the response, if any. | ||
*/ | ||
public function __construct($status = 204, array $headers = []) | ||
{ | ||
$body = new Stream('php://temp', 'r'); | ||
parent::__construct($body, $status, $headers); | ||
} | ||
|
||
/** | ||
* Create an empty response with the given headers. | ||
* | ||
* @param array $headers Headers for the response. | ||
* @return EmptyResponse | ||
*/ | ||
public static function withHeaders(array $headers) | ||
{ | ||
return new static(204, $headers); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @see http://github.com/zendframework/zend-diactoros for the canonical source repository | ||
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace ZendTest\Diactoros\Response; | ||
|
||
use PHPUnit_Framework_TestCase as TestCase; | ||
use Zend\Diactoros\Response\EmptyResponse; | ||
|
||
class EmptyResponseTest extends TestCase | ||
{ | ||
public function testConstructor() | ||
{ | ||
$response = new EmptyResponse(201); | ||
$this->assertInstanceOf('Zend\Diactoros\Response', $response); | ||
$this->assertEquals('', (string) $response->getBody()); | ||
$this->assertEquals(201, $response->getStatusCode()); | ||
} | ||
|
||
public function testHeaderConstructor() | ||
{ | ||
$response = EmptyResponse::withHeaders(['x-empty' => ['true']]); | ||
$this->assertInstanceOf('Zend\Diactoros\Response', $response); | ||
$this->assertEquals('', (string) $response->getBody()); | ||
$this->assertEquals(204, $response->getStatusCode()); | ||
$this->assertEquals('true', $response->getHeaderLine('x-empty')); | ||
} | ||
} |