httphelper.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 \Test\TestCase {
  9. /** @var \OCP\IConfig*/
  10. private $config;
  11. /** @var \OC\HTTPHelper */
  12. private $httpHelperMock;
  13. /** @var \OCP\Http\Client\IClientService */
  14. private $clientService;
  15. protected function setUp() {
  16. parent::setUp();
  17. $this->config = $this->getMockBuilder('\OCP\IConfig')
  18. ->disableOriginalConstructor()->getMock();
  19. $this->clientService = $this->getMock('\OCP\Http\Client\IClientService');
  20. $this->httpHelperMock = $this->getMockBuilder('\OC\HTTPHelper')
  21. ->setConstructorArgs(array($this->config, $this->clientService))
  22. ->setMethods(array('getHeaders'))
  23. ->getMock();
  24. }
  25. public function isHttpTestData() {
  26. return array(
  27. array('http://wwww.owncloud.org/enterprise/', true),
  28. array('https://wwww.owncloud.org/enterprise/', true),
  29. array('HTTPS://WWW.OWNCLOUD.ORG', true),
  30. array('HTTP://WWW.OWNCLOUD.ORG', true),
  31. array('FILE://WWW.OWNCLOUD.ORG', false),
  32. array('file://www.owncloud.org', false),
  33. array('FTP://WWW.OWNCLOUD.ORG', false),
  34. array('ftp://www.owncloud.org', false),
  35. );
  36. }
  37. /**
  38. * @dataProvider isHttpTestData
  39. */
  40. public function testIsHTTP($url, $expected) {
  41. $this->assertSame($expected, $this->httpHelperMock->isHTTPURL($url));
  42. }
  43. public function testPostSuccess() {
  44. $client = $this->getMockBuilder('\OCP\Http\Client\IClient')
  45. ->disableOriginalConstructor()->getMock();
  46. $this->clientService
  47. ->expects($this->once())
  48. ->method('newClient')
  49. ->will($this->returnValue($client));
  50. $response = $this->getMockBuilder('\OCP\Http\Client\IResponse')
  51. ->disableOriginalConstructor()->getMock();
  52. $client
  53. ->expects($this->once())
  54. ->method('post')
  55. ->with(
  56. 'https://owncloud.org',
  57. [
  58. 'body' => [
  59. 'Foo' => 'Bar',
  60. ],
  61. 'connect_timeout' => 10,
  62. ]
  63. )
  64. ->will($this->returnValue($response));
  65. $response
  66. ->expects($this->once())
  67. ->method('getBody')
  68. ->will($this->returnValue('Body of the requested page'));
  69. $response = $this->httpHelperMock->post('https://owncloud.org', ['Foo' => 'Bar']);
  70. $expected = [
  71. 'success' => true,
  72. 'result' => 'Body of the requested page'
  73. ];
  74. $this->assertSame($expected, $response);
  75. }
  76. public function testPostException() {
  77. $client = $this->getMockBuilder('\OCP\Http\Client\IClient')
  78. ->disableOriginalConstructor()->getMock();
  79. $this->clientService
  80. ->expects($this->once())
  81. ->method('newClient')
  82. ->will($this->returnValue($client));
  83. $client
  84. ->expects($this->once())
  85. ->method('post')
  86. ->with(
  87. 'https://owncloud.org',
  88. [
  89. 'body' => [
  90. 'Foo' => 'Bar',
  91. ],
  92. 'connect_timeout' => 10,
  93. ]
  94. )
  95. ->will($this->throwException(new \Exception('Something failed')));
  96. $response = $this->httpHelperMock->post('https://owncloud.org', ['Foo' => 'Bar']);
  97. $expected = [
  98. 'success' => false,
  99. 'result' => 'Something failed'
  100. ];
  101. $this->assertSame($expected, $response);
  102. }
  103. }