Skip to content

Commit f20ea4c

Browse files
Merge pull request #20 from integer-net/unauthorized-response
Return correct HTTP status code for unauthorized requests
2 parents 2ed7b7c + d296953 commit f20ea4c

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace RunAsRoot\PrometheusExporter\Test\Integration\Controller;
5+
6+
use Magento\TestFramework\TestCase\AbstractController;
7+
use Magento\Framework\App\Request;
8+
use Magento\Framework\App\Response;
9+
10+
/**
11+
* @method Request\Http getRequest()
12+
* @method Response\Http getResponse()
13+
*/
14+
class IndexControllerTest extends AbstractController
15+
{
16+
/**
17+
* @magentoAppArea frontend
18+
*/
19+
public function testUnauthorizedResponse()
20+
{
21+
$this->dispatch('metrics/index/index');
22+
$this->assertEquals(401, $this->getResponse()->getStatusCode(), 'Status code should be 401 Unauthorized');
23+
$this->assertEquals(
24+
'You are not allowed to see these metrics.',
25+
$this->getResponse()->getBody(),
26+
'Body should be error message'
27+
);
28+
}
29+
30+
/**
31+
* @magentoAppArea frontend
32+
* @magentoConfigFixture current_store metric_configuration/security/token supersecrettokenxxx
33+
*/
34+
public function testAuthorizedResponse()
35+
{
36+
$this->getRequest()->getHeaders()->addHeaderLine('Authorization: Bearer supersecrettokenxxx');
37+
$this->dispatch('metrics/index/index');
38+
$this->assertEquals(200, $this->getResponse()->getStatusCode(), 'Status code should be 200 OK');
39+
}
40+
}

src/Controller/Index/Index.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
namespace RunAsRoot\PrometheusExporter\Controller\Index;
66

7+
use Laminas\Http\Response;
78
use Magento\Framework\App\Action\Action;
89
use Magento\Framework\App\Action\Context;
10+
use Magento\Framework\App\Response\Http;
911
use Magento\Framework\Controller\ResultFactory;
1012
use Magento\Framework\Controller\ResultInterface;
1113
use RunAsRoot\PrometheusExporter\Data\Config;
@@ -33,11 +35,12 @@ public function execute(): ResultInterface
3335
$authorizationHeader = $this->getRequest()->getHeader('Authorization');
3436

3537
if ($token !== $authorizationHeader) {
36-
/** @var \Magento\Framework\Controller\Result\Raw $response */
37-
$response = $this->resultFactory->create(ResultFactory::TYPE_RAW);
38-
$response->setContents('You are not allowed to see these metrics.');
38+
/** @var \Magento\Framework\Controller\Result\Raw $result */
39+
$result = $this->resultFactory->create(ResultFactory::TYPE_RAW);
40+
$result->setHttpResponseCode(Http::STATUS_CODE_401);
41+
$result->setContents('You are not allowed to see these metrics.');
3942

40-
return $response;
43+
return $result;
4144
}
4245

4346
return $this->prometheusResultFactory->create();

0 commit comments

Comments
 (0)