aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2016-01-04 10:45:54 +0100
committerArthurHoaro <arthur@hoa.ro>2016-01-11 21:19:31 +0100
commit1557cefbd76257ceb830f65806831b490faf0acc (patch)
tree787f6d8fdabe8ea2fc0c37b61d616e667cdfbda5 /tests
parentc0a50f3663e207d5df007e0fa321219c1b32d6ea (diff)
downloadShaarli-1557cefbd76257ceb830f65806831b490faf0acc.tar.gz
Shaarli-1557cefbd76257ceb830f65806831b490faf0acc.tar.zst
Shaarli-1557cefbd76257ceb830f65806831b490faf0acc.zip
Fixes #410 - Retrieve title fails in multiple cases
* `get_http_url()` renamed to `get_http_response()`. * Use the same HTTP context to retrieve response headers and content. * Follow HTTP 301 and 302 redirections to retrieve the title (default max 3 redirections). * Add `LinkUtils` to extract titles and charset. * Try to retrieve charset from HTTP headers first (new), then HTML content. * Use mb_string to re-encode title if necessary.
Diffstat (limited to 'tests')
-rw-r--r--tests/HttpUtils/GetHttpUrlTest.php26
-rw-r--r--tests/LinkUtilsTest.php85
-rw-r--r--tests/Url/UrlTest.php18
3 files changed, 116 insertions, 13 deletions
diff --git a/tests/HttpUtils/GetHttpUrlTest.php b/tests/HttpUtils/GetHttpUrlTest.php
index 76092b80..fd293505 100644
--- a/tests/HttpUtils/GetHttpUrlTest.php
+++ b/tests/HttpUtils/GetHttpUrlTest.php
@@ -6,7 +6,7 @@
6require_once 'application/HttpUtils.php'; 6require_once 'application/HttpUtils.php';
7 7
8/** 8/**
9 * Unitary tests for get_http_url() 9 * Unitary tests for get_http_response()
10 */ 10 */
11class GetHttpUrlTest extends PHPUnit_Framework_TestCase 11class GetHttpUrlTest extends PHPUnit_Framework_TestCase
12{ 12{
@@ -15,12 +15,15 @@ class GetHttpUrlTest extends PHPUnit_Framework_TestCase
15 */ 15 */
16 public function testGetInvalidLocalUrl() 16 public function testGetInvalidLocalUrl()
17 { 17 {
18 list($headers, $content) = get_http_url('/non/existent', 1); 18 // Local
19 $this->assertEquals('HTTP Error', $headers[0]); 19 list($headers, $content) = get_http_response('/non/existent', 1);
20 $this->assertRegexp( 20 $this->assertEquals('Invalid HTTP Url', $headers[0]);
21 '/failed to open stream: No such file or directory/', 21 $this->assertFalse($content);
22 $content 22
23 ); 23 // Non HTTP
24 list($headers, $content) = get_http_response('ftp://save.tld/mysave', 1);
25 $this->assertEquals('Invalid HTTP Url', $headers[0]);
26 $this->assertFalse($content);
24 } 27 }
25 28
26 /** 29 /**
@@ -28,11 +31,8 @@ class GetHttpUrlTest extends PHPUnit_Framework_TestCase
28 */ 31 */
29 public function testGetInvalidRemoteUrl() 32 public function testGetInvalidRemoteUrl()
30 { 33 {
31 list($headers, $content) = get_http_url('http://non.existent', 1); 34 list($headers, $content) = @get_http_response('http://non.existent', 1);
32 $this->assertEquals('HTTP Error', $headers[0]); 35 $this->assertFalse($headers);
33 $this->assertRegexp( 36 $this->assertFalse($content);
34 '/Name or service not known/',
35 $content
36 );
37 } 37 }
38} 38}
diff --git a/tests/LinkUtilsTest.php b/tests/LinkUtilsTest.php
new file mode 100644
index 00000000..c2257590
--- /dev/null
+++ b/tests/LinkUtilsTest.php
@@ -0,0 +1,85 @@
1<?php
2
3require_once 'application/LinkUtils.php';
4
5/**
6* Class LinkUtilsTest.
7*/
8class LinkUtilsTest extends PHPUnit_Framework_TestCase
9{
10 /**
11 * Test html_extract_title() when the title is found.
12 */
13 public function testHtmlExtractExistentTitle()
14 {
15 $title = 'Read me please.';
16 $html = '<html><meta>stuff</meta><title>'. $title .'</title></html>';
17 $this->assertEquals($title, html_extract_title($html));
18 }
19
20 /**
21 * Test html_extract_title() when the title is not found.
22 */
23 public function testHtmlExtractNonExistentTitle()
24 {
25 $html = '<html><meta>stuff</meta></html>';
26 $this->assertFalse(html_extract_title($html));
27 }
28
29 /**
30 * Test get_charset() with all priorities.
31 */
32 public function testGetCharset()
33 {
34 $headers = array('Content-Type' => 'text/html; charset=Headers');
35 $html = '<html><meta>stuff</meta><meta charset="Html"/></html>';
36 $default = 'default';
37 $this->assertEquals('headers', get_charset($headers, $html, $default));
38 $this->assertEquals('html', get_charset(array(), $html, $default));
39 $this->assertEquals($default, get_charset(array(), '', $default));
40 $this->assertEquals('utf-8', get_charset(array(), ''));
41 }
42
43 /**
44 * Test headers_extract_charset() when the charset is found.
45 */
46 public function testHeadersExtractExistentCharset()
47 {
48 $charset = 'x-MacCroatian';
49 $headers = array('Content-Type' => 'text/html; charset='. $charset);
50 $this->assertEquals(strtolower($charset), headers_extract_charset($headers));
51 }
52
53 /**
54 * Test headers_extract_charset() when the charset is not found.
55 */
56 public function testHeadersExtractNonExistentCharset()
57 {
58 $headers = array();
59 $this->assertFalse(headers_extract_charset($headers));
60
61 $headers = array('Content-Type' => 'text/html');
62 $this->assertFalse(headers_extract_charset($headers));
63 }
64
65 /**
66 * Test html_extract_charset() when the charset is found.
67 */
68 public function testHtmlExtractExistentCharset()
69 {
70 $charset = 'x-MacCroatian';
71 $html = '<html><meta>stuff2</meta><meta charset="'. $charset .'"/></html>';
72 $this->assertEquals(strtolower($charset), html_extract_charset($html));
73 }
74
75 /**
76 * Test html_extract_charset() when the charset is not found.
77 */
78 public function testHtmlExtractNonExistentCharset()
79 {
80 $html = '<html><meta>stuff</meta></html>';
81 $this->assertFalse(html_extract_charset($html));
82 $html = '<html><meta>stuff</meta><meta charset=""/></html>';
83 $this->assertFalse(html_extract_charset($html));
84 }
85}
diff --git a/tests/Url/UrlTest.php b/tests/Url/UrlTest.php
index af6daaa4..425327ed 100644
--- a/tests/Url/UrlTest.php
+++ b/tests/Url/UrlTest.php
@@ -156,4 +156,22 @@ class UrlTest extends PHPUnit_Framework_TestCase
156 $this->assertEquals($strOn, add_trailing_slash($strOn)); 156 $this->assertEquals($strOn, add_trailing_slash($strOn));
157 $this->assertEquals($strOn, add_trailing_slash($strOff)); 157 $this->assertEquals($strOn, add_trailing_slash($strOff));
158 } 158 }
159
160 /**
161 * Test valid HTTP url.
162 */
163 function testUrlIsHttp()
164 {
165 $url = new Url(self::$baseUrl);
166 $this->assertTrue($url->isHttp());
167 }
168
169 /**
170 * Test non HTTP url.
171 */
172 function testUrlIsNotHttp()
173 {
174 $url = new Url('ftp://save.tld/mysave');
175 $this->assertFalse($url->isHttp());
176 }
159} 177}