Skip to content

Commit 42be799

Browse files
limingxinleohuangzhhui
authored andcommitted
修复Json Validator会失效的BUG (swoft-cloud/swoft-component#153)
* 修复Json Validator无法使用的BUG * 完善单测 * 替换composer国内源为laravel-china
1 parent d1db1cf commit 42be799

File tree

7 files changed

+258
-11
lines changed

7 files changed

+258
-11
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
},
2121
"autoload-dev": {
2222
"psr-4": {
23-
"SwoftTest\\HttpServer\\": "test/Cases"
23+
"SwoftTest\\HttpServer\\": "test/Cases",
24+
"SwoftTest\\Testing\\": "test/Testing"
2425
}
2526
},
2627
"repositories": [
2728
{
2829
"type": "composer",
29-
"url": "https://packagist.phpcomposer.com"
30+
"url": "https://packagist.laravel-china.org"
3031
}
3132
],
3233
"require-dev": {

src/Validator/HttpValidator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Swoft\Bean\Annotation\ValidatorFrom;
77
use Swoft\Helper\ArrayHelper;
88
use Swoft\Helper\JsonHelper;
9+
use Swoft\Helper\StringHelper;
910
use Swoft\Http\Message\Server\Request;
1011
use Swoft\Http\Message\Stream\SwooleStream;
1112
use Swoft\Validator\AbstractValidator;
@@ -58,7 +59,8 @@ private function validateField($request, array $matches, string $type, array $va
5859
$post = $request->getParsedBody();
5960
$contentType = $request->getHeader('content-type');
6061
$isPostJson = false;
61-
if ($contentType && \in_array('application/json', $contentType)) {
62+
63+
if (isset($contentType[0]) && StringHelper::startsWith($contentType[0], 'application/json')) {
6264
$isPostJson = true;
6365
$post = $request->json();
6466
}

test/Cases/AbstractTestCase.php

Lines changed: 170 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,179 @@
33
namespace SwoftTest\HttpServer;
44

55
use PHPUnit\Framework\TestCase;
6+
use Swoft\App;
7+
use Swoft\Helper\ArrayHelper;
8+
use Swoft\Testing\SwooleRequest as TestSwooleRequest;
9+
use Swoft\Testing\SwooleResponse as TestSwooleResponse;
10+
use Swoft\Http\Message\Testing\Web\Request;
11+
use Swoft\Http\Message\Testing\Web\Response;
612

713
/**
8-
* @uses AbstractTestCase
9-
* @version 2017年11月03日
10-
* @author huangzhhui <[email protected]>
11-
* @copyright Copyright 2010-2017 Swoft software
12-
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt}
14+
* Class AbstractTestCase
15+
*
16+
* @package Swoft\Test\Cases
1317
*/
14-
abstract class AbstractTestCase extends TestCase
18+
class AbstractTestCase extends TestCase
1519
{
20+
const ACCEPT_VIEW = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8';
1621

22+
const ACCEPT_JSON = 'application/json';
23+
24+
const ACCEPT_RAW = 'text/plain';
25+
26+
/**
27+
* Send a mock request
28+
*
29+
* @param string $method
30+
* @param string $uri
31+
* @param array $parameters
32+
* @param string $accept
33+
* @param array $headers
34+
* @param string $rawContent
35+
* @return bool|\Swoft\Http\Message\Testing\Web\Response
36+
*/
37+
public function request(
38+
string $method,
39+
string $uri,
40+
array $parameters = [],
41+
string $accept = self::ACCEPT_JSON,
42+
array $headers = [],
43+
string $rawContent = ''
44+
) {
45+
$method = strtoupper($method);
46+
$swooleResponse = new TestSwooleResponse();
47+
$swooleRequest = new TestSwooleRequest();
48+
49+
$this->buildMockRequest($method, $uri, $parameters, $accept, $swooleRequest, $headers);
50+
51+
$swooleRequest->setRawContent($rawContent);
52+
53+
$request = Request::loadFromSwooleRequest($swooleRequest);
54+
$response = new Response($swooleResponse);
55+
56+
/** @var \Swoft\Http\Server\ServerDispatcher $dispatcher */
57+
$dispatcher = App::getBean('serverDispatcher');
58+
return $dispatcher->dispatch($request, $response);
59+
}
60+
61+
/**
62+
* Send a mock json request
63+
*
64+
* @param string $method
65+
* @param string $uri
66+
* @param array $parameters
67+
* @param array $headers
68+
* @param string $rawContent
69+
* @return bool|\Swoft\Http\Message\Testing\Web\Response
70+
*/
71+
public function json(
72+
string $method,
73+
string $uri,
74+
array $parameters = [],
75+
array $headers = [],
76+
string $rawContent = ''
77+
) {
78+
return $this->request($method, $uri, $parameters, self::ACCEPT_JSON, $headers, $rawContent);
79+
}
80+
81+
/**
82+
* Send a mock view request
83+
*
84+
* @param string $method
85+
* @param string $uri
86+
* @param array $parameters
87+
* @param array $headers
88+
* @param string $rawContent
89+
* @return bool|\Swoft\Http\Message\Testing\Web\Response
90+
*/
91+
public function view(
92+
string $method,
93+
string $uri,
94+
array $parameters = [],
95+
array $headers = [],
96+
string $rawContent = ''
97+
) {
98+
return $this->request($method, $uri, $parameters, self::ACCEPT_VIEW, $headers, $rawContent);
99+
}
100+
101+
/**
102+
* Send a mock raw content request
103+
*
104+
* @param string $method
105+
* @param string $uri
106+
* @param array $parameters
107+
* @param array $headers
108+
* @param string $rawContent
109+
* @return bool|\Swoft\Http\Message\Testing\Web\Response
110+
*/
111+
public function raw(
112+
string $method,
113+
string $uri,
114+
array $parameters = [],
115+
array $headers = [],
116+
string $rawContent = ''
117+
) {
118+
return $this->request($method, $uri, $parameters, self::ACCEPT_RAW, $headers, $rawContent);
119+
}
120+
121+
/**
122+
* @param string $method
123+
* @param string $uri
124+
* @param array $parameters
125+
* @param string $accept
126+
* @param \Swoole\Http\Request $swooleRequest
127+
* @param array $headers
128+
*/
129+
protected function buildMockRequest(
130+
string $method,
131+
string $uri,
132+
array $parameters,
133+
string $accept,
134+
&$swooleRequest,
135+
array $headers = []
136+
) {
137+
$urlAry = parse_url($uri);
138+
$urlParams = [];
139+
if (isset($urlAry['query'])) {
140+
parse_str($urlAry['query'], $urlParams);
141+
}
142+
$defaultHeaders = [
143+
'host' => '127.0.0.1',
144+
'connection' => 'keep-alive',
145+
'cache-control' => 'max-age=0',
146+
'user-agent' => 'PHPUnit',
147+
'upgrade-insecure-requests' => '1',
148+
'accept' => $accept,
149+
'dnt' => '1',
150+
'accept-encoding' => 'gzip, deflate, br',
151+
'accept-language' => 'zh-CN,zh;q=0.8,en;q=0.6,it-IT;q=0.4,it;q=0.2',
152+
];
153+
154+
$swooleRequest->fd = 1;
155+
$swooleRequest->header = ArrayHelper::merge($headers, $defaultHeaders);
156+
$swooleRequest->server = [
157+
'request_method' => $method,
158+
'request_uri' => $uri,
159+
'path_info' => '/',
160+
'request_time' => microtime(),
161+
'request_time_float' => microtime(true),
162+
'server_port' => 80,
163+
'remote_port' => 54235,
164+
'remote_addr' => '10.0.2.2',
165+
'master_time' => microtime(),
166+
'server_protocol' => 'HTTP/1.1',
167+
'server_software' => 'swoole-http-server',
168+
];
169+
170+
if ($method == 'GET') {
171+
$swooleRequest->get = $parameters;
172+
} elseif ($method == 'POST') {
173+
$swooleRequest->post = $parameters;
174+
}
175+
176+
if (! empty($urlParams)) {
177+
$get = empty($swooleRequest->get) ? [] : $swooleRequest->get;
178+
$swooleRequest->get = array_merge($urlParams, $get);
179+
}
180+
}
17181
}

test/Cases/ValidatorTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace SwoftTest\HttpServer;
4+
5+
use Swoft\Helper\JsonHelper;
6+
7+
class ValidatorTest extends AbstractTestCase
8+
{
9+
public function testDemo()
10+
{
11+
$headers = [
12+
'Content-Type' => 'application/json'
13+
];
14+
$raw = JsonHelper::encode([
15+
'test' => [
16+
'id' => 1
17+
]
18+
]);
19+
$res = $this->raw('POST', '/validator/json', [], $headers, $raw)->getBody()->getContents();
20+
$this->assertEquals('[1,"limx"]', $res);
21+
22+
$headers = [
23+
'Content-Type' => 'application/json;charset=UTF-8'
24+
];
25+
$raw = JsonHelper::encode([
26+
'test' => [
27+
'id' => 1
28+
]
29+
]);
30+
$res = $this->raw('POST', '/validator/json', [], $headers, $raw)->getBody()->getContents();
31+
$this->assertEquals('[1,"limx"]', $res);
32+
}
33+
34+
35+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
namespace SwoftTest\Testing\Controllers;
3+
4+
use Swoft\Http\Message\Server\Request;
5+
use Swoft\Http\Server\Bean\Annotation\Controller;
6+
use Swoft\Http\Server\Bean\Annotation\RequestMapping;
7+
use Swoft\Http\Server\Bean\Annotation\RequestMethod;
8+
use Swoft\Bean\Annotation\Number;
9+
use Swoft\Bean\Annotation\Strings;
10+
use Swoft\Http\Message\Server\Response;
11+
12+
/**
13+
* Class ValidatorController
14+
* @Controller(prefix="/validator")
15+
*/
16+
class ValidatorController
17+
{
18+
/**
19+
* @Number(name="test.id", max=10)
20+
* @Strings(name="test.name", default="limx")
21+
* @RequestMapping(route="json", method=RequestMethod::POST)
22+
*/
23+
public function json(Request $request, Response $response)
24+
{
25+
$id = $request->json('test.id');
26+
$name = $request->json('test.name');
27+
28+
return $response->json([$id, $name]);
29+
}
30+
}

test/config/beans/base.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
<?php
22

3-
return [];
3+
use Swoft\Http\Server\Parser\RequestParser;
4+
use Swoft\Http\Server\Router\HandlerMapping;
5+
use Swoft\Http\Server\ServerDispatcher;
6+
7+
return [
8+
'serverDispatcher' => [
9+
'class' => ServerDispatcher::class,
10+
],
11+
'httpRouter' => [
12+
'class' => HandlerMapping::class,
13+
],
14+
'requestParser' => [
15+
'class' => RequestParser::class,
16+
],
17+
];

test/config/properties/app.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"version" => '1.0',
44
'autoInitBean' => true,
55
'beanScan' => [
6-
'Swoft\\Http\\Server\\Test\\Testing' => BASE_PATH."/Testing"
6+
'SwoftTest\\Testing' => BASE_PATH . "/Testing",
7+
'Swoft\\Http\\Server' => BASE_PATH . '/../src',
78
],
89
'I18n' => [
910
'sourceLanguage' => '@root/resources/messages/',

0 commit comments

Comments
 (0)