1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kitodo\Dlf\Tests\Functional\Api; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as HttpClient; |
6
|
|
|
use Kitodo\Dlf\Tests\Functional\FunctionalTestCase; |
7
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
8
|
|
|
|
9
|
|
|
class PageViewProxyTest extends FunctionalTestCase |
10
|
|
|
{ |
11
|
|
|
protected $disableJsonWrappedResponse = true; |
12
|
|
|
|
13
|
|
|
protected function getDlfConfiguration() |
14
|
|
|
{ |
15
|
|
|
return array_merge(parent::getDlfConfiguration(), [ |
16
|
|
|
'enableInternalProxy' => true, |
17
|
|
|
]); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
protected function queryProxy(array $query, string $method = 'GET') |
21
|
|
|
{ |
22
|
|
|
$query['eID'] = 'tx_dlf_pageview_proxy'; |
23
|
|
|
|
24
|
|
|
return $this->httpClient->request($method, '', [ |
25
|
|
|
'query' => $query, |
26
|
|
|
]); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @test |
31
|
|
|
*/ |
32
|
|
|
public function cannotAccessFileUrl(): void |
33
|
|
|
{ |
34
|
|
|
$response = $this->queryProxy([ |
35
|
|
|
'url' => 'file:///etc/passwd', |
36
|
|
|
]); |
37
|
|
|
|
38
|
|
|
$this->assertEquals(400, $response->getStatusCode()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @test |
43
|
|
|
*/ |
44
|
|
|
public function cannotAccessUrlWithoutUrlHash(): void |
45
|
|
|
{ |
46
|
|
|
$response = $this->queryProxy([ |
47
|
|
|
'url' => 'http://web:8001/Tests/Fixtures/PageViewProxy/test.txt', |
48
|
|
|
]); |
49
|
|
|
|
50
|
|
|
$this->assertEquals(401, $response->getStatusCode()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @test |
55
|
|
|
*/ |
56
|
|
|
public function cannotAccessUrlWithInvalidUrlHash(): void |
57
|
|
|
{ |
58
|
|
|
$response = $this->queryProxy([ |
59
|
|
|
'url' => 'http://web:8001/Tests/Fixtures/PageViewProxy/test.txt', |
60
|
|
|
'uHash' => 'nottherealhash', |
61
|
|
|
]); |
62
|
|
|
|
63
|
|
|
$this->assertEquals(401, $response->getStatusCode()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @test |
68
|
|
|
*/ |
69
|
|
|
public function canAccessPageWithUrlHash(): void |
70
|
|
|
{ |
71
|
|
|
$targetUrl = 'http://web:8001/Tests/Fixtures/PageViewProxy/test.txt'; |
72
|
|
|
$uHash = GeneralUtility::hmac($targetUrl, 'PageViewProxy'); |
73
|
|
|
|
74
|
|
|
$response = $this->queryProxy([ |
75
|
|
|
'url' => $targetUrl, |
76
|
|
|
'uHash' => $uHash, |
77
|
|
|
]); |
78
|
|
|
|
79
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
80
|
|
|
$this->assertEquals('This is some plain text test file.' . "\n", (string) $response->getBody()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @test |
85
|
|
|
*/ |
86
|
|
|
public function cannotSendPostRequest(): void |
87
|
|
|
{ |
88
|
|
|
$targetUrl = 'http://web:8001/Tests/Fixtures/PageViewProxy/test.txt'; |
89
|
|
|
$uHash = GeneralUtility::hmac($targetUrl, 'PageViewProxy'); |
90
|
|
|
|
91
|
|
|
$response = $this->queryProxy([ |
92
|
|
|
'url' => $targetUrl, |
93
|
|
|
'uHash' => $uHash, |
94
|
|
|
], 'POST'); |
95
|
|
|
|
96
|
|
|
$this->assertEquals(405, $response->getStatusCode()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @test |
101
|
|
|
*/ |
102
|
|
|
public function sendsUserAgentToTarget(): void |
103
|
|
|
{ |
104
|
|
|
$targetUrl = 'http://web:8001/Tests/Fixtures/PageViewProxy/echo_user_agent.php'; |
105
|
|
|
$uHash = GeneralUtility::hmac($targetUrl, 'PageViewProxy'); |
106
|
|
|
|
107
|
|
|
$response = $this->queryProxy([ |
108
|
|
|
'url' => $targetUrl, |
109
|
|
|
'uHash' => $uHash, |
110
|
|
|
]); |
111
|
|
|
|
112
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
113
|
|
|
$this->assertEquals('Kitodo.Presentation Proxy', (string) $response->getBody()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @test |
118
|
|
|
*/ |
119
|
|
|
public function canQueryOptions(): void |
120
|
|
|
{ |
121
|
|
|
$response = $this->queryProxy([], 'OPTIONS'); |
122
|
|
|
|
123
|
|
|
$this->assertGreaterThanOrEqual(200, $response->getStatusCode()); |
124
|
|
|
$this->assertLessThan(300, $response->getStatusCode()); |
125
|
|
|
|
126
|
|
|
$this->assertNotEmpty($response->getHeader('Access-Control-Allow-Methods')); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|