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

Make ExecuteJavascriptMiddleware handle requests #264

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
22 changes: 17 additions & 5 deletions src/Downloader/Middleware/ExecuteJavascriptMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@

namespace RoachPHP\Downloader\Middleware;

use GuzzleHttp\Psr7\Response as Psr7Response;
use Psr\Log\LoggerInterface;
use RoachPHP\Http\Request;
use RoachPHP\Http\Response;
use RoachPHP\Support\Configurable;
use Spatie\Browsershot\Browsershot;

final class ExecuteJavascriptMiddleware implements ResponseMiddlewareInterface
final class ExecuteJavascriptMiddleware implements RequestMiddlewareInterface
{
use Configurable;

Expand All @@ -37,10 +39,10 @@ public function __construct(
$this->getBrowsershot = $getBrowsershot ?? static fn (string $uri): Browsershot => Browsershot::url($uri)->waitUntilNetworkIdle();
}

public function handleResponse(Response $response): Response
public function handleRequest(Request $request): Request
{
$browsershot = $this->configureBrowsershot(
$response->getRequest()->getUri(),
$request->getUri(),
);

try {
Expand All @@ -51,10 +53,20 @@ public function handleResponse(Response $response): Response
'trace' => $e->getTraceAsString(),
]);

return $response->drop('Error while executing javascript');
return $request->drop('Error while executing javascript');
}

return $response->withBody($body);
return $request->withResponse(
$this->makeResponse($request, $body),
);
}

private function makeResponse(Request $request, string $body): Response
{
return new Response(
new Psr7Response(200, [], $body),
$request,
);
}

/**
Expand Down
24 changes: 9 additions & 15 deletions tests/Downloader/Middleware/ExecuteJavascriptMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ final class ExecuteJavascriptMiddlewareTest extends IntegrationTestCase

public function testUpdateResponseBodyWithHtmlAfterExecutingJavascript(): void
{
$response = $this->makeResponse(
$this->makeRequest('http://localhost:8000/javascript'),
);
$request = $this->makeRequest('http://localhost:8000/javascript');
$middleware = new ExecuteJavascriptMiddleware(new FakeLogger());

$processedResponse = $middleware->handleResponse($response);
$processedRequest = $middleware->handleRequest($request);
$processedResponse = $processedRequest->getResponse();

self::assertSame('Headline', $processedResponse->filter('#content h1')->text(''));
self::assertSame('I was loaded via Javascript!', $processedResponse->filter('#content p')->text(''));
Expand All @@ -52,9 +51,9 @@ public function bodyHtml(): string
static fn (string $uri): Browsershot => $throwingBrowsershot->setUrl($uri),
);

$processedResponse = $middleware->handleResponse($this->makeResponse());
$processedRequest = $middleware->handleRequest($this->makeRequest('http://fake-url.com'));

self::assertTrue($processedResponse->wasDropped());
self::assertTrue($processedRequest->wasDropped());
}

public function testLogErrors(): void
Expand All @@ -71,7 +70,7 @@ public function bodyHtml(): string
static fn (string $uri): Browsershot => $throwingBrowsershot->setUrl($uri),
);

$middleware->handleResponse($this->makeResponse());
$middleware->handleRequest($this->makeRequest('http://fake-url.com'));

self::assertTrue(
$logger->messageWasLogged(
Expand All @@ -84,19 +83,14 @@ public function bodyHtml(): string
public function testUsesTheProvidedUserAgentOption(): void
{
$mockBrowserShot = $this->createMock(Browsershot::class);
$response = $this->makeResponse(
$this->makeRequest('http://localhost:8000/javascript'),
);
$middleware = new ExecuteJavascriptMiddleware(
new FakeLogger(),
static fn (string $uri): Browsershot => $mockBrowserShot,
);
$request = $this->makeRequest('http://localhost:8000/javascript');
$middleware = new ExecuteJavascriptMiddleware(new FakeLogger(), static fn (string $uri): Browsershot => $mockBrowserShot);
$middleware->configure(['userAgent' => 'custom']);

$mockBrowserShot->expects(self::once())
->method('userAgent')
->with(self::equalTo('custom'));

$middleware->handleResponse($response);
$middleware->handleRequest($request);
}
}
Loading