Skip to content

Commit

Permalink
Add getHeaderLines
Browse files Browse the repository at this point in the history
  • Loading branch information
stelin committed Jul 15, 2019
1 parent 2bbf586 commit 74a2a91
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/http-message/src/Concern/MessageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,21 @@ public function getHeaderLine($name): string
return implode(', ', $this->getHeader($name));
}

/**
* Get all headerLines
*
* @return array
*/
public function getHeaderLines(): array
{
$headers = [];
foreach ($this->getHeaders() as $name => $header) {
$headers[$name] = implode(', ', $header);
}

return $headers;
}

/**
* Return an instance with the provided value replacing the specified header.
*
Expand Down Expand Up @@ -214,13 +229,13 @@ public function withHeader($name, $value)
}

/**
* @see MessageInterface::withAddedHeader()
*
* @param string $name Case-insensitive header field name to add.
* @param string|string[] $value Header value(s).
*
* @return static
* @throws InvalidArgumentException for invalid header names or values.
* @see MessageInterface::withAddedHeader()
*
*/
public function withAddedHeader($name, $value)
{
Expand Down
30 changes: 30 additions & 0 deletions src/http-server/test/testing/Controller/HeaderController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);


namespace SwoftTest\Http\Server\Testing\Controller;

use Swoft\Http\Message\Request;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;

/**
* Class HeaderController
*
* @since 2.0
*
* @Controller("testHeader")
*/
class HeaderController
{
/**
* @RequestMapping()
*
* @param Request $request
*
* @return array
*/
public function headerLines(Request $request): array
{
return $request->getHeaderLines();
}
}
34 changes: 34 additions & 0 deletions src/http-server/test/unit/HeaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types=1);


namespace SwoftTest\Http\Server\Unit;

use ReflectionException;
use Swoft\Bean\Exception\ContainerException;

/**
* Class HeaderTest
*
* @since 2.0
*/
class HeaderTest extends TestCase
{
/**
* @throws ReflectionException
* @throws ContainerException
*/
public function testHeaderLines(): void
{
$headers = [
'user-agent' => 'curl/7.29.0',
'host' => '127.0.0.1:18306',
'content-type' => 'application/json',
'accept' => '*/*',
'id' => 1024,
'id2' => 2048
];

$response = $this->mockServer->request('POST', '/testHeader/headerLines', [], $headers);
$response->assertEqualJson($headers);
}
}
1 change: 1 addition & 0 deletions src/process/src/ProcessDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ProcessDispatcher
*/
public function dispatcher(Pool $pool, int $workerId): void
{
$workerId = $workerId + 1;

try {
$process = $this->getProcess($workerId);
Expand Down
1 change: 0 additions & 1 deletion src/process/src/ProcessRegister.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public static function registerProcess(string $className, int $workerId): void
*/
public static function getProcess(int $workerId): string
{
$workerId = $workerId + 1;
$className = self::$process[$workerId]['class'] ?? '';
if (!empty($className)) {
return $className;
Expand Down

0 comments on commit 74a2a91

Please sign in to comment.