Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
99 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Contributte\OAuth2Server\Http; | ||
|
||
use Nette\Application\IResponse as AppResponse; | ||
use Nette\Http\IRequest; | ||
use Nette\Http\IResponse; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class Oauth2Response implements AppResponse | ||
{ | ||
|
||
/** @var ResponseInterface */ | ||
private $psr7; | ||
|
||
public function __construct(ResponseInterface $psr7) | ||
{ | ||
$this->psr7 = $psr7; | ||
} | ||
|
||
public function send(IRequest $httpRequest, IResponse $httpResponse): void | ||
{ | ||
$httpResponse->setCode($this->psr7->getStatusCode()); | ||
|
||
foreach ($this->psr7->getHeaders() as $name => $values) { | ||
foreach ($values as $value) { | ||
$httpResponse->addHeader($name, $value); | ||
} | ||
} | ||
|
||
$stream = $this->psr7->getBody(); | ||
$stream->rewind(); | ||
|
||
while (!$stream->eof()) { | ||
echo $stream->read(8192); | ||
} | ||
} | ||
|
||
} |
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,45 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Tests\Cases\E2E; | ||
|
||
use Contributte\OAuth2Server\Http\Oauth2Response; | ||
use Contributte\Psr7\Psr7Response; | ||
use Nette\Application\IPresenter; | ||
use Nette\Application\IResponse; | ||
use Nette\Application\Request; | ||
use Nette\Http\RequestFactory; | ||
use Nette\Http\Response; | ||
use Ninjify\Nunjuck\Toolkit; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Tester\Assert; | ||
|
||
require_once __DIR__ . '/../../bootstrap.php'; | ||
|
||
Toolkit::test(function (): void { | ||
$presenter = new class implements IPresenter { | ||
|
||
/** @var ResponseInterface */ | ||
public $psr7; | ||
|
||
public function run(Request $request): IResponse | ||
{ | ||
return new Oauth2Response($this->psr7); | ||
} | ||
|
||
}; | ||
|
||
$psr7 = Psr7Response::fromGlobals() | ||
->withStatus(200); | ||
$psr7->getBody()->write('test'); | ||
|
||
$presenter->psr7 = $psr7; | ||
$response = $presenter->run(new Request('test')); | ||
|
||
ob_start(); | ||
$response->send((new RequestFactory())->fromGlobals(), new Response()); | ||
$result = ob_get_contents(); | ||
ob_end_clean(); | ||
|
||
Assert::equal(200, http_response_code()); | ||
Assert::equal('test', $result); | ||
}); |