1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Tests for http_request.inc.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Tests for the http library.
|
10
|
*/
|
11
|
class FeedsHTTPRequestTestCase extends FeedsUnitTestHelper {
|
12
|
|
13
|
/**
|
14
|
* {@inheritdoc}
|
15
|
*/
|
16
|
public static function getInfo() {
|
17
|
return array(
|
18
|
'name' => 'HTTP library',
|
19
|
'description' => 'Tests for Feeds HTTP library.',
|
20
|
'group' => 'Feeds',
|
21
|
);
|
22
|
}
|
23
|
|
24
|
/**
|
25
|
* {@inheritdoc}
|
26
|
*/
|
27
|
public function setUp() {
|
28
|
parent::setUp();
|
29
|
feeds_include_library('http_request.inc', 'http_request');
|
30
|
}
|
31
|
|
32
|
/**
|
33
|
* Tests http_request_find_feeds().
|
34
|
*/
|
35
|
public function testHTTPRequestFindFeeds() {
|
36
|
$html = <<<EOF
|
37
|
<html>
|
38
|
<head>
|
39
|
<title>Welcome to Example.com</title>
|
40
|
<link rel="stylesheet" type="text/css" media="screen, projection" href="/stuff.css" >
|
41
|
<link rel="search" title="Something" href="//example.com/search">
|
42
|
<link rel="alternate" title="Something RSS" href="http://example.com/rss.xml" type="application/rss+xml">
|
43
|
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
|
44
|
</head>
|
45
|
<body>
|
46
|
This is a body.
|
47
|
</body>
|
48
|
</html
|
49
|
EOF;
|
50
|
|
51
|
$links = http_request_find_feeds($html);
|
52
|
$this->assertEqual(count($links), 1);
|
53
|
$this->assertEqual($links[0], 'http://example.com/rss.xml');
|
54
|
|
55
|
// Test single quoted HTML.
|
56
|
$links = http_request_find_feeds(str_replace('"', "'", $html));
|
57
|
$this->assertEqual(count($links), 1);
|
58
|
$this->assertEqual($links[0], 'http://example.com/rss.xml');
|
59
|
}
|
60
|
|
61
|
/**
|
62
|
* Tests fetching data from a protected directory.
|
63
|
*/
|
64
|
public function testProtectedPages() {
|
65
|
// Create a directory and put a file in it.
|
66
|
$dir = 'public://feeds-protected';
|
67
|
file_prepare_directory($dir, FILE_CREATE_DIRECTORY);
|
68
|
file_put_contents($dir . '/content.txt', 'A protected document');
|
69
|
|
70
|
// Create .htpasswd and .htaccess files.
|
71
|
file_put_contents($dir . '/.htpasswd', 'Morticia:$apr1$s3SZUBCe$loi5XqSMHJ9CRfsY9GzLW/');
|
72
|
$htaccess = "AuthType Basic\nAuthName \"Restricted area\"\nAuthUserFile !path\nrequire valid-user";
|
73
|
$htaccess = strtr($htaccess, array(
|
74
|
'!path' => drupal_realpath($dir . '/.htpasswd'),
|
75
|
));
|
76
|
file_put_contents($dir . '/.htaccess', $htaccess);
|
77
|
|
78
|
// Create url to file.
|
79
|
$url = file_create_url($dir . '/content.txt');
|
80
|
|
81
|
// Try to access the file without username/password.
|
82
|
$result = feeds_http_request($url, array(
|
83
|
'cache_http_result' => FALSE,
|
84
|
));
|
85
|
// Assert that it was forbidden.
|
86
|
$this->assertEqual(401, $result->code);
|
87
|
$this->assertNotEqual('A protected document', $result->data);
|
88
|
|
89
|
// Now access the same with username/password.
|
90
|
$result = feeds_http_request($url, array(
|
91
|
'username' => 'Morticia',
|
92
|
'password' => 'mort',
|
93
|
'cache_http_result' => FALSE,
|
94
|
));
|
95
|
// And assert that this time the request was successful.
|
96
|
$this->assertEqual(200, $result->code);
|
97
|
$this->assertEqual('A protected document', $result->data);
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* Tests http_request_create_absolute_url().
|
102
|
*/
|
103
|
public function testHTTPRequestCreateAbsoluteUrl() {
|
104
|
$test_urls = array(
|
105
|
// Rels that do not start with "/".
|
106
|
array(
|
107
|
'rel' => 'h',
|
108
|
'base' => 'http://www',
|
109
|
'expected' => 'http://www/h',
|
110
|
),
|
111
|
array(
|
112
|
'rel' => 'h',
|
113
|
'base' => 'http://www/',
|
114
|
'expected' => 'http://www/h',
|
115
|
),
|
116
|
array(
|
117
|
'rel' => 'h',
|
118
|
'base' => 'http://www/?c;d=e#f',
|
119
|
'expected' => 'http://www/h',
|
120
|
),
|
121
|
array(
|
122
|
'rel' => 'h',
|
123
|
'base' => 'http://www/a/b',
|
124
|
'expected' => 'http://www/a/h',
|
125
|
),
|
126
|
|
127
|
array(
|
128
|
'rel' => 'h/j',
|
129
|
'base' => 'http://www',
|
130
|
'expected' => 'http://www/h/j',
|
131
|
),
|
132
|
array(
|
133
|
'rel' => 'h/j',
|
134
|
'base' => 'http://www/',
|
135
|
'expected' => 'http://www/h/j',
|
136
|
),
|
137
|
array(
|
138
|
'rel' => 'h/j',
|
139
|
'base' => 'http://www/?c;d=e#f',
|
140
|
'expected' => 'http://www/h/j',
|
141
|
),
|
142
|
array(
|
143
|
'rel' => 'h/j',
|
144
|
'base' => 'http://www/a/b',
|
145
|
'expected' => 'http://www/a/h/j',
|
146
|
),
|
147
|
array(
|
148
|
'rel' => 'h/j',
|
149
|
'base' => 'http://www/a/b/',
|
150
|
'expected' => 'http://www/a/b/h/j',
|
151
|
),
|
152
|
|
153
|
// Rels that start with "/".
|
154
|
array(
|
155
|
'rel' => '/h',
|
156
|
'base' => 'http://www',
|
157
|
'expected' => 'http://www/h',
|
158
|
),
|
159
|
array(
|
160
|
'rel' => '/h',
|
161
|
'base' => 'http://www/',
|
162
|
'expected' => 'http://www/h',
|
163
|
),
|
164
|
array(
|
165
|
'rel' => '/h',
|
166
|
'base' => 'http://www/?c;d=e#f',
|
167
|
'expected' => 'http://www/h',
|
168
|
),
|
169
|
array(
|
170
|
'rel' => '/h',
|
171
|
'base' => 'http://www/a/b',
|
172
|
'expected' => 'http://www/h',
|
173
|
),
|
174
|
|
175
|
array(
|
176
|
'rel' => '/h/j',
|
177
|
'base' => 'http://www',
|
178
|
'expected' => 'http://www/h/j',
|
179
|
),
|
180
|
array(
|
181
|
'rel' => '/h/j',
|
182
|
'base' => 'http://www/',
|
183
|
'expected' => 'http://www/h/j',
|
184
|
),
|
185
|
array(
|
186
|
'rel' => '/h/j',
|
187
|
'base' => 'http://www/?c;d=e#f',
|
188
|
'expected' => 'http://www/h/j',
|
189
|
),
|
190
|
array(
|
191
|
'rel' => '/h/j',
|
192
|
'base' => 'http://www/a/b',
|
193
|
'expected' => 'http://www/h/j',
|
194
|
),
|
195
|
array(
|
196
|
'rel' => '/h/j',
|
197
|
'base' => 'http://www/a/b/',
|
198
|
'expected' => 'http://www/h/j',
|
199
|
),
|
200
|
|
201
|
// Rels that contain ".".
|
202
|
array(
|
203
|
'rel' => './h',
|
204
|
'base' => 'http://www',
|
205
|
'expected' => 'http://www/h',
|
206
|
),
|
207
|
array(
|
208
|
'rel' => './h',
|
209
|
'base' => 'http://www/',
|
210
|
'expected' => 'http://www/h',
|
211
|
),
|
212
|
array(
|
213
|
'rel' => './h',
|
214
|
'base' => 'http://www/?c;d=e#f',
|
215
|
'expected' => 'http://www/h',
|
216
|
),
|
217
|
array(
|
218
|
'rel' => './h',
|
219
|
'base' => 'http://www/a/b',
|
220
|
'expected' => 'http://www/a/h',
|
221
|
),
|
222
|
|
223
|
array(
|
224
|
'rel' => './h/j',
|
225
|
'base' => 'http://www',
|
226
|
'expected' => 'http://www/h/j',
|
227
|
),
|
228
|
array(
|
229
|
'rel' => './h/j',
|
230
|
'base' => 'http://www/',
|
231
|
'expected' => 'http://www/h/j',
|
232
|
),
|
233
|
array(
|
234
|
'rel' => './h/j',
|
235
|
'base' => 'http://www/?c;d=e#f',
|
236
|
'expected' => 'http://www/h/j',
|
237
|
),
|
238
|
array(
|
239
|
'rel' => './h/j',
|
240
|
'base' => 'http://www/a/b',
|
241
|
'expected' => 'http://www/a/h/j',
|
242
|
),
|
243
|
array(
|
244
|
'rel' => './h/j',
|
245
|
'base' => 'http://www/a/b/',
|
246
|
'expected' => 'http://www/a/b/h/j',
|
247
|
),
|
248
|
|
249
|
array(
|
250
|
'rel' => 'h/./j',
|
251
|
'base' => 'http://www',
|
252
|
'expected' => 'http://www/h/j',
|
253
|
),
|
254
|
array(
|
255
|
'rel' => 'h/./j',
|
256
|
'base' => 'http://www/',
|
257
|
'expected' => 'http://www/h/j',
|
258
|
),
|
259
|
array(
|
260
|
'rel' => 'h/./j',
|
261
|
'base' => 'http://www/?c;d=e#f',
|
262
|
'expected' => 'http://www/h/j',
|
263
|
),
|
264
|
array(
|
265
|
'rel' => 'h/./j',
|
266
|
'base' => 'http://www/a/b',
|
267
|
'expected' => 'http://www/a/h/j',
|
268
|
),
|
269
|
array(
|
270
|
'rel' => 'h/./j',
|
271
|
'base' => 'http://www/a/b/',
|
272
|
'expected' => 'http://www/a/b/h/j',
|
273
|
),
|
274
|
|
275
|
array(
|
276
|
'rel' => '/h/./j',
|
277
|
'base' => 'http://www',
|
278
|
'expected' => 'http://www/h/j',
|
279
|
),
|
280
|
array(
|
281
|
'rel' => '/h/./j',
|
282
|
'base' => 'http://www/',
|
283
|
'expected' => 'http://www/h/j',
|
284
|
),
|
285
|
array(
|
286
|
'rel' => '/h/./j',
|
287
|
'base' => 'http://www/?c;d=e#f',
|
288
|
'expected' => 'http://www/h/j',
|
289
|
),
|
290
|
array(
|
291
|
'rel' => '/h/./j',
|
292
|
'base' => 'http://www/a/b',
|
293
|
'expected' => 'http://www/h/j',
|
294
|
),
|
295
|
array(
|
296
|
'rel' => '/h/./j',
|
297
|
'base' => 'http://www/a/b/',
|
298
|
'expected' => 'http://www/h/j',
|
299
|
),
|
300
|
|
301
|
// Rels that starts with "../".
|
302
|
array(
|
303
|
'rel' => '../h/j',
|
304
|
'base' => 'http://www',
|
305
|
'expected' => 'http://www/h/j',
|
306
|
),
|
307
|
array(
|
308
|
'rel' => '../h/j',
|
309
|
'base' => 'http://www/',
|
310
|
'expected' => 'http://www/h/j',
|
311
|
),
|
312
|
array(
|
313
|
'rel' => '../h/j',
|
314
|
'base' => 'http://www/?c;d=e#f',
|
315
|
'expected' => 'http://www/h/j',
|
316
|
),
|
317
|
array(
|
318
|
'rel' => '../h/j',
|
319
|
'base' => 'http://www/a/b',
|
320
|
'expected' => 'http://www/h/j',
|
321
|
),
|
322
|
array(
|
323
|
'rel' => '../h/j',
|
324
|
'base' => 'http://www/a/b/',
|
325
|
'expected' => 'http://www/a/h/j',
|
326
|
),
|
327
|
array(
|
328
|
'rel' => '../h/j',
|
329
|
'base' => 'http://www/a/b/c/',
|
330
|
'expected' => 'http://www/a/b/h/j',
|
331
|
),
|
332
|
|
333
|
// Rels that start with "../../".
|
334
|
array(
|
335
|
'rel' => '../../h/j',
|
336
|
'base' => 'http://www',
|
337
|
'expected' => 'http://www/h/j',
|
338
|
),
|
339
|
array(
|
340
|
'rel' => '../../h/j',
|
341
|
'base' => 'http://www/',
|
342
|
'expected' => 'http://www/h/j',
|
343
|
),
|
344
|
array(
|
345
|
'rel' => '../../h/j',
|
346
|
'base' => 'http://www/?c;d=e#f',
|
347
|
'expected' => 'http://www/h/j',
|
348
|
),
|
349
|
array(
|
350
|
'rel' => '../../h/j',
|
351
|
'base' => 'http://www/a/b',
|
352
|
'expected' => 'http://www/h/j',
|
353
|
),
|
354
|
array(
|
355
|
'rel' => '../../h/j',
|
356
|
'base' => 'http://www/a/b/',
|
357
|
'expected' => 'http://www/h/j',
|
358
|
),
|
359
|
array(
|
360
|
'rel' => '../../h/j',
|
361
|
'base' => 'http://www/a/b/c/',
|
362
|
'expected' => 'http://www/a/h/j',
|
363
|
),
|
364
|
array(
|
365
|
'rel' => '../../h/j',
|
366
|
'base' => 'http://www/a/b/c/d',
|
367
|
'expected' => 'http://www/a/h/j',
|
368
|
),
|
369
|
|
370
|
// Crazy rels.
|
371
|
array(
|
372
|
'rel' => 'h/../../j/./k',
|
373
|
'base' => 'http://www/a/b/c/',
|
374
|
'expected' => 'http://www/a/b/j/k',
|
375
|
),
|
376
|
array(
|
377
|
'rel' => 'h/../../j/./k',
|
378
|
'base' => 'http://www/a/b/c/d',
|
379
|
'expected' => 'http://www/a/b/j/k',
|
380
|
),
|
381
|
array(
|
382
|
'rel' => '../../../',
|
383
|
'base' => 'http://www/a/b/c/',
|
384
|
'expected' => 'http://www/',
|
385
|
),
|
386
|
array(
|
387
|
'rel' => 'h/j/k/../../',
|
388
|
'base' => 'http://www/a/b/c/',
|
389
|
'expected' => 'http://www/a/b/c/h',
|
390
|
),
|
391
|
);
|
392
|
|
393
|
foreach ($test_urls as $test_url) {
|
394
|
$result_url = http_request_create_absolute_url($test_url['rel'], $test_url['base']);
|
395
|
$this->assertEqual($test_url['expected'], $result_url, format_string('Creating an absolute URL from base @base and rel @rel resulted into @expected (actual: @actual).', array(
|
396
|
'@actual' => var_export($result_url, TRUE),
|
397
|
'@expected' => var_export($test_url['expected'], TRUE),
|
398
|
'@rel' => var_export($test_url['rel'], TRUE),
|
399
|
'@base' => var_export($test_url['base'], TRUE),
|
400
|
)));
|
401
|
}
|
402
|
|
403
|
}
|
404
|
|
405
|
}
|