summaryrefslogtreecommitdiff
path: root/tests/UrlHelperTest.php
blob: fe4eb5db208edf7e35157ce9a37676439cf8b78b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php

use PHPUnit\Framework\TestCase;

final class UrlHelperTest extends TestCase {
	public function test_rewrite_relative(): void {
		// protocol-neutral URL
		$this->assertEquals(
			'https://example.com/example.html',
			UrlHelper::rewrite_relative('http://example.com/example/', '//example.com/example.html')
		);

		// magnet allowed because it's a href attribute
		$this->assertEquals(
			'magnet:?xt=urn:btih:...',
			UrlHelper::rewrite_relative('http://example.com/example/',
				'magnet:?xt=urn:btih:...',
				"a", "href", "")
		);

		// disallowed magnet
		$this->assertEquals(
			'http://example.com?xt=urn:btih:...',
			UrlHelper::rewrite_relative('http://example.com/example/',
				'magnet:?xt=urn:btih:...')
		);

		$this->assertEquals(
			'https://apod.nasa.gov/apod/image/2203/Road2Stars_EsoHoralek_1080.jpg',
			UrlHelper::rewrite_relative('https://apod.nasa.gov/apod/ap220315.html', 'image/2203/Road2Stars_EsoHoralek_1080.jpg')
		);

		$this->assertEquals(
			'https://apod.nasa.gov/apod/image/2203/Road2Stars_EsoHoralek_1080.jpg',
			UrlHelper::rewrite_relative('https://apod.nasa.gov/apod/ap220315.html', './image/2203/Road2Stars_EsoHoralek_1080.jpg')
		);

		$this->assertEquals(
			'http://example.com/test/url',
			UrlHelper::rewrite_relative('http://example.com/test/url', '')
		);

		$this->assertEquals(
			'http://www.example.com/test',
			UrlHelper::rewrite_relative('http://www.example2.com ', 'http://www.example.com/test')
		);

		$this->assertEquals(
			'http://www.example.com/test',
			UrlHelper::rewrite_relative('http://www.example.com/test2 ', 'http://www.example.com/test')
		);

	}
}