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.
- Loading branch information
Showing
7 changed files
with
258 additions
and
206 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
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,52 @@ | ||
<?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 InvalidArgumentException; | ||
use Psr\Http\Message\StreamInterface; | ||
use Psr\Http\Message\UriInterface; | ||
use Zend\Diactoros\Response; | ||
|
||
/** | ||
* Produce a HTML response. | ||
*/ | ||
class HtmlResponse extends Response | ||
{ | ||
/** | ||
* Create a HTML response. | ||
* | ||
* Produces a simple response with the string passed in parameter. | ||
* MIME Content-type is text/html | ||
* | ||
* @param string|StreamInterface $body The body of the response. | ||
* @param int $status Integer status code for the response; 200 by default. | ||
* @param array $headers Array of headers to use at initialization. | ||
*/ | ||
public function __construct($body, $status = 200, array $headers = []) | ||
{ | ||
if (! is_string($body) && ! $body instanceof StreamInterface) { | ||
throw new InvalidArgumentException(sprintf( | ||
'Body provided to %s MUST be a string or Psr\Http\Message\StreamInterface instance; received "%s"', | ||
__CLASS__, | ||
(is_object($body) ? get_class($body) : gettype($body)) | ||
)); | ||
} | ||
|
||
if (!isset($headers['content-type'])) { | ||
$headers['content-type'] = 'text/html'; | ||
} | ||
|
||
if ($body instanceof StreamInterface) { | ||
parent::__construct($body, $status, $headers); | ||
} else { | ||
parent::__construct('php://temp', $status, $headers); | ||
$this->getBody()->write($body); | ||
} | ||
} | ||
} |
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,53 @@ | ||
<?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 InvalidArgumentException; | ||
use Psr\Http\Message\StreamInterface; | ||
use Psr\Http\Message\UriInterface; | ||
use Zend\Diactoros\Response; | ||
|
||
/** | ||
* Produce a Json response. | ||
*/ | ||
class JsonResponse extends Response | ||
{ | ||
/** | ||
* Create a Json response. | ||
* | ||
* Produces a JSON response, serialized from $data passed in parameter. | ||
* MIME Content-type is application/json | ||
* | ||
* Default JSON encoding is performed with JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT options | ||
* (RFC4627-compliant JSON, which may also be embedded into HTML) | ||
* | ||
* @param mixed $data The response data, to be serialized in JSON. | ||
* @param int $status Integer status code for the response; 200 by default. | ||
* @param array $headers Array of headers to use at initialization. | ||
* @param int $encodingOptions JSON Encoding parameters. | ||
* @throws \Exception | ||
*/ | ||
public function __construct($data, $status = 200, array $headers = [], $encodingOptions = 15) | ||
{ | ||
// Clear json_last_error() | ||
json_encode(null); | ||
$json = json_encode($data, $encodingOptions); | ||
|
||
if (JSON_ERROR_NONE !== json_last_error()) { | ||
throw new \InvalidArgumentException(json_last_error_msg()); | ||
} | ||
|
||
if (!isset($headers['content-type'])) { | ||
$headers['content-type'] = 'application/json'; | ||
} | ||
|
||
parent::__construct('php://temp', $status, $headers); | ||
$this->getBody()->write($json); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,60 @@ | ||
<?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\HtmlResponse; | ||
use Zend\Diactoros\Response\StringResponse; | ||
use Zend\Diactoros\Stream; | ||
|
||
class HtmlResponseTest extends TestCase | ||
{ | ||
public function testHtmlConstructor() | ||
{ | ||
$body = '<html>Uh oh not found</html>'; | ||
$status = 404; | ||
$headers = [ | ||
'x-custom' => [ 'foo-bar' ], | ||
]; | ||
|
||
$response = new HtmlResponse($body, $status, $headers); | ||
$this->assertSame($body, (string) $response->getBody()); | ||
$this->assertEquals(404, $response->getStatusCode()); | ||
$this->assertEquals(['foo-bar'], $response->getHeader('x-custom')); | ||
$this->assertEquals('text/html', $response->getHeaderLine('content-type')); | ||
} | ||
|
||
public function testHtmlConstructorWithStream() | ||
{ | ||
$body = '<html>Hello world</html>'; | ||
$stream = new Stream("php://memory", "wb+"); | ||
$stream->write($body); | ||
|
||
$response = new HtmlResponse($stream); | ||
$this->assertSame($body, (string) $response->getBody()); | ||
} | ||
|
||
public function testContentTypeCanBeOverwritten() | ||
{ | ||
$body = '<html>Uh oh not found</html>'; | ||
|
||
$response = new HtmlResponse($body, 200, ['content-type' => 'foo/html']); | ||
$this->assertSame($body, (string) $response->getBody()); | ||
$this->assertEquals('foo/html', $response->getHeaderLine('content-type')); | ||
} | ||
|
||
/** | ||
* @expectedException InvalidArgumentException | ||
*/ | ||
public function testInvalidContent() | ||
{ | ||
new HtmlResponse(new \stdClass()); | ||
} | ||
} |
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,55 @@ | ||
<?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\JsonResponse; | ||
use Zend\Diactoros\Response\StringResponse; | ||
use ZendTest\Diactoros\TestAsset\JsonSerializableObject; | ||
|
||
class JsonResponseTest extends TestCase | ||
{ | ||
public function testJsonConstructor() | ||
{ | ||
$data = [ | ||
'nested' => [ | ||
'json' => [ | ||
'tree', | ||
], | ||
], | ||
]; | ||
$json = '{"nested":{"json":["tree"]}}'; | ||
|
||
$response = new JsonResponse($data); | ||
$this->assertEquals(200, $response->getStatusCode()); | ||
$this->assertEquals('application/json', $response->getHeaderLine('content-type')); | ||
$this->assertSame($json, (string) $response->getBody()); | ||
} | ||
|
||
public function testContentTypeCanBeOverwritten() | ||
{ | ||
$data = []; | ||
$json = '[]'; | ||
|
||
$response = new JsonResponse($data, 200, ['content-type' => 'foo/json']); | ||
$this->assertSame($json, (string) $response->getBody()); | ||
$this->assertEquals('foo/json', $response->getHeaderLine('content-type')); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
*/ | ||
public function testJsonErrorHandling() | ||
{ | ||
// Serializing something that is not serializable. | ||
$resource = fopen("php://memory", "r"); | ||
new JsonResponse($resource); | ||
} | ||
} |
Oops, something went wrong.