httphelper.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class TestHTTPHelper extends \PHPUnit_Framework_TestCase {
  9. /** @var \OC\AllConfig*/
  10. private $config;
  11. /** @var \OC\HTTPHelper */
  12. private $httpHelperMock;
  13. function setUp() {
  14. $this->config = $this->getMockBuilder('\OC\AllConfig')
  15. ->disableOriginalConstructor()->getMock();
  16. $this->httpHelperMock = $this->getMockBuilder('\OC\HTTPHelper')
  17. ->setConstructorArgs(array($this->config))
  18. ->setMethods(array('getHeaders'))
  19. ->getMock();
  20. }
  21. public function testIsHTTPProvider() {
  22. return array(
  23. array('http://wwww.owncloud.org/enterprise/', true),
  24. array('https://wwww.owncloud.org/enterprise/', true),
  25. array('HTTPS://WWW.OWNCLOUD.ORG', true),
  26. array('HTTP://WWW.OWNCLOUD.ORG', true),
  27. array('FILE://WWW.OWNCLOUD.ORG', false),
  28. array('file://www.owncloud.org', false),
  29. array('FTP://WWW.OWNCLOUD.ORG', false),
  30. array('ftp://www.owncloud.org', false),
  31. );
  32. }
  33. /**
  34. * Note: Not using a dataprovider because onConsecutiveCalls expects not
  35. * an array but the function arguments directly
  36. */
  37. public function testGetFinalLocationOfURLValid() {
  38. $url = 'https://www.owncloud.org/enterprise/';
  39. $expected = 'https://www.owncloud.com/enterprise/';
  40. $this->httpHelperMock->expects($this->any())
  41. ->method('getHeaders')
  42. ->will($this->onConsecutiveCalls(
  43. array('Location' => 'http://www.owncloud.com/enterprise/'),
  44. array('Location' => 'https://www.owncloud.com/enterprise/')
  45. ));
  46. $result = $this->httpHelperMock->getFinalLocationOfURL($url);
  47. $this->assertSame($expected, $result);
  48. }
  49. /**
  50. * Note: Not using a dataprovider because onConsecutiveCalls expects not
  51. * an array but the function arguments directly
  52. */
  53. public function testGetFinalLocationOfURLInvalid() {
  54. $url = 'https://www.owncloud.org/enterprise/';
  55. $expected = 'http://www.owncloud.com/enterprise/';
  56. $this->httpHelperMock->expects($this->any())
  57. ->method('getHeaders')
  58. ->will($this->onConsecutiveCalls(
  59. array('Location' => 'http://www.owncloud.com/enterprise/'),
  60. array('Location' => 'file://etc/passwd'),
  61. array('Location' => 'http://www.example.com/')
  62. ));
  63. $result = $this->httpHelperMock->getFinalLocationOfURL($url);
  64. $this->assertSame($expected, $result);
  65. }
  66. /**
  67. * @expectedException \Exception
  68. * @expectedExceptionMessage URL must begin with HTTPS or HTTP.
  69. */
  70. public function testGetFinalLocationOfURLException() {
  71. $this->httpHelperMock->getFinalLocationOfURL('file://etc/passwd');
  72. }
  73. /**
  74. * @dataProvider testIsHTTPProvider
  75. */
  76. public function testIsHTTP($url, $expected) {
  77. $this->assertSame($expected, $this->httpHelperMock->isHTTPURL($url));
  78. }
  79. }