summaryrefslogtreecommitdiffstats
path: root/tests/lib/httphelper.php
blob: 34dee35fe0295db1bcb5357251f14a8e21dcb4a5 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/**
 * Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

class TestHTTPHelper extends \Test\TestCase {

	/** @var \OC\AllConfig*/
	private $config;
	/** @var \OC\HTTPHelper */
	private $httpHelperMock;

	protected function setUp() {
		parent::setUp();

		$this->config = $this->getMockBuilder('\OC\AllConfig')
			->disableOriginalConstructor()->getMock();
		$this->httpHelperMock = $this->getMockBuilder('\OC\HTTPHelper')
			->setConstructorArgs(array($this->config))
			->setMethods(array('getHeaders'))
			->getMock();
	}

	public function testIsHTTPProvider() {
		return array(
			array('http://wwww.owncloud.org/enterprise/', true),
			array('https://wwww.owncloud.org/enterprise/', true),
			array('HTTPS://WWW.OWNCLOUD.ORG', true),
			array('HTTP://WWW.OWNCLOUD.ORG', true),
			array('FILE://WWW.OWNCLOUD.ORG', false),
			array('file://www.owncloud.org', false),
			array('FTP://WWW.OWNCLOUD.ORG', false),
			array('ftp://www.owncloud.org', false),
		);
	}

	/**
	 * Note: Not using a dataprovider because onConsecutiveCalls expects not
	 * an array but the function arguments directly
	 */
	public function testGetFinalLocationOfURLValid() {
		$url = 'https://www.owncloud.org/enterprise/';
		$expected = 'https://www.owncloud.com/enterprise/';
		$this->httpHelperMock->expects($this->any())
			->method('getHeaders')
			->will($this->onConsecutiveCalls(
				array('Location' => 'http://www.owncloud.com/enterprise/'),
				array('Location' => 'https://www.owncloud.com/enterprise/')
			));
		$result = $this->httpHelperMock->getFinalLocationOfURL($url);
		$this->assertSame($expected, $result);
	}

	/**
	 * Note: Not using a dataprovider because onConsecutiveCalls expects not
	 * an array but the function arguments directly
	 */
	public function testGetFinalLocationOfURLInvalid() {
		$url = 'https://www.owncloud.org/enterprise/';
		$expected = 'http://www.owncloud.com/enterprise/';
		$this->httpHelperMock->expects($this->any())
			->method('getHeaders')
			->will($this->onConsecutiveCalls(
				array('Location' => 'http://www.owncloud.com/enterprise/'),
				array('Location' => 'file://etc/passwd'),
				array('Location' => 'http://www.example.com/')
			));
		$result = $this->httpHelperMock->getFinalLocationOfURL($url);
		$this->assertSame($expected, $result);
	}

	/**
	 * @expectedException \Exception
	 * @expectedExceptionMessage URL must begin with HTTPS or HTTP.
	 */
	public function testGetFinalLocationOfURLException() {
		$this->httpHelperMock->getFinalLocationOfURL('file://etc/passwd');
	}

	/**
	 * @dataProvider testIsHTTPProvider
	 */
	public function testIsHTTP($url, $expected) {
			$this->assertSame($expected, $this->httpHelperMock->isHTTPURL($url));
	}

}