Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Browser middleware #492

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace React\Http;

use Psr\Http\Message\ResponseInterface;
use React\Http\Browser\MiddlewareInterface;
use React\Http\Browser\MiddlewareRunner;
use RingCentral\Psr7\Uri;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
Expand All @@ -27,6 +29,9 @@ class Browser
'User-Agent' => 'ReactPHP/1'
);

/** @var MiddlewareInterface[] */
private $middlewares = array();

/**
* The `Browser` is responsible for sending HTTP requests to your HTTP server
* and keeps track of pending incoming HTTP responses.
Expand Down Expand Up @@ -851,8 +856,21 @@ private function requestMayBeStreaming($method, $url, array $headers = array(),
}
}

return $this->transaction->send(
new Request($method, $url, $headers, $body, $this->protocolVersion)
);
$request = new Request($method, $url, $headers, $body, $this->protocolVersion);

$middlewareRunner = new MiddlewareRunner($this->middlewares, $this->transaction);
return $middlewareRunner($request);
}

/**
* @param MiddlewareInterface $middleware
* @return Browser
*/
public function withMiddleware(MiddlewareInterface $middleware)
{
$browser = clone $this;
$browser->middlewares[] = $middleware;

return $browser;
}
}
17 changes: 17 additions & 0 deletions src/Browser/MiddlewareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace React\Http\Browser;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use React\Promise\PromiseInterface;

interface MiddlewareInterface
{
/**
* @param RequestInterface $request
* @param callable $next
* @return PromiseInterface<ResponseInterface>|ResponseInterface
*/
public function __invoke(RequestInterface $request, callable $next);
}
67 changes: 67 additions & 0 deletions src/Browser/MiddlewareRunner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace React\Http\Browser;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use React\Http\Io\Transaction;
use React\Promise\PromiseInterface;

/**
* [Internal] Middleware runner to expose an array of middleware request handlers as a single request handler callable
*
* @internal
*/
final class MiddlewareRunner
{
/** @var MiddlewareInterface[] */
private $middleware;

/** @var Transaction */
private $transaction;

/**
* @param MiddlewareInterface[] $middleware
*/
public function __construct(array $middleware, Transaction $transaction)
{
$this->middleware = \array_values($middleware);
$this->transaction = $transaction;
}

/**
* @param RequestInterface $request
* @return ResponseInterface|PromiseInterface<ResponseInterface>
* @throws \Exception
*/
public function __invoke(RequestInterface $request)
{
return $this->call($request, 0);
}

/** @internal */
public function call(RequestInterface $request, $position)
{
if (count($this->middleware) === 0) {
return $this->transaction->send($request);
}

// final request handler will be invoked with transaction send callable
if (!isset($this->middleware[$position + 1])) {
$handler = $this->middleware[$position];
$that = $this;
return $handler($request, function () use ($that, $request) {
return $that->transaction->send($request);
});
}

$that = $this;
$next = function (RequestInterface $request) use ($that, $position) {
return $that->call($request, $position + 1);
};

// invoke middleware request handler with next handler
$handler = $this->middleware[$position];
return $handler($request, $next);
}
}
52 changes: 52 additions & 0 deletions tests/Browser/MiddlewareRunnerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace React\Tests\Http\Browser;

use PHPUnit\Framework\MockObject\MockObject;
use React\Http\Browser\MiddlewareRunner;
use React\Http\Io\Transaction;
use React\Http\Message\Request;
use React\Http\Message\ServerRequest;
use React\Tests\Http\BrowserMiddlewareStub;
use React\Tests\Http\TestCase;
use RingCentral\Psr7\Response;

final class MiddlewareRunnerTest extends TestCase
{
public function testEmptyMiddlewareStack()
{
$request = new Request('GET', 'https://example.com/');

/** @var Transaction&MockObject $transaction */
$transaction = $this->getMockBuilder('\React\Http\Io\Transaction')
->disableOriginalConstructor()
->getMock();
$response = new Response();
$transaction->expects(static::once())->method('send')->with($request)->willReturn($response);

$middlewares = array();
$middlewareStack = new MiddlewareRunner($middlewares, $transaction);

$actualResponse = $middlewareStack($request);
$this->assertSame($response, $actualResponse);
}

public function testWithMiddleware()
{
/** @var Transaction&MockObject $transaction */
$transaction = $this->getMockBuilder('\React\Http\Io\Transaction')
->disableOriginalConstructor()
->getMock();
$transaction->expects(static::never())->method('send');

$response = new Response();
$middlewareStack = new MiddlewareRunner(array(
new BrowserMiddlewareStub($response)
), $transaction);

$request = new ServerRequest('GET', 'http://example.com/');

$actualResponse = $middlewareStack($request);
$this->assertSame($response, $actualResponse);
}
}
29 changes: 29 additions & 0 deletions tests/BrowserMiddlewareStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace React\Tests\Http;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use React\Http\Browser\MiddlewareInterface;
use React\Promise\PromiseInterface;

final class BrowserMiddlewareStub implements MiddlewareInterface
{
/** @var ResponseInterface */
private $response;

public function __construct(ResponseInterface $response)
{
$this->response = $response;
}

/**
* @param RequestInterface $request
* @param callable $next
* @return PromiseInterface<ResponseInterface>|ResponseInterface
*/
public function __invoke(RequestInterface $request, callable $next)
{
return $this->response;
}
}