Skip to content

Commit

Permalink
Merge pull request #1361 from ulue/dev2
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 14, 2020
2 parents c8f3bf1 + 85df5e2 commit c345c29
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 21 deletions.
21 changes: 12 additions & 9 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,24 @@ return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules([
'@PSR2' => true,
'header_comment' => [
'comment_type' => 'PHPDoc',
'header' => $header,
'separate' => 'bottom'
],
'array_syntax' => [
'syntax' => 'short'
],
'encoding' => true, // MUST use only UTF-8 without BOM
'single_quote' => true,
'class_attributes_separation' => true,
'declare_strict_types' => true,
'encoding' => true, // MUST use only UTF-8 without BOM
'global_namespace_import' => [
'import_constants' => true,
'import_functions' => true,
],
'header_comment' => [
'comment_type' => 'PHPDoc',
'header' => $header,
'separate' => 'bottom'
],
'no_unused_imports' => true,
'global_namespace_import' => true,
'single_quote' => true,
'standardize_not_equals' => true,
'declare_strict_types' => true,
])
->setFinder(
PhpCsFixer\Finder::create()
Expand Down
67 changes: 67 additions & 0 deletions app/Http/Middleware/DomainLimitMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php declare(strict_types=1);
/**
* This file is part of Swoft.
*
* @link https://swoft.org
* @document https://swoft.org/docs
* @contact group@swoft.org
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/

namespace App\Http\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Http\Message\Request;
use Swoft\Http\Server\Contract\MiddlewareInterface;
use function context;
use function strpos;

/**
* Class DomainLimitMiddleware
*
* @Bean()
*/
class DomainLimitMiddleware implements MiddlewareInterface
{
private $domain2paths = [
'user.com' => [
// match all /user/*
'/user/',
],
'blog.com' => [
// match all /blog/*
'/blog/',
]
];

/**
* Process an incoming server request.
*
* @param ServerRequestInterface|Request $request
* @param RequestHandlerInterface $handler
*
* @return ResponseInterface
* @inheritdoc
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$uriPath = $request->getUriPath();
$domain = $request->getUri()->getHost();

if (!isset($this->domain2paths[$domain])) {
return context()->getResponse()->withStatus(404)->withContent('invalid request domain');
}

foreach ($this->domain2paths[$domain] as $prefix) {
// not match route prefix
if (strpos($uriPath, $prefix) !== 0) {
return context()->getResponse()->withStatus(404)->withContent('page not found');
}
}

return $handler->handle($request);
}
}
5 changes: 3 additions & 2 deletions app/Http/Middleware/FavIconMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Exception\SwoftException;
use Swoft\Http\Message\Request;
use Swoft\Http\Server\Contract\MiddlewareInterface;
use function context;
Expand All @@ -34,10 +33,12 @@ class FavIconMiddleware implements MiddlewareInterface
*
* @return ResponseInterface
* @inheritdoc
* @throws SwoftException
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$uriPath = $request->getUriPath();
$domain = $request->getUri()->getHost();

if ($request->getUriPath() === '/favicon.ico') {
return context()->getResponse()->withStatus(404);
}
Expand Down
26 changes: 16 additions & 10 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
# Tests

## Http tests

**If you want to run http client tests, you must start the http server.**

### Run http tests

Can only be run inside phpstorm.

![http-client-tests](testdata/http-client-tests.png)

## API tests

**If you want to run api tests, you must start the http server.**

tests case dir: `/test/api/`

## Start server

```bash
Expand All @@ -30,8 +22,22 @@ php test/run.php -c phpunit.xml --testsuite apiTests

## Unit tests

tests case dir: `/test/unit/`

### Run unit tests

```bash
php test/run.php -c phpunit.xml --testsuite unitTests
```

## Http tests

tests case dir: `/test/httptest/`

**If you want to run http client tests, you must start the http server.**

### Run http tests

Can only be run inside phpstorm.

![http-client-tests](testdata/http-client-tests.png)
4 changes: 4 additions & 0 deletions test/unit/Common/MyBeanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use App\Common\MyBean;
use PHPUnit\Framework\TestCase;
use Swoft\Log\Helper\Log;
use function bean;

/**
Expand All @@ -25,6 +26,9 @@ public function testMyMethod2(): void
{
$bean = bean(MyBean::class);

vdump('test message');
Log::info('test message');

$this->assertSame(MyBean::class . '::myMethod2', $bean->myMethod2());
}
}

0 comments on commit c345c29

Please sign in to comment.