418185e4c5290144d8fd85a00819e6147f2d4dde
[phinde.git] / tests / HubUrlExtractorTest.php
1 <?php
2 class HubUrlExtractorTest extends \PHPUnit\Framework\TestCase
3 {
4     public function testGetUrlsHEAD()
5     {
6         $mock = new HTTP_Request2_Adapter_Mock();
7         $this->addResponse(
8             $mock,
9             "HTTP/1.0 200 OK\r\n"
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"
13             . "\r\n",
14             'http://example.org/'
15         );
16
17         $extractor = new phinde\HubUrlExtractor();
18         $extractor->setRequestTemplate(
19             new HTTP_Request2(null, null, ['adapter' => $mock])
20         );
21
22         $this->assertEquals(
23             [
24                 'hub'  => 'https://hub.example.com/',
25                 'self' => 'http://example.com/feed',
26             ],
27             $extractor->getUrls('http://example.org/')
28         );
29     }
30
31     public function testGetUrlsHtml()
32     {
33         $mock = new HTTP_Request2_Adapter_Mock();
34         //HEAD
35         $this->addResponse(
36             $mock,
37             "HTTP/1.0 200 OK\r\n"
38             . "Content-type: text/html\r\n"
39             . "\r\n",
40             'http://example.org/'
41         );
42         //HEAD
43         $this->addResponse(
44             $mock,
45             "HTTP/1.0 200 OK\r\n"
46             . "Content-type: text/html\r\n"
47             . "\r\n"
48             . <<<HTM
49 <html>
50  <head>
51   <link rel='hub' href='https://hub.example.com/'/>
52   <link rel='self' href='http://example.com/feed'/>
53  </head>
54 </html>
55 HTM,
56             'http://example.org/'
57         );
58
59         $extractor = new phinde\HubUrlExtractor();
60         $extractor->setRequestTemplate(
61             new HTTP_Request2(null, null, ['adapter' => $mock])
62         );
63
64         $this->assertEquals(
65             [
66                 'hub'  => 'https://hub.example.com/',
67                 'self' => 'http://example.com/feed',
68             ],
69             $extractor->getUrls('http://example.org/')
70         );
71     }
72
73     public function testGetUrlsXHtml()
74     {
75         $mock = new HTTP_Request2_Adapter_Mock();
76         //HEAD
77         $this->addResponse(
78             $mock,
79             "HTTP/1.0 200 OK\r\n"
80             . "Content-type: application/xhtml+xml\r\n"
81             . "\r\n",
82             'http://example.org/'
83         );
84         //HEAD
85         $this->addResponse(
86             $mock,
87             "HTTP/1.0 200 OK\r\n"
88             . "Content-type: application/xhtml+xml\r\n"
89             . "\r\n"
90             . <<<HTM
91 <html>
92  <head>
93   <link rel='hub' href='https://hub.example.com/'/>
94   <link rel='self' href='http://example.com/feed'/>
95  </head>
96 </html>
97 HTM,
98             'http://example.org/'
99         );
100
101         $extractor = new phinde\HubUrlExtractor();
102         $extractor->setRequestTemplate(
103             new HTTP_Request2(null, null, ['adapter' => $mock])
104         );
105
106         $this->assertEquals(
107             [
108                 'hub'  => 'https://hub.example.com/',
109                 'self' => 'http://example.com/feed',
110             ],
111             $extractor->getUrls('http://example.org/')
112         );
113     }
114
115     public function testGetUrlsAtom()
116     {
117         $mock = new HTTP_Request2_Adapter_Mock();
118         //HEAD
119         $this->addResponse(
120             $mock,
121             "HTTP/1.0 200 OK\r\n"
122             . "Content-type: application/atom+xml\r\n"
123             . "\r\n",
124             'http://example.org/'
125         );
126         //HEAD
127         $this->addResponse(
128             $mock,
129             "HTTP/1.0 200 OK\r\n"
130             . "Content-type: application/atom+xml\r\n"
131             . "\r\n"
132             . <<<HTM
133 <?xml version="1.0" encoding="utf-8"?>
134 <feed xmlns="http://www.w3.org/2005/Atom">
135  <link href="http://example.org/"/>
136  <link rel="self" href="http://example.com/feed"/>
137  <link rel="hub" href="https://hub.example.com/"/>
138 </feed>
139 HTM,
140             'http://example.org/'
141         );
142
143         $extractor = new phinde\HubUrlExtractor();
144         $extractor->setRequestTemplate(
145             new HTTP_Request2(null, null, ['adapter' => $mock])
146         );
147
148         $this->assertEquals(
149             [
150                 'hub'  => 'https://hub.example.com/',
151                 'self' => 'http://example.com/feed',
152             ],
153             $extractor->getUrls('http://example.org/')
154         );
155     }
156
157     public function testGetUrlsRss2()
158     {
159         $mock = new HTTP_Request2_Adapter_Mock();
160         //HEAD
161         $this->addResponse(
162             $mock,
163             "HTTP/1.0 200 OK\r\n"
164             . "Content-type: application/rss+xml\r\n"
165             . "\r\n",
166             'http://example.org/'
167         );
168         //HEAD
169         $this->addResponse(
170             $mock,
171             "HTTP/1.0 200 OK\r\n"
172             . "Content-type: application/rss+xml\r\n"
173             . "\r\n"
174             . <<<HTM
175 <?xml version="1.0" encoding="utf-8"?>
176 <rss version="2.0">
177  <channel>
178   <link>http://www.example.com/main.html</link>
179   <link rel="self" href="http://example.com/feed"/>
180   <link rel="hub" href="https://hub.example.com/"/>
181  </channel>
182 </rss>
183 HTM,
184             'http://example.org/'
185         );
186
187         $extractor = new phinde\HubUrlExtractor();
188         $extractor->setRequestTemplate(
189             new HTTP_Request2(null, null, ['adapter' => $mock])
190         );
191
192         $this->assertEquals(
193             [
194                 'hub'  => 'https://hub.example.com/',
195                 'self' => 'http://example.com/feed',
196             ],
197             $extractor->getUrls('http://example.org/')
198         );
199     }
200
201     public function testGetUrlsRss2WebLinking()
202     {
203         $mock = new HTTP_Request2_Adapter_Mock();
204         //HEAD
205         $this->addResponse(
206             $mock,
207             "HTTP/1.0 200 OK\r\n"
208             . "Content-type: application/rss+xml\r\n"
209             . "\r\n",
210             'http://example.org/'
211         );
212         //HEAD
213         $this->addResponse(
214             $mock,
215             "HTTP/1.0 200 OK\r\n"
216             . "Content-type: application/rss+xml\r\n"
217             . "\r\n"
218             . <<<HTM
219 <?xml version="1.0" encoding="utf-8"?>
220 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
221  <channel>
222   <link>http://www.example.com/main.html</link>
223   <atom:link rel="self" href="http://example.com/feed"/>
224   <atom:link rel="hub" href="https://hub.example.com/"/>
225  </channel>
226 </rss>
227 HTM,
228             'http://example.org/'
229         );
230
231         $extractor = new phinde\HubUrlExtractor();
232         $extractor->setRequestTemplate(
233             new HTTP_Request2(null, null, ['adapter' => $mock])
234         );
235
236         $this->assertEquals(
237             [
238                 'hub'  => 'https://hub.example.com/',
239                 'self' => 'http://example.com/feed',
240             ],
241             $extractor->getUrls('http://example.org/')
242         );
243     }
244
245     protected function addResponse($mock, $responseContent, $effectiveUrl)
246     {
247         $mock->addResponse(
248             static::createResponseFromString($responseContent, $effectiveUrl)
249         );
250     }
251
252     public static function createResponseFromString($str, $effectiveUrl)
253     {
254         $parts       = preg_split('!(\r?\n){2}!m', $str, 2);
255         $headerLines = explode("\n", $parts[0]);
256         $response    = new HTTP_Request2_Response(
257             array_shift($headerLines), true, $effectiveUrl
258         );
259         foreach ($headerLines as $headerLine) {
260             $response->parseHeaderLine($headerLine);
261         }
262         $response->parseHeaderLine('');
263         if (isset($parts[1])) {
264             $response->appendBody($parts[1]);
265         }
266         return $response;
267     }
268 }
269 ?>