You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HTTPHelperTest.php 3.1KB

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