3 namespace Friendica\Test\src\Network;
5 use Friendica\Network\Probe;
6 use Friendica\Test\DiceHttpMockHandlerTrait;
7 use Friendica\Test\FixtureTest;
8 use GuzzleHttp\Middleware;
10 class ProbeTest extends FixtureTest
12 use DiceHttpMockHandlerTrait;
14 protected function setUp(): void
18 $this->setupHttpMockHandler();
21 const TEMPLATENOBASE = '
25 <title>Example Blog</title>
26 <link href="{{$link}}" rel="alternate" type="application/rss+xml" title="Example Blog" />
27 <link href="{{$link}}" rel="feed" type="application/rss+xml" title="Example Blog" />
34 const TEMPLATEBASE = '
38 <title>Example Blog</title>
39 <link href="{{$link}}" rel="alternate" type="application/rss+xml" title="Example Blog" />
40 <link href="{{$link}}" rel="feed" type="application/rss+xml" title="Example Blog" />
41 <base href="{{$url}}">
49 'https://example.org/path/to/blog/index.php' => [
50 'index.xml' => 'https://example.org/path/to/blog/index.xml',
51 './index.xml' => 'https://example.org/path/to/blog/index.xml',
52 '../index.xml' => 'https://example.org/path/to/index.xml',
53 '/index.xml' => 'https://example.org/index.xml',
54 '//example.com/index.xml' => 'https://example.com/index.xml',
56 'https://example.org/path/to/blog/' => [
57 'index.xml' => 'https://example.org/path/to/blog/index.xml',
58 './index.xml' => 'https://example.org/path/to/blog/index.xml',
59 '../index.xml' => 'https://example.org/path/to/index.xml',
60 '/index.xml' => 'https://example.org/index.xml',
61 '//example.com/index.xml' => 'https://example.com/index.xml',
63 'https://example.org/blog/' => [
64 'index.xml' => 'https://example.org/blog/index.xml',
65 './index.xml' => 'https://example.org/blog/index.xml',
66 '../index.xml' => 'https://example.org/index.xml',
67 '/index.xml' => 'https://example.org/index.xml',
68 '//example.com/index.xml' => 'https://example.com/index.xml',
70 'https://example.org' => [
71 'index.xml' => 'https://example.org/index.xml',
72 './index.xml' => 'https://example.org/index.xml',
73 '../index.xml' => 'https://example.org/index.xml',
74 '/index.xml' => 'https://example.org/index.xml',
75 '//example.com/index.xml' => 'https://example.com/index.xml',
79 private function replaceMacros($template, $vars)
81 foreach ($vars as $var => $value) {
82 $template = str_replace('{{' . $var . '}}', $value, $template);
91 public function testGetFeedLinkNoBase()
93 foreach (self::EXPECTED as $url => $hrefs) {
94 foreach ($hrefs as $href => $expected) {
95 $body = $this->replaceMacros(self::TEMPLATENOBASE, ['$link' => $href]);
97 $feedLink = Probe::getFeedLink($url, $body);
99 self::assertEquals($expected, $feedLink, 'base url = ' . $url . ' | href = ' . $href);
107 public function testGetFeedLinkBase()
109 foreach (self::EXPECTED as $url => $hrefs) {
110 foreach ($hrefs as $href => $expected) {
111 $body = $this->replaceMacros(self::TEMPLATEBASE, ['$url' => $url, '$link' => $href]);
113 $feedLink = Probe::getFeedLink('http://example.com', $body);
115 self::assertEquals($expected, $feedLink, 'base url = ' . $url . ' | href = ' . $href);
120 public function dataUri()
124 'uri' => '@Artists4Future_Muenchen@climatejustice.global',
125 'assertUri' => 'Artists4Future_Muenchen@climatejustice.global',
127 'name' => 'Artists4Future München',
128 'nick' => 'Artists4Future_Muenchen',
129 'url' => 'https://climatejustice.global/users/Artists4Future_Muenchen',
130 'alias' => 'https://climatejustice.global/@Artists4Future_Muenchen',
131 'photo' => 'https://cdn.masto.host/climatejusticeglobal/accounts/avatars/000/021/220/original/05ee9e827a5b47fc.jpg',
132 'header' => 'https://cdn.masto.host/climatejusticeglobal/accounts/headers/000/021/220/original/9b98b75cf696cd11.jpg',
134 'about' => 'Wir sind Künstler oder einfach gerne kreativ tätig und setzen uns unabhängig von politischen Parteien für den Klimaschutz ein. Die Bedingungen zu schaffen, die die [url=https://climatejustice.global/tags/Klimakrise]#Klimakrise[/url] verhindern/eindämmen (gemäß den Forderungen der [url=https://climatejustice.global/tags/Fridays4Future]#Fridays4Future[/url]) ist Aufgabe der Politik, muss aber gesamtgesellschaftlich getragen werden. Mit unseren künstlerischen Aktionen wollen wir einen anderen Zugang anbieten für wissenschaftlich rationale Argumente, speziell zur Erderwärmung und ihre Konsequenzen.',
136 'batch' => 'https://climatejustice.global/inbox',
137 'notify' => 'https://climatejustice.global/users/Artists4Future_Muenchen/inbox',
138 'poll' => 'https://climatejustice.global/users/Artists4Future_Muenchen/outbox',
139 'subscribe' => 'https://climatejustice.global/authorize_interaction?uri={uri}',
140 'following' => 'https://climatejustice.global/users/Artists4Future_Muenchen/following',
141 'followers' => 'https://climatejustice.global/users/Artists4Future_Muenchen/followers',
142 'inbox' => 'https://climatejustice.global/users/Artists4Future_Muenchen/inbox',
143 'outbox' => 'https://climatejustice.global/users/Artists4Future_Muenchen/outbox',
144 'sharedinbox' => 'https://climatejustice.global/inbox',
147 'pubkey' => '-----BEGIN PUBLIC KEY-----
148 MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6pYKPuDKb+rmBB869uPV
149 uLYFPosGxMUfenWqfWmFKzEqJ87rAft0IQDAL6dCoYE55ov/lEDNROhasTZLirZf
150 M5b7/1JmwMrAfEiaciuYqDWT3/yDpnekOIdzP5iSClg4zt7e6HRFuClqo4+b6hIE
151 DTMV4ksItvq/92MIu62pZ2SZr5ADPPZ/914lJ86hIH5BanbE8ZFzDS9vJA7V74rt
152 Vvkr5c/OiUyuODNYApSl87Ez8cuj8Edt89YWkDCajQn3EkmXGeJY/VRjEDfcyk6r
153 AvdUa0ArjXud3y3NkakVFZ0d7tmB20Vn9s/CfYHU8FXzbI1kFkov2BX899VVP5Ay
155 -----END PUBLIC KEY-----',
156 'manually-approve' => 0,
157 'baseurl' => 'https://climatejustice.global',
164 * @dataProvider dataUri
166 public function testCleanUri(string $uri, string $assertUri, array $assertInfos)
168 self::markTestIncomplete('hard work due mocking 19 different http-requests');
173 * GET : https://climatejustice.global/.well-known/webfinger?resource=acct:Artists4Future_Muenchen%40climatejustice.global
175 * GET : http://localhost/.well-known/nodeinfo
177 * GET : http://localhost/statistics.json
179 * GET : http://localhost
181 * GET : http://localhost/friendica/json
183 * GET : http://localhost/friendika/json
185 * GET : http://localhost/poco
187 * GET : http://localhost/api/v1/directory?limit=1
189 * GET : http://localhost/.well-known/x-social-relay
191 * GET : http://localhost/friendica
193 * GET : https://climatejustice.global/users/Artists4Future_Muenchen
195 * GET : https://climatejustice.global/users/Artists4Future_Muenchen/following
197 * GET : https://climatejustice.global/users/Artists4Future_Muenchen/followers
199 * GET : https://climatejustice.global/users/Artists4Future_Muenchen/outbox
201 * GET : https://climatejustice.global/.well-known/nodeinfo
203 * GET : https://climatejustice.global/nodeinfo/2.0
205 * GET : https://climatejustice.global/poco
207 * GET : https://climatejustice.global/api/v1/directory?limit=1
209 * GET : https://climatejustice.global/.well-known/webfinger?resource=acct%3AArtists4Future_Muenchen%40climatejustice.global
215 $history = Middleware::history($container);
217 $this->httpRequestHandler->push($history);
219 $cleaned = Probe::cleanURI($uri);
220 self::assertEquals($assertUri, $cleaned);
221 self::assertArraySubset($assertInfos, Probe::uri($cleaned, '', 0));
224 // Iterate over the requests and responses
225 foreach ($container as $transaction) {
226 echo $transaction['request']->getMethod() . " : " . $transaction['request']->getUri() . PHP_EOL;
228 if ($transaction['response']) {
229 echo $transaction['response']->getStatusCode() . PHP_EOL;
231 } elseif ($transaction['error']) {
232 echo $transaction['error'];