-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
修复Json Validator会失效的BUG (swoft-cloud/swoft-component#153)
* 修复Json Validator无法使用的BUG * 完善单测 * 替换composer国内源为laravel-china
- Loading branch information
1 parent
d1db1cf
commit 42be799
Showing
7 changed files
with
258 additions
and
11 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
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 |
---|---|---|
|
@@ -3,15 +3,179 @@ | |
namespace SwoftTest\HttpServer; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Swoft\App; | ||
use Swoft\Helper\ArrayHelper; | ||
use Swoft\Testing\SwooleRequest as TestSwooleRequest; | ||
use Swoft\Testing\SwooleResponse as TestSwooleResponse; | ||
use Swoft\Http\Message\Testing\Web\Request; | ||
use Swoft\Http\Message\Testing\Web\Response; | ||
|
||
/** | ||
* @uses AbstractTestCase | ||
* @version 2017年11月03日 | ||
* @author huangzhhui <[email protected]> | ||
* @copyright Copyright 2010-2017 Swoft software | ||
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt} | ||
* Class AbstractTestCase | ||
* | ||
* @package Swoft\Test\Cases | ||
*/ | ||
abstract class AbstractTestCase extends TestCase | ||
class AbstractTestCase extends TestCase | ||
{ | ||
const ACCEPT_VIEW = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'; | ||
|
||
const ACCEPT_JSON = 'application/json'; | ||
|
||
const ACCEPT_RAW = 'text/plain'; | ||
|
||
/** | ||
* Send a mock request | ||
* | ||
* @param string $method | ||
* @param string $uri | ||
* @param array $parameters | ||
* @param string $accept | ||
* @param array $headers | ||
* @param string $rawContent | ||
* @return bool|\Swoft\Http\Message\Testing\Web\Response | ||
*/ | ||
public function request( | ||
string $method, | ||
string $uri, | ||
array $parameters = [], | ||
string $accept = self::ACCEPT_JSON, | ||
array $headers = [], | ||
string $rawContent = '' | ||
) { | ||
$method = strtoupper($method); | ||
$swooleResponse = new TestSwooleResponse(); | ||
$swooleRequest = new TestSwooleRequest(); | ||
|
||
$this->buildMockRequest($method, $uri, $parameters, $accept, $swooleRequest, $headers); | ||
|
||
$swooleRequest->setRawContent($rawContent); | ||
|
||
$request = Request::loadFromSwooleRequest($swooleRequest); | ||
$response = new Response($swooleResponse); | ||
|
||
/** @var \Swoft\Http\Server\ServerDispatcher $dispatcher */ | ||
$dispatcher = App::getBean('serverDispatcher'); | ||
return $dispatcher->dispatch($request, $response); | ||
} | ||
|
||
/** | ||
* Send a mock json request | ||
* | ||
* @param string $method | ||
* @param string $uri | ||
* @param array $parameters | ||
* @param array $headers | ||
* @param string $rawContent | ||
* @return bool|\Swoft\Http\Message\Testing\Web\Response | ||
*/ | ||
public function json( | ||
string $method, | ||
string $uri, | ||
array $parameters = [], | ||
array $headers = [], | ||
string $rawContent = '' | ||
) { | ||
return $this->request($method, $uri, $parameters, self::ACCEPT_JSON, $headers, $rawContent); | ||
} | ||
|
||
/** | ||
* Send a mock view request | ||
* | ||
* @param string $method | ||
* @param string $uri | ||
* @param array $parameters | ||
* @param array $headers | ||
* @param string $rawContent | ||
* @return bool|\Swoft\Http\Message\Testing\Web\Response | ||
*/ | ||
public function view( | ||
string $method, | ||
string $uri, | ||
array $parameters = [], | ||
array $headers = [], | ||
string $rawContent = '' | ||
) { | ||
return $this->request($method, $uri, $parameters, self::ACCEPT_VIEW, $headers, $rawContent); | ||
} | ||
|
||
/** | ||
* Send a mock raw content request | ||
* | ||
* @param string $method | ||
* @param string $uri | ||
* @param array $parameters | ||
* @param array $headers | ||
* @param string $rawContent | ||
* @return bool|\Swoft\Http\Message\Testing\Web\Response | ||
*/ | ||
public function raw( | ||
string $method, | ||
string $uri, | ||
array $parameters = [], | ||
array $headers = [], | ||
string $rawContent = '' | ||
) { | ||
return $this->request($method, $uri, $parameters, self::ACCEPT_RAW, $headers, $rawContent); | ||
} | ||
|
||
/** | ||
* @param string $method | ||
* @param string $uri | ||
* @param array $parameters | ||
* @param string $accept | ||
* @param \Swoole\Http\Request $swooleRequest | ||
* @param array $headers | ||
*/ | ||
protected function buildMockRequest( | ||
string $method, | ||
string $uri, | ||
array $parameters, | ||
string $accept, | ||
&$swooleRequest, | ||
array $headers = [] | ||
) { | ||
$urlAry = parse_url($uri); | ||
$urlParams = []; | ||
if (isset($urlAry['query'])) { | ||
parse_str($urlAry['query'], $urlParams); | ||
} | ||
$defaultHeaders = [ | ||
'host' => '127.0.0.1', | ||
'connection' => 'keep-alive', | ||
'cache-control' => 'max-age=0', | ||
'user-agent' => 'PHPUnit', | ||
'upgrade-insecure-requests' => '1', | ||
'accept' => $accept, | ||
'dnt' => '1', | ||
'accept-encoding' => 'gzip, deflate, br', | ||
'accept-language' => 'zh-CN,zh;q=0.8,en;q=0.6,it-IT;q=0.4,it;q=0.2', | ||
]; | ||
|
||
$swooleRequest->fd = 1; | ||
$swooleRequest->header = ArrayHelper::merge($headers, $defaultHeaders); | ||
$swooleRequest->server = [ | ||
'request_method' => $method, | ||
'request_uri' => $uri, | ||
'path_info' => '/', | ||
'request_time' => microtime(), | ||
'request_time_float' => microtime(true), | ||
'server_port' => 80, | ||
'remote_port' => 54235, | ||
'remote_addr' => '10.0.2.2', | ||
'master_time' => microtime(), | ||
'server_protocol' => 'HTTP/1.1', | ||
'server_software' => 'swoole-http-server', | ||
]; | ||
|
||
if ($method == 'GET') { | ||
$swooleRequest->get = $parameters; | ||
} elseif ($method == 'POST') { | ||
$swooleRequest->post = $parameters; | ||
} | ||
|
||
if (! empty($urlParams)) { | ||
$get = empty($swooleRequest->get) ? [] : $swooleRequest->get; | ||
$swooleRequest->get = array_merge($urlParams, $get); | ||
} | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
namespace SwoftTest\HttpServer; | ||
|
||
use Swoft\Helper\JsonHelper; | ||
|
||
class ValidatorTest extends AbstractTestCase | ||
{ | ||
public function testDemo() | ||
{ | ||
$headers = [ | ||
'Content-Type' => 'application/json' | ||
]; | ||
$raw = JsonHelper::encode([ | ||
'test' => [ | ||
'id' => 1 | ||
] | ||
]); | ||
$res = $this->raw('POST', '/validator/json', [], $headers, $raw)->getBody()->getContents(); | ||
$this->assertEquals('[1,"limx"]', $res); | ||
|
||
$headers = [ | ||
'Content-Type' => 'application/json;charset=UTF-8' | ||
]; | ||
$raw = JsonHelper::encode([ | ||
'test' => [ | ||
'id' => 1 | ||
] | ||
]); | ||
$res = $this->raw('POST', '/validator/json', [], $headers, $raw)->getBody()->getContents(); | ||
$this->assertEquals('[1,"limx"]', $res); | ||
} | ||
|
||
|
||
} |
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,30 @@ | ||
<?php | ||
namespace SwoftTest\Testing\Controllers; | ||
|
||
use Swoft\Http\Message\Server\Request; | ||
use Swoft\Http\Server\Bean\Annotation\Controller; | ||
use Swoft\Http\Server\Bean\Annotation\RequestMapping; | ||
use Swoft\Http\Server\Bean\Annotation\RequestMethod; | ||
use Swoft\Bean\Annotation\Number; | ||
use Swoft\Bean\Annotation\Strings; | ||
use Swoft\Http\Message\Server\Response; | ||
|
||
/** | ||
* Class ValidatorController | ||
* @Controller(prefix="/validator") | ||
*/ | ||
class ValidatorController | ||
{ | ||
/** | ||
* @Number(name="test.id", max=10) | ||
* @Strings(name="test.name", default="limx") | ||
* @RequestMapping(route="json", method=RequestMethod::POST) | ||
*/ | ||
public function json(Request $request, Response $response) | ||
{ | ||
$id = $request->json('test.id'); | ||
$name = $request->json('test.name'); | ||
|
||
return $response->json([$id, $name]); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,17 @@ | ||
<?php | ||
|
||
return []; | ||
use Swoft\Http\Server\Parser\RequestParser; | ||
use Swoft\Http\Server\Router\HandlerMapping; | ||
use Swoft\Http\Server\ServerDispatcher; | ||
|
||
return [ | ||
'serverDispatcher' => [ | ||
'class' => ServerDispatcher::class, | ||
], | ||
'httpRouter' => [ | ||
'class' => HandlerMapping::class, | ||
], | ||
'requestParser' => [ | ||
'class' => RequestParser::class, | ||
], | ||
]; |
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