clienttest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Copyright (c) 2015 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 OC\Http\Client;
  9. use GuzzleHttp\Message\Response;
  10. use OCP\IConfig;
  11. /**
  12. * Class ClientTest
  13. */
  14. class ClientTest extends \Test\TestCase {
  15. /** @var \GuzzleHttp\Client */
  16. private $guzzleClient;
  17. /** @var Client */
  18. private $client;
  19. /** @var IConfig */
  20. private $config;
  21. public function setUp() {
  22. parent::setUp();
  23. $this->config = $this->getMock('\OCP\IConfig');
  24. $this->guzzleClient = $this->getMockBuilder('\GuzzleHttp\Client')
  25. ->disableOriginalConstructor()
  26. ->getMock();
  27. $certificateManager = $this->getMock('\OCP\ICertificateManager');
  28. $this->client = new Client(
  29. $this->config,
  30. $certificateManager,
  31. $this->guzzleClient
  32. );
  33. }
  34. public function testGetProxyUri() {
  35. $this->config
  36. ->expects($this->at(0))
  37. ->method('getSystemValue')
  38. ->with('proxy', null)
  39. ->willReturn(null);
  40. $this->config
  41. ->expects($this->at(1))
  42. ->method('getSystemValue')
  43. ->with('proxyuserpwd', null)
  44. ->willReturn(null);
  45. $this->assertSame('', \Test_Helper::invokePrivate($this->client, 'getProxyUri'));
  46. }
  47. public function testGetProxyUriProxyHostEmptyPassword() {
  48. $this->config
  49. ->expects($this->at(0))
  50. ->method('getSystemValue')
  51. ->with('proxy', null)
  52. ->willReturn('foo');
  53. $this->config
  54. ->expects($this->at(1))
  55. ->method('getSystemValue')
  56. ->with('proxyuserpwd', null)
  57. ->willReturn(null);
  58. $this->assertSame('foo', \Test_Helper::invokePrivate($this->client, 'getProxyUri'));
  59. }
  60. public function testGetProxyUriProxyHostWithPassword() {
  61. $this->config
  62. ->expects($this->at(0))
  63. ->method('getSystemValue')
  64. ->with('proxy', null)
  65. ->willReturn('foo');
  66. $this->config
  67. ->expects($this->at(1))
  68. ->method('getSystemValue')
  69. ->with('proxyuserpwd', null)
  70. ->willReturn('username:password');
  71. $this->assertSame('username:password@foo', \Test_Helper::invokePrivate($this->client, 'getProxyUri'));
  72. }
  73. public function testGet() {
  74. $this->guzzleClient->method('get')
  75. ->willReturn(new Response(1337));
  76. $this->assertEquals(1337, $this->client->get('http://localhost/', [])->getStatusCode());
  77. }
  78. public function testPost() {
  79. $this->guzzleClient->method('post')
  80. ->willReturn(new Response(1337));
  81. $this->assertEquals(1337, $this->client->post('http://localhost/', [])->getStatusCode());
  82. }
  83. public function testPut() {
  84. $this->guzzleClient->method('put')
  85. ->willReturn(new Response(1337));
  86. $this->assertEquals(1337, $this->client->put('http://localhost/', [])->getStatusCode());
  87. }
  88. public function testDelete() {
  89. $this->guzzleClient->method('delete')
  90. ->willReturn(new Response(1337));
  91. $this->assertEquals(1337, $this->client->delete('http://localhost/', [])->getStatusCode());
  92. }
  93. public function testOptions() {
  94. $this->guzzleClient->method('options')
  95. ->willReturn(new Response(1337));
  96. $this->assertEquals(1337, $this->client->options('http://localhost/', [])->getStatusCode());
  97. }
  98. }