Skip to content
This repository was archived by the owner on Feb 5, 2024. It is now read-only.

Commit 903f595

Browse files
authored
Merge pull request #3 from robsonvn/revert-2-revert-1-master
Revert "Revert "Add support for SSL in StreamClient""
2 parents 18e5bdd + a543c3e commit 903f595

File tree

10 files changed

+46
-7
lines changed

10 files changed

+46
-7
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ php:
1111
- 7.1
1212

1313
before_script:
14-
- curl -X PUT localhost:5984/doctrine_test_database
1514
- composer install
1615

1716
script:

lib/Doctrine/CouchDB/CouchDBClient.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ static public function create(array $options)
122122
'path' => null,
123123
'logging' => false,
124124
'timeout' => 10,
125+
'headers' => array(),
125126
);
126127
$options = array_merge($defaults, $options);
127128

@@ -139,7 +140,8 @@ static public function create(array $options)
139140
$options['ip'],
140141
$options['ssl'],
141142
$options['path'],
142-
$options['timeout']
143+
$options['timeout'],
144+
$options['headers']
143145
);
144146
if ($options['logging'] === true) {
145147
$connection = new HTTP\LoggingClient($connection);

lib/Doctrine/CouchDB/HTTP/AbstractHTTPClient.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ abstract class AbstractHTTPClient implements Client
3030
'username' => null,
3131
'password' => null,
3232
'path' => null,
33+
'headers' => array(),
3334
);
3435

3536
/**
@@ -45,9 +46,11 @@ abstract class AbstractHTTPClient implements Client
4546
* @param string $ip
4647
* @param bool $ssl
4748
* @param string $path
49+
* @param int $timeout
50+
* @param array $headers
4851
* @return \Doctrine\CouchDB\HTTP\AbstractHTTPClient
4952
*/
50-
public function __construct($host = 'localhost', $port = 5984, $username = null, $password = null, $ip = null , $ssl = false, $path = null, $timeout = 10)
53+
public function __construct($host = 'localhost', $port = 5984, $username = null, $password = null, $ip = null , $ssl = false, $path = null, $timeout = 10, array $headers = array())
5154
{
5255
$this->options['host'] = (string) $host;
5356
$this->options['port'] = (int) $port;
@@ -56,6 +59,7 @@ public function __construct($host = 'localhost', $port = 5984, $username = null,
5659
$this->options['password'] = $password;
5760
$this->options['path'] = $path;
5861
$this->options['timeout'] = (float) $timeout;
62+
$this->options['headers'] = $headers;
5963

6064
if ($ip === null) {
6165
$this->options['ip'] = gethostbyname($this->options['host']);
@@ -86,6 +90,7 @@ public function setOption( $option, $value )
8690
break;
8791

8892
case 'http-log':
93+
case 'headers':
8994
case 'password':
9095
case 'username':
9196
$this->options[$option] = $value;

lib/Doctrine/CouchDB/HTTP/MultipartParserAndSender.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public function __construct(
5050
$sourceOptions['ip'],
5151
$sourceOptions['ssl'],
5252
$sourceOptions['path'],
53-
$sourceOptions['timeout']
53+
$sourceOptions['timeout'],
54+
$sourceOptions['headers']
5455
);
5556

5657
$targetOptions = $target->getOptions();
@@ -62,7 +63,8 @@ public function __construct(
6263
$targetOptions['ip'],
6364
$targetOptions['ssl'],
6465
$targetOptions['path'],
65-
$targetOptions['timeout']
66+
$targetOptions['timeout'],
67+
$sourceOptions['headers']
6668
);
6769
}
6870

lib/Doctrine/CouchDB/HTTP/SocketClient.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ protected function buildRequest(
159159
// available in the locale net.
160160
$request .= "Connection: " . ($this->options['keep-alive'] ? 'Keep-Alive' : 'Close') . "\r\n";
161161

162+
if ($this->options['headers']) {
163+
$headers = array_merge($this->options['headers'], $headers);
164+
}
162165
if (!isset($headers['Content-Type'])) {
163166
$headers['Content-Type'] = 'application/json';
164167
}

lib/Doctrine/CouchDB/HTTP/StreamClient.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ protected function checkConnection($method, $path, $data, $headers)
8181
if ($this->options['username']) {
8282
$basicAuth .= "{$this->options['username']}:{$this->options['password']}@";
8383
}
84+
if ($this->options['headers']) {
85+
$headers = array_merge($this->options['headers'], $headers);
86+
}
8487
if (!isset($headers['Content-Type'])) {
8588
$headers['Content-Type'] = 'application/json';
8689
}

tests/Doctrine/Tests/CouchDB/CouchDBClientTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function testCreateClientFromUrl()
3939
'timeout' => 10,
4040
'keep-alive' => true,
4141
'path' => null,
42+
'headers' => array(),
4243
),
4344
$client->getHttpClient()->getOptions()
4445
);
@@ -60,11 +61,24 @@ public function testCreateClientFromUrlWithPath()
6061
'timeout' => 10,
6162
'keep-alive' => true,
6263
'path' => 'baz/qux',
64+
'headers' => array(),
6365
),
6466
$client->getHttpClient()->getOptions()
6567
);
6668
}
6769

70+
public function testCreateClientWithDefaultHeaders()
71+
{
72+
$client = CouchDBClient::create(array('dbname' => 'test', 'headers' => array('X-Test' => 'test')));
73+
$http_client = $client->getHttpClient();
74+
$connection_options = $http_client->getOptions();
75+
$this->assertSame(array('X-Test' => 'test'), $connection_options['headers']);
76+
77+
$http_client->setOption('headers', array('X-Test-New' => 'new'));
78+
$connection_options = $http_client->getOptions();
79+
$this->assertSame(array('X-Test-New' => 'new'), $connection_options['headers']);
80+
}
81+
6882
public function testCreateClientWithLogging()
6983
{
7084
$client = CouchDBClient::create(array('dbname' => 'test', 'logging' => true));

tests/Doctrine/Tests/CouchDB/CouchDBFunctionalTestCase.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ abstract class CouchDBFunctionalTestCase extends \PHPUnit_Framework_TestCase
99
{
1010
private $httpClient = null;
1111

12-
/**
12+
protected function tearDown() {
13+
parent::tearDown();
14+
$this->createCouchDBClient()->deleteDatabase($this->getTestDatabase());
15+
}
16+
17+
/**
1318
* @return \Doctrine\CouchDB\HTTP\Client
1419
*/
1520
public function getHttpClient()

tests/Doctrine/Tests/CouchDB/Functional/CouchDBClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class CouchDBClientTest extends \Doctrine\Tests\CouchDB\CouchDBFunctionalTestCas
1414
public function setUp()
1515
{
1616
$this->couchClient = $this->createCouchDBClient();
17+
$this->couchClient->createDatabase($this->getTestDatabase());
1718
}
1819

1920
public function deleteAndCreateDatabase($databaseName){
@@ -79,7 +80,6 @@ public function testDropMultipleTimesSkips()
7980
*/
8081
public function testCreateDuplicateDatabaseThrowsException()
8182
{
82-
$this->couchClient->createDatabase($this->getTestDatabase());
8383
$this->setExpectedException('Doctrine\CouchDB\HTTP\HTTPException', 'HTTP Error with status 412 occurred while requesting /'.$this->getTestDatabase().'. Error: file_exists The database could not be created, the file already exists.');
8484
$this->couchClient->createDatabase($this->getTestDatabase());
8585
}

tests/Doctrine/Tests/CouchDB/Functional/HTTP/MultipartParserAndSenderTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public function setUp()
5252

5353
}
5454

55+
public function tearDown()
56+
{
57+
parent::tearDown();
58+
$this->createCouchDBClient()->deleteDatabase($this->getTestDatabase() . '_multipart_copy');
59+
}
60+
5561
public function testRequestThrowsHTTPExceptionOnEmptyStatus()
5662
{
5763
$this->setExpectedException(

0 commit comments

Comments
 (0)