aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/http/UrlUtils
diff options
context:
space:
mode:
authorVirtualTam <virtualtam@flibidi.net>2018-12-03 00:34:53 +0100
committerVirtualTam <virtualtam@flibidi.net>2019-01-12 22:47:48 +0100
commit51753e403fa69c0ce124ede27d300477e3e799ca (patch)
tree0afe2a84598648b49cc53bb3e8640569ac370240 /tests/http/UrlUtils
parentfb1b182fbf0ee5afed586f77eec84d7a906831ef (diff)
downloadShaarli-51753e403fa69c0ce124ede27d300477e3e799ca.tar.gz
Shaarli-51753e403fa69c0ce124ede27d300477e3e799ca.tar.zst
Shaarli-51753e403fa69c0ce124ede27d300477e3e799ca.zip
namespacing: move HTTP utilities along \Shaarli\Http\ classes
Signed-off-by: VirtualTam <virtualtam@flibidi.net>
Diffstat (limited to 'tests/http/UrlUtils')
-rw-r--r--tests/http/UrlUtils/CleanupUrlTest.php111
-rw-r--r--tests/http/UrlUtils/GetUrlSchemeTest.php32
-rw-r--r--tests/http/UrlUtils/UnparseUrlTest.php32
-rw-r--r--tests/http/UrlUtils/WhitelistProtocolsTest.php63
4 files changed, 238 insertions, 0 deletions
diff --git a/tests/http/UrlUtils/CleanupUrlTest.php b/tests/http/UrlUtils/CleanupUrlTest.php
new file mode 100644
index 00000000..6c4d124b
--- /dev/null
+++ b/tests/http/UrlUtils/CleanupUrlTest.php
@@ -0,0 +1,111 @@
1<?php
2/**
3 * Unitary tests for cleanup_url()
4 */
5
6namespace Shaarli\Http;
7
8require_once 'application/http/UrlUtils.php';
9
10class CleanupUrlTest extends \PHPUnit\Framework\TestCase
11{
12 /**
13 * @var string reference URL
14 */
15 protected $ref = 'http://domain.tld:3000';
16
17
18 /**
19 * Clean empty URL
20 */
21 public function testCleanupUrlEmpty()
22 {
23 $this->assertEquals('', cleanup_url(''));
24 }
25
26 /**
27 * Clean an already cleaned URL
28 */
29 public function testCleanupUrlAlreadyClean()
30 {
31 $this->assertEquals($this->ref, cleanup_url($this->ref));
32 $this->ref2 = $this->ref.'/path/to/dir/';
33 $this->assertEquals($this->ref2, cleanup_url($this->ref2));
34 }
35
36 /**
37 * Clean URL fragments
38 */
39 public function testCleanupUrlFragment()
40 {
41 $this->assertEquals($this->ref, cleanup_url($this->ref.'#tk.rss_all'));
42 $this->assertEquals($this->ref, cleanup_url($this->ref.'#xtor=RSS-'));
43 $this->assertEquals($this->ref, cleanup_url($this->ref.'#xtor=RSS-U3ht0tkc4b'));
44 }
45
46 /**
47 * Clean URL query - single annoying parameter
48 */
49 public function testCleanupUrlQuerySingle()
50 {
51 $this->assertEquals($this->ref, cleanup_url($this->ref.'?action_object_map=junk'));
52 $this->assertEquals($this->ref, cleanup_url($this->ref.'?action_ref_map=Cr4p!'));
53 $this->assertEquals($this->ref, cleanup_url($this->ref.'?action_type_map=g4R84g3'));
54
55 $this->assertEquals($this->ref, cleanup_url($this->ref.'?fb_stuff=v41u3'));
56 $this->assertEquals($this->ref, cleanup_url($this->ref.'?fb=71m3w4573'));
57
58 $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_campaign=zomg'));
59 $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_medium=numnum'));
60 $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_source=c0d3'));
61 $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_term=1n4l'));
62
63 $this->assertEquals($this->ref, cleanup_url($this->ref.'?xtor=some-url'));
64
65 $this->assertEquals($this->ref, cleanup_url($this->ref.'?campaign_name=junk'));
66 $this->assertEquals($this->ref, cleanup_url($this->ref.'?campaign_start=junk'));
67 $this->assertEquals($this->ref, cleanup_url($this->ref.'?campaign_item_index=junk'));
68 }
69
70 /**
71 * Clean URL query - multiple annoying parameters
72 */
73 public function testCleanupUrlQueryMultiple()
74 {
75 $this->assertEquals($this->ref, cleanup_url($this->ref.'?xtor=some-url&fb=som3th1ng'));
76
77 $this->assertEquals($this->ref, cleanup_url(
78 $this->ref.'?fb=stuff&utm_campaign=zomg&utm_medium=numnum&utm_source=c0d3'
79 ));
80
81 $this->assertEquals($this->ref, cleanup_url(
82 $this->ref.'?campaign_start=zomg&campaign_name=numnum'
83 ));
84 }
85
86 /**
87 * Clean URL query - multiple annoying parameters and fragment
88 */
89 public function testCleanupUrlQueryFragment()
90 {
91 $this->assertEquals($this->ref, cleanup_url(
92 $this->ref.'?xtor=some-url&fb=som3th1ng#tk.rss_all'
93 ));
94
95 // ditch annoying query params and fragment, keep useful params
96 $this->assertEquals(
97 $this->ref.'?my=stuff&is=kept',
98 cleanup_url(
99 $this->ref.'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#tk.rss_all'
100 )
101 );
102
103 // ditch annoying query params, keep useful params and fragment
104 $this->assertEquals(
105 $this->ref.'?my=stuff&is=kept#again',
106 cleanup_url(
107 $this->ref.'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#again'
108 )
109 );
110 }
111}
diff --git a/tests/http/UrlUtils/GetUrlSchemeTest.php b/tests/http/UrlUtils/GetUrlSchemeTest.php
new file mode 100644
index 00000000..2b97f7be
--- /dev/null
+++ b/tests/http/UrlUtils/GetUrlSchemeTest.php
@@ -0,0 +1,32 @@
1<?php
2/**
3 * Unitary tests for get_url_scheme()
4 */
5
6namespace Shaarli\Http;
7
8require_once 'application/http/UrlUtils.php';
9
10class GetUrlSchemeTest extends \PHPUnit\Framework\TestCase
11{
12 /**
13 * Get empty scheme string for empty UrlUtils
14 */
15 public function testGetUrlSchemeEmpty()
16 {
17 $this->assertEquals('', get_url_scheme(''));
18 }
19
20 /**
21 * Get normal scheme of UrlUtils
22 */
23 public function testGetUrlScheme()
24 {
25 $this->assertEquals('http', get_url_scheme('http://domain.tld:3000'));
26 $this->assertEquals('https', get_url_scheme('https://domain.tld:3000'));
27 $this->assertEquals('http', get_url_scheme('domain.tld'));
28 $this->assertEquals('ssh', get_url_scheme('ssh://domain.tld'));
29 $this->assertEquals('ftp', get_url_scheme('ftp://domain.tld'));
30 $this->assertEquals('git', get_url_scheme('git://domain.tld/push?pull=clone#checkout'));
31 }
32}
diff --git a/tests/http/UrlUtils/UnparseUrlTest.php b/tests/http/UrlUtils/UnparseUrlTest.php
new file mode 100644
index 00000000..040d8c54
--- /dev/null
+++ b/tests/http/UrlUtils/UnparseUrlTest.php
@@ -0,0 +1,32 @@
1<?php
2/**
3 * Unpares UrlUtils's tests
4 */
5
6namespace Shaarli\Http;
7
8require_once 'application/http/UrlUtils.php';
9
10/**
11 * Unitary tests for unparse_url()
12 */
13class UnparseUrlTest extends \PHPUnit\Framework\TestCase
14{
15 /**
16 * Thanks for building nothing
17 */
18 public function testUnparseEmptyArray()
19 {
20 $this->assertEquals('', unparse_url(array()));
21 }
22
23 /**
24 * Rebuild a full-featured URL
25 */
26 public function testUnparseFull()
27 {
28 $ref = 'http://username:password@hostname:9090/path'
29 .'?arg1=value1&arg2=value2#anchor';
30 $this->assertEquals($ref, unparse_url(parse_url($ref)));
31 }
32}
diff --git a/tests/http/UrlUtils/WhitelistProtocolsTest.php b/tests/http/UrlUtils/WhitelistProtocolsTest.php
new file mode 100644
index 00000000..69512dbd
--- /dev/null
+++ b/tests/http/UrlUtils/WhitelistProtocolsTest.php
@@ -0,0 +1,63 @@
1<?php
2
3namespace Shaarli\Http;
4
5require_once 'application/http/UrlUtils.php';
6
7/**
8 * Class WhitelistProtocolsTest
9 *
10 * Test whitelist_protocols() function of UrlUtils.
11 */
12class WhitelistProtocolsTest extends \PHPUnit\Framework\TestCase
13{
14 /**
15 * Test whitelist_protocols() on a note (relative URL).
16 */
17 public function testWhitelistProtocolsRelative()
18 {
19 $whitelist = ['ftp', 'magnet'];
20 $url = '?12443564';
21 $this->assertEquals($url, whitelist_protocols($url, $whitelist));
22 $url = '/path.jpg';
23 $this->assertEquals($url, whitelist_protocols($url, $whitelist));
24 }
25
26 /**
27 * Test whitelist_protocols() on a note (relative URL).
28 */
29 public function testWhitelistProtocolMissing()
30 {
31 $whitelist = ['ftp', 'magnet'];
32 $url = 'test.tld/path/?query=value#hash';
33 $this->assertEquals('http://'. $url, whitelist_protocols($url, $whitelist));
34 }
35
36 /**
37 * Test whitelist_protocols() with allowed protocols.
38 */
39 public function testWhitelistAllowedProtocol()
40 {
41 $whitelist = ['ftp', 'magnet'];
42 $url = 'http://test.tld/path/?query=value#hash';
43 $this->assertEquals($url, whitelist_protocols($url, $whitelist));
44 $url = 'https://test.tld/path/?query=value#hash';
45 $this->assertEquals($url, whitelist_protocols($url, $whitelist));
46 $url = 'ftp://test.tld/path/?query=value#hash';
47 $this->assertEquals($url, whitelist_protocols($url, $whitelist));
48 $url = 'magnet:test.tld/path/?query=value#hash';
49 $this->assertEquals($url, whitelist_protocols($url, $whitelist));
50 }
51
52 /**
53 * Test whitelist_protocols() with allowed protocols.
54 */
55 public function testWhitelistDisallowedProtocol()
56 {
57 $whitelist = ['ftp', 'magnet'];
58 $url = 'javascript:alert("xss");';
59 $this->assertEquals('http://alert("xss");', whitelist_protocols($url, $whitelist));
60 $url = 'other://test.tld/path/?query=value#hash';
61 $this->assertEquals('http://test.tld/path/?query=value#hash', whitelist_protocols($url, $whitelist));
62 }
63}