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

Skip AssetHttpMiddleware processing for JSON:API requests, never redirect POST requests #37

Merged
merged 6 commits into from
Apr 8, 2022
Merged
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
4 changes: 3 additions & 1 deletion src/EventSubscriber/RedirectResponseSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ private function buildRedirectUrl(string $url) : string {
public function onResponse(ResponseEvent $event) : void {
if (
!$this->validProxyDomains ||
!$this->proxyManager->isConfigured(ProxyManagerInterface::DEFAULT_PROXY_DOMAIN)
!$this->proxyManager->isConfigured(ProxyManagerInterface::DEFAULT_PROXY_DOMAIN) ||
!$event->getRequest()->isMethod('GET')
) {
// Nothing to do if default proxy domain is not defined.
// Only redirect on GET requests as well.
return;
}
$response = $event->getResponse();
Expand Down
8 changes: 7 additions & 1 deletion src/HttpMiddleware/AssetHttpMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ private function isJsonResponse(Response $response) : bool {
return TRUE;
}

return $response->headers->get('content-type') === 'application/json';
$jsonTypes = [
'application/json',
'application/vnd.api+json',
];
return in_array($response->headers->get('content-type'), $jsonTypes);
}

/**
Expand Down Expand Up @@ -112,10 +116,12 @@ public function handle(
if ($this->isXmlResponse($response)) {
return $response;
}

// Nothing to do if asset path is not configured.
if (!$this->proxyManager->isConfigured(ProxyManagerInterface::ASSET_PATH)) {
return $response;
}

$content = $response->getContent();

if (!is_string($content)) {
Expand Down
38 changes: 37 additions & 1 deletion tests/src/Unit/RedirectResponseSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,35 @@ public function testNoProxyDomain() : void {
$sut->onResponse($event->reveal());
}

/**
* Tests that response stays intact for non-get requests.
*
* @covers ::onResponse
* @covers ::__construct
*/
public function testPostRequestResponse() : void {
$proxyManagerMock = $this->prophesize(ProxyManagerInterface::class);
$proxyManagerMock->isConfigured(ProxyManagerInterface::DEFAULT_PROXY_DOMAIN)
->shouldBeCalled()
->willReturn(TRUE);
$request = $this->prophesize(Request::class);
$request->isMethod('GET')
->willReturn(FALSE);

$response = $this->prophesize(RedirectResponse::class);
$response->getTargetUrl()
->shouldNotBeCalled();
$event = new ResponseEvent(
$this->createMock(HttpKernelInterface::class),
$request->reveal(),
HttpKernelInterface::MASTER_REQUEST,
$response->reveal()
);
// Make sure response stays intact when the request method is not GET.
$sut = new RedirectResponseSubscriber($proxyManagerMock->reveal(), ['www.hel.fi']);
$sut->onResponse($event);
}

/**
* Tests onResponse() with RedirectResponse.
*
Expand All @@ -83,9 +112,13 @@ public function testRedirectResponse() : void {
$response->getTargetUrl()
->willReturn('http://localhost:8888/test?x=1');

$request = $this->prophesize(Request::class);
$request->isMethod('GET')
->willReturn(TRUE);

$event = new ResponseEvent(
$this->createMock(HttpKernelInterface::class),
$this->createMock(Request::class),
$request->reveal(),
HttpKernelInterface::MASTER_REQUEST,
$response->reveal()
);
Expand Down Expand Up @@ -118,6 +151,9 @@ public function testResponseRedirect() : void {
$request->getSchemeAndHttpHost()
->shouldBeCalled()
->willReturn('http://localhost:8888');
$request->isMethod('GET')
->shouldBeCalled()
->willReturn(TRUE);
$request->getRequestUri()
->shouldBeCalled()
->willReturn('/test?x=1');
Expand Down