Add atom and rss feed link url extraction
[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     protected function addResponse($mock, $responseContent, $effectiveUrl)
202     {
203         $mock->addResponse(
204             static::createResponseFromString($responseContent, $effectiveUrl)
205         );
206     }
207
208     public static function createResponseFromString($str, $effectiveUrl)
209     {
210         $parts       = preg_split('!(\r?\n){2}!m', $str, 2);
211         $headerLines = explode("\n", $parts[0]);
212         $response    = new HTTP_Request2_Response(
213             array_shift($headerLines), true, $effectiveUrl
214         );
215         foreach ($headerLines as $headerLine) {
216             $response->parseHeaderLine($headerLine);
217         }
218         $response->parseHeaderLine('');
219         if (isset($parts[1])) {
220             $response->appendBody($parts[1]);
221         }
222         return $response;
223     }
224 }
225 ?>