HTTPHelperTest.php 3.1 KB

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