-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Squashed 'src/http-server/' changes from f54584ce..4b3176b2
4b3176b2 Upstream travis ci config (#160) 42be799f 修复Json Validator会失效的BUG (#153) d1db1cf1 修复执行php bin/swoft stop命令时master进程异常未退出的问题,以及停止失败后,pid文件被删除的问题 (#134) 649e67a4 Validator增加Json格式支持 (#122) git-subtree-dir: src/http-server git-subtree-split: 4b3176b2d43dba4a3f7db5ad754c017ab1d8d294
- Loading branch information
1 parent
5071dbf
commit 73e71e9
Showing
9 changed files
with
285 additions
and
17 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
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, | ||
], | ||
]; |
Oops, something went wrong.