Skip to content

Commit

Permalink
Merge pull request #35 from City-of-Helsinki/UHF-4799_sitemap_xml_res…
Browse files Browse the repository at this point in the history
…ponse_fix

UHF-4799 sitemap xml response fix
  • Loading branch information
rpnykanen authored Mar 16, 2022
2 parents 68189cc + 8fddc10 commit 8033d63
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/HttpMiddleware/AssetHttpMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Drupal\helfi_proxy\HttpMiddleware;

use Drupal\helfi_proxy\ProxyManager;
use Drupal\helfi_proxy\ProxyManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -23,12 +22,12 @@ final class AssetHttpMiddleware implements HttpKernelInterface {
*
* @param \Symfony\Component\HttpKernel\HttpKernelInterface $httpKernel
* The http kernel.
* @param \Drupal\helfi_proxy\ProxyManager $proxyManager
* @param \Drupal\helfi_proxy\ProxyManagerInterface $proxyManager
* The proxy manager.
*/
public function __construct(
private HttpKernelInterface $httpKernel,
private ProxyManager $proxyManager
private ProxyManagerInterface $proxyManager
) {
}

Expand Down Expand Up @@ -81,6 +80,25 @@ private function isJsonResponse(Response $response) : bool {
return $response->headers->get('content-type') === 'application/json';
}

/**
* Checks for xml type mainly for sitemap.
*
* @param \Symfony\Component\HttpFoundation\Response $response
* The response.
*
* @return bool
* TRUE if response is XML
*/
private function isXmlResponse(Response $response) : bool {
if (!$response->headers->has('content-type')) {
return FALSE;
}
return str_starts_with(
$response->headers->get('content-type') ?: '',
'application/xml',
);
}

/**
* {@inheritdoc}
*/
Expand All @@ -91,6 +109,9 @@ public function handle(
) : Response {
$response = $this->httpKernel->handle($request, $type, $catch);

if ($this->isXmlResponse($response)) {
return $response;
}
// Nothing to do if asset path is not configured.
if (!$this->proxyManager->isConfigured(ProxyManagerInterface::ASSET_PATH)) {
return $response;
Expand All @@ -107,6 +128,7 @@ public function handle(
}
return $response;
}

$content = $this->proxyManager
->processHtml($content, $request);

Expand Down
102 changes: 102 additions & 0 deletions tests/src/Unit/AssetHttpMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

declare(strict_types = 1);

namespace Drupal\Tests\helfi_proxy\Unit;

use Drupal\helfi_proxy\HttpMiddleware\AssetHttpMiddleware;
use Drupal\helfi_proxy\ProxyManagerInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
* Tests asset middleware.
*
* @coversDefaultClass \Drupal\helfi_proxy\HttpMiddleware\AssetHttpMiddleware
* @group helfi_proxy
*/
class AssetHttpMiddlewareTest extends UnitTestCase {

/**
* Tests that XML response stays intact.
*
* @covers ::handle
* @covers ::isXmlResponse
* @dataProvider xmlResponseProvider
*/
public function testXmlResponse(?string $contentType) : void {
$headers = $this->prophesize(ParameterBag::class);
$headers->has('content-type')
->shouldBeCalled()
->willReturn(TRUE);

$headers->set('content-type', $contentType);
$headers->get('content-type')
->shouldBeCalled()
->willReturn($contentType);

$responseMock = new Response();
$responseMock->setContent('123');
$responseMock->headers = $headers->reveal();

$mockHttpKernel = $this->createMock(HttpKernelInterface::class);
$mockHttpKernel->method('handle')
->willReturn($responseMock);

$proxyManagerMock = $this->prophesize(ProxyManagerInterface::class);
$proxyManagerMock->isConfigured(ProxyManagerInterface::ASSET_PATH)
->shouldNotBeCalled();

$requestMock = $this->createMock(Request::class);
$sut = new AssetHttpMiddleware($mockHttpKernel, $proxyManagerMock->reveal());
$this->assertEquals(
$sut->handle($requestMock)->headers->get('content-type'),
$contentType
);
}

/**
* The data provider for xml response test.
*
* @return \string[][]
* The data.
*/
public function xmlResponseProvider() : array {
return [
['application/xml'],
['application/xml; charset=utf-8'],
];
}

/**
* Tests that response is intact when no asset path is configured.
*
* @covers ::handle
*/
public function testNoAssetPathConfigured() : void {
$headers = $this->prophesize(ParameterBag::class);
$headers->has('content-type')
->shouldBeCalled()
->willReturn(FALSE);
$responseMock = new Response();
$responseMock->setContent('123');
$responseMock->headers = $headers->reveal();

$mockHttpKernel = $this->createMock(HttpKernelInterface::class);
$mockHttpKernel->method('handle')
->willReturn($responseMock);

$proxyManagerMock = $this->prophesize(ProxyManagerInterface::class);
$proxyManagerMock->isConfigured(ProxyManagerInterface::ASSET_PATH)
->shouldBeCalled()
->willReturn(FALSE);

$sut = new AssetHttpMiddleware($mockHttpKernel, $proxyManagerMock->reveal());
$requestMock = $this->createMock(Request::class);
$sut->handle($requestMock);
}

}

0 comments on commit 8033d63

Please sign in to comment.