Skip to content

Commit

Permalink
up: swoft-cloud/swoft/issues/1292 allow use empty string on RequestMa…
Browse files Browse the repository at this point in the history
…pping.route
  • Loading branch information
inhere committed May 15, 2020
1 parent 6f50b7d commit 36cca20
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 34 deletions.
19 changes: 16 additions & 3 deletions src/http-server/src/Annotation/Mapping/RequestMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@
*
* @since 2.0
*/
class RequestMapping
final class RequestMapping
{
// If route path:
// - use "", eg: `@RequestMapping("")`
// - or use "@prefix", eg: `@RequestMapping("@prefix")`
// Will use the route prefix(Controller.prefix) as route.
public const USE_PREFIX = '@prefix';

/**
* Action routing path
*
Expand Down Expand Up @@ -58,10 +64,17 @@ class RequestMapping
*/
public function __construct(array $values)
{
$route = null;
if (isset($values['value'])) {
$this->route = (string)$values['value'];
$route = (string)$values['value'];
} elseif (isset($values['route'])) {
$this->route = (string)$values['route'];
$route = (string)$values['route'];
}

if (isset($route)) {
// If use "", eg: `@RequestMapping("")`
// Will use the route prefix as route.
$this->route = $route === '' ? self::USE_PREFIX : $route;
}

if (isset($values['name'])) {
Expand Down
2 changes: 0 additions & 2 deletions src/http-server/src/Formatter/AcceptResponseFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace Swoft\Http\Server\Formatter;

use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Exception\SwoftException;
use Swoft\Http\Message\ContentType;
use Swoft\Http\Message\Contract\ResponseFormatterInterface;
use Swoft\Http\Message\Response;
Expand Down Expand Up @@ -51,7 +50,6 @@ class AcceptResponseFormatter implements ResponseFormatterInterface
* @param Response $response
*
* @return Response
* @throws SwoftException
*/
public function format(Response $response): Response
{
Expand Down
3 changes: 0 additions & 3 deletions src/http-server/src/HttpDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Concern\AbstractDispatcher;
use Swoft\Context\Context;
use Swoft\Exception\SwoftException;
use Swoft\Http\Message\Request;
use Swoft\Http\Message\Response;
use Swoft\Http\Server\Formatter\AcceptResponseFormatter;
Expand Down Expand Up @@ -60,8 +59,6 @@ class HttpDispatcher extends AbstractDispatcher
* Dispatch http request
*
* @param mixed ...$params
*
* @throws SwoftException
*/
public function dispatch(...$params): void
{
Expand Down
14 changes: 11 additions & 3 deletions src/http-server/src/Router/RouteRegister.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Swoft\Http\Server\Router;

use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Stdlib\Helper\Str;
use function rtrim;

Expand Down Expand Up @@ -81,9 +82,16 @@ public static function registerRoutes(Router $router): void
// Ensure is not empty
$routePath = $route['route'] ?: $route['action'];

// A route starting with '/' is a separate route
// Unused '/' needs to be combined with the controller group into a route
$path = $routePath[0] === '/' ? $routePath : $prefix . '/' . $routePath;
// If use "", eg: `@RequestMapping("")`
// Will use the route prefix(Controller.prefix) as route.
if ($routePath === RequestMapping::USE_PREFIX) {
$path = $prefix;
} else {
// A route starting with '/' is a separate route
// Unused '/' needs to be combined with the controller group into a route
$path = $routePath[0] === '/' ? $routePath : $prefix . '/' . $routePath;
}

$handler = $class . '@' . $route['action'];

$router->map($route['method'], $path, $handler, $route['params'], [
Expand Down
14 changes: 12 additions & 2 deletions src/http-server/test/testing/Controller/RouteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
use function context;

/**
* Class RouteController
Expand All @@ -24,6 +25,16 @@ class RouteController extends BaseController
{
use TraitController;

/**
* @RequestMapping("")
*
* @return string
*/
public function testRoute(): string
{
return 'testRoute';
}

/**
* @RequestMapping("string")
*
Expand Down Expand Up @@ -70,8 +81,7 @@ public function data(): array
*/
public function parser(): array
{
$body = \context()->getRequest()->getParsedBody();
return $body;
return context()->getRequest()->getParsedBody();
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/http-server/test/testing/Controller/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
*/
class TestController
{
/**
* @RequestMapping("@prefix")
* @return string
*/
public function home(): string
{
return 'home';
}

/**
* @RequestMapping()
* @return string
Expand Down
3 changes: 0 additions & 3 deletions src/http-server/test/testing/MockHttpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use RuntimeException;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\BeanFactory;
use Swoft\Exception\SwoftException;
use Swoft\Http\Message\Request as ServerRequest;
use Swoft\Http\Message\Response as ServerResponse;
use Swoft\Http\Server\HttpDispatcher;
Expand All @@ -37,7 +36,6 @@ class MockHttpServer
* @param array $ext
*
* @return MockResponse
* @throws SwoftException
*/
public function request(
string $method,
Expand Down Expand Up @@ -96,7 +94,6 @@ public function mockRequest(
* @param Response $response
*
* @return ServerResponse
* @throws SwoftException
*/
public function onRequest(Request $request, Response $response): ServerResponse
{
Expand Down
15 changes: 8 additions & 7 deletions src/http-server/test/unit/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
namespace SwoftTest\Http\Server\Unit;

use Swoft\Exception\SwoftException;
use Swoft\Http\Message\ContentType;
use SwoftTest\Http\Server\Testing\Controller\TestController;
use SwoftTest\Http\Server\Testing\MockRequest;
Expand All @@ -21,9 +20,14 @@
*/
class ResponseTest extends HttpServerTestCase
{
/**
* @throws SwoftException
*/
public function testRoute(): void
{
/** @see TestController */
$response = $this->mockServer->request(MockRequest::GET, '/fixture/test');

$response->assertEqualJson(['data' => 'home']);
}

public function testCookie(): void
{
/** @see TestController */
Expand All @@ -34,9 +38,6 @@ public function testCookie(): void
$this->assertSame('ck=val', $cks['ck']);
}

/**
* @throws SwoftException
*/
public function testHtml(): void
{
/** @see TestController */
Expand Down
17 changes: 6 additions & 11 deletions src/http-server/test/unit/RestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
*/
class RestTest extends HttpServerTestCase
{
/**
* @throws \Swoft\Exception\SwoftException
*/
public function testList()
public function testList(): void
{
$headers = [
ContentType::KEY => ContentType::JSON
Expand All @@ -33,9 +30,7 @@ public function testList()
$response->assertEqualJson(['list']);
}

/**
*/
public function testCreate()
public function testCreate(): void
{
$headers = [
ContentType::KEY => ContentType::JSON
Expand All @@ -53,7 +48,7 @@ public function testCreate()
$response->assertEqualJson($data);
}

public function testGetUser()
public function testGetUser(): void
{
$headers = [
ContentType::KEY => ContentType::JSON
Expand All @@ -72,7 +67,7 @@ public function testGetUser()
$response->assertEqualJson(['getUser', 0]);
}

public function testGetBookFromUser()
public function testGetBookFromUser(): void
{
$headers = [
ContentType::KEY => ContentType::JSON
Expand All @@ -88,7 +83,7 @@ public function testGetBookFromUser()
$response->assertEqualJson($data);
}

public function testDelete()
public function testDelete(): void
{
$headers = [
ContentType::KEY => ContentType::JSON
Expand All @@ -103,7 +98,7 @@ public function testDelete()
$response->assertEqualJson($data);
}

public function testUpdate()
public function testUpdate(): void
{
$headers = [
ContentType::KEY => ContentType::JSON
Expand Down
3 changes: 3 additions & 0 deletions src/http-server/test/unit/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public function testReturnType(): void

$response = $this->mockServer->request(MockRequest::GET, '/testRoute/null');
$response->assertEqualContent('{}');

$response = $this->mockServer->request(MockRequest::GET, '/testRoute');
$response->assertEqualJson(['data' => 'testRoute']);
}

/**
Expand Down

0 comments on commit 36cca20

Please sign in to comment.