mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-02 11:48:08 +08:00
Added testing.
This commit is contained in:
parent
52b15ee87c
commit
57d45b8d44
@ -13,8 +13,6 @@ declare(strict_types=1);
|
||||
namespace Hyperf\HttpServer;
|
||||
|
||||
use BadMethodCallException;
|
||||
use Hyperf\HttpMessage\Cookie\Cookie;
|
||||
use Hyperf\HttpMessage\Server\Response as ServerResponse;
|
||||
use Hyperf\HttpMessage\Stream\SwooleStream;
|
||||
use Hyperf\HttpServer\Contract\ResponseInterface;
|
||||
use Hyperf\HttpServer\Exception\Http\EncodingException;
|
||||
@ -28,18 +26,9 @@ use Hyperf\Utils\Traits\Macroable;
|
||||
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
use SimpleXMLElement;
|
||||
use Swoole\Http\Response as SwooleResponse;
|
||||
use function get_class;
|
||||
|
||||
/**
|
||||
* @method void send()
|
||||
* @method ServerResponse withContent(string $content)
|
||||
* @method ServerResponse withCookie(Cookie $cookie)
|
||||
* @method null|SwooleResponse getSwooleResponse()
|
||||
* @method ServerResponse setSwooleResponse(SwooleResponse $swooleResponse)
|
||||
* @method void buildSwooleResponse(SwooleResponse $swooleResponse, ServerResponse $response)
|
||||
*/
|
||||
class Response implements ResponseInterface
|
||||
class Response implements PsrResponseInterface, ResponseInterface
|
||||
{
|
||||
use Macroable;
|
||||
|
||||
|
@ -12,6 +12,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace HyperfTest\HttpServer;
|
||||
|
||||
use Hyperf\HttpMessage\Stream\SwooleStream;
|
||||
use Hyperf\HttpServer\Response;
|
||||
use Hyperf\Utils\ApplicationContext;
|
||||
use Hyperf\Utils\Context;
|
||||
@ -129,4 +130,47 @@ class ResponseTest extends TestCase
|
||||
};
|
||||
$this->assertSame($expected, $reflectionMethod->invoke($response, $xmlable));
|
||||
}
|
||||
|
||||
public function testToJson()
|
||||
{
|
||||
$container = Mockery::mock(ContainerInterface::class);
|
||||
ApplicationContext::setContainer($container);
|
||||
|
||||
$psrResponse = new \Hyperf\HttpMessage\Base\Response();
|
||||
Context::set(PsrResponseInterface::class, $psrResponse);
|
||||
|
||||
$response = new Response();
|
||||
$json = $response->json([
|
||||
'kstring' => 'string',
|
||||
'kint1' => 1,
|
||||
'kint0' => 0,
|
||||
'kfloat' => 0.12345,
|
||||
'kfalse' => false,
|
||||
'ktrue' => true,
|
||||
'karray' => [
|
||||
'kstring' => 'string',
|
||||
'kint1' => 1,
|
||||
'kint0' => 0,
|
||||
'kfloat' => 0.12345,
|
||||
'kfalse' => false,
|
||||
'ktrue' => true,
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertSame('{"kstring":"string","kint1":1,"kint0":0,"kfloat":0.12345,"kfalse":false,"ktrue":true,"karray":{"kstring":"string","kint1":1,"kint0":0,"kfloat":0.12345,"kfalse":false,"ktrue":true}}', $json->getBody()->getContents());
|
||||
}
|
||||
|
||||
public function testPsrResponse()
|
||||
{
|
||||
$container = Mockery::mock(ContainerInterface::class);
|
||||
ApplicationContext::setContainer($container);
|
||||
|
||||
$psrResponse = new \Hyperf\HttpMessage\Base\Response();
|
||||
Context::set(PsrResponseInterface::class, $psrResponse);
|
||||
|
||||
$response = new Response();
|
||||
$response = $response->withBody(new SwooleStream('xxx'));
|
||||
|
||||
$this->assertInstanceOf(PsrResponseInterface::class, $response);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user