-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test PageViewProxy, adding web server
Fix empty header in PageViewProxy.
- Loading branch information
Kajetan Dvoracek
committed
Mar 24, 2022
1 parent
bc018a6
commit 2f67253
Showing
8 changed files
with
258 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
echo $_SERVER['HTTP_USER_AGENT']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is some plain text test file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Kitodo\Dlf\Tests\Functional\Api; | ||
|
||
use Kitodo\Dlf\Tests\Functional\FunctionalTestCase; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
class PageViewProxyDisabledTest extends FunctionalTestCase | ||
{ | ||
protected $disableJsonWrappedResponse = true; | ||
|
||
protected function queryProxy(array $query, string $method = 'GET') | ||
{ | ||
$query['eID'] = 'tx_dlf_pageview_proxy'; | ||
|
||
return $this->httpClient->request($method, '', [ | ||
'query' => $query, | ||
]); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function cannotAccessPageWhenProxyIsDisabled(): void | ||
{ | ||
$targetUrl = 'http://web:8001/Tests/Fixtures/PageViewProxy/test.txt'; | ||
$uHash = GeneralUtility::hmac($targetUrl, 'PageViewProxy'); | ||
|
||
$response = $this->queryProxy([ | ||
'url' => $targetUrl, | ||
'uHash' => $uHash, | ||
]); | ||
|
||
$this->assertEquals(404, $response->getStatusCode()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
namespace Kitodo\Dlf\Tests\Functional\Api; | ||
|
||
use GuzzleHttp\Client as HttpClient; | ||
use Kitodo\Dlf\Tests\Functional\FunctionalTestCase; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
class PageViewProxyTest extends FunctionalTestCase | ||
{ | ||
protected $disableJsonWrappedResponse = true; | ||
|
||
protected function getDlfConfiguration() | ||
{ | ||
return array_merge(parent::getDlfConfiguration(), [ | ||
'enableInternalProxy' => true, | ||
]); | ||
} | ||
|
||
protected function queryProxy(array $query, string $method = 'GET') | ||
{ | ||
$query['eID'] = 'tx_dlf_pageview_proxy'; | ||
|
||
return $this->httpClient->request($method, '', [ | ||
'query' => $query, | ||
]); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function cannotAccessFileUrl(): void | ||
{ | ||
$response = $this->queryProxy([ | ||
'url' => 'file:///etc/passwd', | ||
]); | ||
|
||
$this->assertEquals(400, $response->getStatusCode()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function cannotAccessUrlWithoutUrlHash(): void | ||
{ | ||
$response = $this->queryProxy([ | ||
'url' => 'http://web:8001/Tests/Fixtures/PageViewProxy/test.txt', | ||
]); | ||
|
||
$this->assertEquals(401, $response->getStatusCode()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function cannotAccessUrlWithInvalidUrlHash(): void | ||
{ | ||
$response = $this->queryProxy([ | ||
'url' => 'http://web:8001/Tests/Fixtures/PageViewProxy/test.txt', | ||
'uHash' => 'nottherealhash', | ||
]); | ||
|
||
$this->assertEquals(401, $response->getStatusCode()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function canAccessPageWithUrlHash(): void | ||
{ | ||
$targetUrl = 'http://web:8001/Tests/Fixtures/PageViewProxy/test.txt'; | ||
$uHash = GeneralUtility::hmac($targetUrl, 'PageViewProxy'); | ||
|
||
$response = $this->queryProxy([ | ||
'url' => $targetUrl, | ||
'uHash' => $uHash, | ||
]); | ||
|
||
$this->assertEquals(200, $response->getStatusCode()); | ||
$this->assertEquals('This is some plain text test file.' . "\n", (string) $response->getBody()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function cannotSendPostRequest(): void | ||
{ | ||
$targetUrl = 'http://web:8001/Tests/Fixtures/PageViewProxy/test.txt'; | ||
$uHash = GeneralUtility::hmac($targetUrl, 'PageViewProxy'); | ||
|
||
$response = $this->queryProxy([ | ||
'url' => $targetUrl, | ||
'uHash' => $uHash, | ||
], 'POST'); | ||
|
||
$this->assertEquals(405, $response->getStatusCode()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function sendsUserAgentToTarget(): void | ||
{ | ||
$targetUrl = 'http://web:8001/Tests/Fixtures/PageViewProxy/echo_user_agent.php'; | ||
$uHash = GeneralUtility::hmac($targetUrl, 'PageViewProxy'); | ||
|
||
$response = $this->queryProxy([ | ||
'url' => $targetUrl, | ||
'uHash' => $uHash, | ||
]); | ||
|
||
$this->assertEquals(200, $response->getStatusCode()); | ||
$this->assertEquals('Kitodo.Presentation Proxy', (string) $response->getBody()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function canQueryOptions(): void | ||
{ | ||
$response = $this->queryProxy([], 'OPTIONS'); | ||
|
||
$this->assertGreaterThanOrEqual(200, $response->getStatusCode()); | ||
$this->assertLessThan(300, $response->getStatusCode()); | ||
|
||
$this->assertNotEmpty($response->getHeader('Access-Control-Allow-Methods')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
// This router script dynamically sets TYPO3_PATH_ROOT to a TYPO3 test instance, | ||
// then yields control back to PHP server to let it serve the requested file. | ||
// | ||
// In functional tests, this allows us to access the TYPO3 instance via the web | ||
// server. Manually setting TYPO3_PATH_ROOT is necessary because test instances | ||
// merely symlink into "$DLF_ROOT/public/" source files, so TYPO3 would detect | ||
// that as root and not use the correct typo3conf folder. | ||
// | ||
// The TYPO3_PATH_ROOT apparently needs to be absolute, or TYPO3 may throw an | ||
// error about missing extension configuration. | ||
|
||
$matches = []; | ||
preg_match("@.*/(?:acceptance|functional-[a-z0-9]+)@", $_SERVER['REQUEST_URI'], $matches); | ||
|
||
if (!empty($matches)) { | ||
$root = realpath($_SERVER['DOCUMENT_ROOT'] . $matches[0]); | ||
if ($root !== false) { | ||
putenv('TYPO3_PATH_ROOT=' . $root); | ||
putenv('TYPO3_PATH_APP=' . $root); | ||
} | ||
} | ||
|
||
return false; |