2 class HubUrlExtractorTest extends \PHPUnit\Framework\TestCase
4 public function testGetUrlsHEAD()
6 $mock = new HTTP_Request2_Adapter_Mock();
10 . "Content-type: text/html\r\n"
11 . "Link: <https://hub.example.com/>; rel=\"hub\"\r\n"
12 . "Link: <http://example.com/feed>; rel=\"self\"\r\n"
17 $extractor = new phinde\HubUrlExtractor();
18 $extractor->setRequestTemplate(
19 new HTTP_Request2(null, null, ['adapter' => $mock])
24 'hub' => ['https://hub.example.com/'],
25 'self' => 'http://example.com/feed',
27 $extractor->getUrls('http://example.org/')
31 public function testGetUrlsMultipleHubsHEAD()
33 $mock = new HTTP_Request2_Adapter_Mock();
37 . "Content-type: text/html\r\n"
38 . "Link: <https://hub.example.com/>; rel=\"hub\"\r\n"
39 . "Link: <https://hub2.example.com/>; rel=\"hub\"\r\n"
40 . "Link: <http://example.com/feed>; rel=\"self\"\r\n"
41 . "Link: <https://hub3.example.com/>; rel=\"hub\"\r\n"
46 $extractor = new phinde\HubUrlExtractor();
47 $extractor->setRequestTemplate(
48 new HTTP_Request2(null, null, ['adapter' => $mock])
54 'https://hub.example.com/',
55 'https://hub2.example.com/',
56 'https://hub3.example.com/',
58 'self' => 'http://example.com/feed',
60 $extractor->getUrls('http://example.org/')
64 public function testGetUrlsHtml()
66 $mock = new HTTP_Request2_Adapter_Mock();
71 . "Content-type: text/html\r\n"
79 . "Content-type: text/html\r\n"
84 <link rel='hub' href='https://hub.example.com/'/>
85 <link rel='self' href='http://example.com/feed'/>
92 $extractor = new phinde\HubUrlExtractor();
93 $extractor->setRequestTemplate(
94 new HTTP_Request2(null, null, ['adapter' => $mock])
99 'hub' => ['https://hub.example.com/'],
100 'self' => 'http://example.com/feed',
102 $extractor->getUrls('http://example.org/')
106 public function testGetUrlsHtmlMultipleHubs()
108 $mock = new HTTP_Request2_Adapter_Mock();
112 "HTTP/1.0 200 OK\r\n"
113 . "Content-type: text/html\r\n"
115 'http://example.org/'
120 "HTTP/1.0 200 OK\r\n"
121 . "Content-type: text/html\r\n"
126 <link rel='hub' href='https://hub.example.com/'/>
127 <link rel='hub' href='https://hub2.example.com/'/>
128 <link rel='self' href='http://example.com/feed'/>
132 'http://example.org/'
135 $extractor = new phinde\HubUrlExtractor();
136 $extractor->setRequestTemplate(
137 new HTTP_Request2(null, null, ['adapter' => $mock])
143 'https://hub.example.com/',
144 'https://hub2.example.com/',
146 'self' => 'http://example.com/feed',
148 $extractor->getUrls('http://example.org/')
152 public function testGetUrlsXHtml()
154 $mock = new HTTP_Request2_Adapter_Mock();
158 "HTTP/1.0 200 OK\r\n"
159 . "Content-type: application/xhtml+xml\r\n"
161 'http://example.org/'
166 "HTTP/1.0 200 OK\r\n"
167 . "Content-type: application/xhtml+xml\r\n"
172 <link rel='hub' href='https://hub.example.com/'/>
173 <link rel='self' href='http://example.com/feed'/>
177 'http://example.org/'
180 $extractor = new phinde\HubUrlExtractor();
181 $extractor->setRequestTemplate(
182 new HTTP_Request2(null, null, ['adapter' => $mock])
187 'hub' => ['https://hub.example.com/'],
188 'self' => 'http://example.com/feed',
190 $extractor->getUrls('http://example.org/')
194 public function testGetUrlsAtom()
196 $mock = new HTTP_Request2_Adapter_Mock();
200 "HTTP/1.0 200 OK\r\n"
201 . "Content-type: application/atom+xml\r\n"
203 'http://example.org/'
208 "HTTP/1.0 200 OK\r\n"
209 . "Content-type: application/atom+xml\r\n"
212 <?xml version="1.0" encoding="utf-8"?>
213 <feed xmlns="http://www.w3.org/2005/Atom">
214 <link href="http://example.org/"/>
215 <link rel="self" href="http://example.com/feed"/>
216 <link rel="hub" href="https://hub.example.com/"/>
219 'http://example.org/'
222 $extractor = new phinde\HubUrlExtractor();
223 $extractor->setRequestTemplate(
224 new HTTP_Request2(null, null, ['adapter' => $mock])
229 'hub' => ['https://hub.example.com/'],
230 'self' => 'http://example.com/feed',
232 $extractor->getUrls('http://example.org/')
236 public function testGetUrlsRss2()
238 $mock = new HTTP_Request2_Adapter_Mock();
242 "HTTP/1.0 200 OK\r\n"
243 . "Content-type: application/rss+xml\r\n"
245 'http://example.org/'
250 "HTTP/1.0 200 OK\r\n"
251 . "Content-type: application/rss+xml\r\n"
254 <?xml version="1.0" encoding="utf-8"?>
257 <link>http://www.example.com/main.html</link>
258 <link rel="self" href="http://example.com/feed"/>
259 <link rel="hub" href="https://hub.example.com/"/>
263 'http://example.org/'
266 $extractor = new phinde\HubUrlExtractor();
267 $extractor->setRequestTemplate(
268 new HTTP_Request2(null, null, ['adapter' => $mock])
273 'hub' => ['https://hub.example.com/'],
274 'self' => 'http://example.com/feed',
276 $extractor->getUrls('http://example.org/')
280 public function testGetUrlsRss2WebLinking()
282 $mock = new HTTP_Request2_Adapter_Mock();
286 "HTTP/1.0 200 OK\r\n"
287 . "Content-type: application/rss+xml\r\n"
289 'http://example.org/'
294 "HTTP/1.0 200 OK\r\n"
295 . "Content-type: application/rss+xml\r\n"
298 <?xml version="1.0" encoding="utf-8"?>
299 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
301 <link>http://www.example.com/main.html</link>
302 <atom:link rel="self" href="http://example.com/feed"/>
303 <atom:link rel="hub" href="https://hub.example.com/"/>
307 'http://example.org/'
310 $extractor = new phinde\HubUrlExtractor();
311 $extractor->setRequestTemplate(
312 new HTTP_Request2(null, null, ['adapter' => $mock])
317 'hub' => ['https://hub.example.com/'],
318 'self' => 'http://example.com/feed',
320 $extractor->getUrls('http://example.org/')
324 protected function addResponse($mock, $responseContent, $effectiveUrl)
327 static::createResponseFromString($responseContent, $effectiveUrl)
331 public static function createResponseFromString($str, $effectiveUrl)
333 $parts = preg_split('!(\r?\n){2}!m', $str, 2);
334 $headerLines = explode("\n", $parts[0]);
335 $response = new HTTP_Request2_Response(
336 array_shift($headerLines), true, $effectiveUrl
338 foreach ($headerLines as $headerLine) {
339 $response->parseHeaderLine($headerLine);
341 $response->parseHeaderLine('');
342 if (isset($parts[1])) {
343 $response->appendBody($parts[1]);
349 * It is possible to subscribe to URLs that redirect if
350 * they have a hub and self links in the HTTP headers.
351 * If they don't, we need to follow the redirect.
353 public function testGetUrlsHEADLinksForRedirect()
355 $mock = new HTTP_Request2_Adapter_Mock();
358 "HTTP/1.0 307 Temporary Redirect\r\n"
359 . "Content-type: text/html\r\n"
360 . "Location: http://example.org/redir-target\r\n"
361 . "Link: <https://hub.example.com/>; rel=\"hub\"\r\n"
362 . "Link: <http://example.com/feed>; rel=\"self\"\r\n"
364 'http://example.org/'
368 "HTTP/1.0 200 OK\r\n"
369 . "Content-type: text/html\r\n"
370 . "Link: <https://redir-hub.example.com/>; rel=\"hub\"\r\n"
371 . "Link: <http://example.com/redir-feed>; rel=\"self\"\r\n"
373 'http://example.org/redir-target'
376 $extractor = new phinde\HubUrlExtractor();
377 $extractor->setRequestTemplate(
378 new HTTP_Request2(null, null, ['adapter' => $mock])
383 'hub' => ['https://hub.example.com/'],
384 'self' => 'http://example.com/feed',
386 $extractor->getUrls('http://example.org/')
390 public function testGetUrlsHEADLinksForRedirectNone()
392 $mock = new HTTP_Request2_Adapter_Mock();
395 "HTTP/1.0 307 Temporary Redirect\r\n"
396 . "Content-type: text/html\r\n"
397 . "Location: http://example.org/redir-target\r\n"
399 'http://example.org/'
403 "HTTP/1.0 200 OK\r\n"
404 . "Content-type: text/html\r\n"
405 . "Link: <https://redir-hub.example.com/>; rel=\"hub\"\r\n"
406 . "Link: <http://example.com/redir-feed>; rel=\"self\"\r\n"
408 'http://example.org/redir-target'
411 $extractor = new phinde\HubUrlExtractor();
412 $extractor->setRequestTemplate(
413 new HTTP_Request2(null, null, ['adapter' => $mock])
418 'hub' => ['https://redir-hub.example.com/'],
419 'self' => 'http://example.com/redir-feed',
421 $extractor->getUrls('http://example.org/')