Skip to content

Commit

Permalink
Merge pull request doctrine#1 from damiankloip/default-headers
Browse files Browse the repository at this point in the history
Allow default headers to be set for clients
  • Loading branch information
damiankloip committed Apr 7, 2017
2 parents 911c017 + 99a8fe3 commit eeec709
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 7 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ php:
- 7.0

before_script:
- curl -X PUT localhost:5984/doctrine_test_database
- composer install

script:
Expand Down
4 changes: 3 additions & 1 deletion lib/Doctrine/CouchDB/CouchDBClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ static public function create(array $options)
'path' => null,
'logging' => false,
'timeout' => 10,
'headers' => array(),
);
$options = array_merge($defaults, $options);

Expand All @@ -139,7 +140,8 @@ static public function create(array $options)
$options['ip'],
$options['ssl'],
$options['path'],
$options['timeout']
$options['timeout'],
$options['headers']
);
if ($options['logging'] === true) {
$connection = new HTTP\LoggingClient($connection);
Expand Down
7 changes: 6 additions & 1 deletion lib/Doctrine/CouchDB/HTTP/AbstractHTTPClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ abstract class AbstractHTTPClient implements Client
'username' => null,
'password' => null,
'path' => null,
'headers' => array(),
);

/**
Expand All @@ -45,9 +46,11 @@ abstract class AbstractHTTPClient implements Client
* @param string $ip
* @param bool $ssl
* @param string $path
* @param int $timeout
* @param array $headers
* @return \Doctrine\CouchDB\HTTP\AbstractHTTPClient
*/
public function __construct($host = 'localhost', $port = 5984, $username = null, $password = null, $ip = null , $ssl = false, $path = null, $timeout = 10)
public function __construct($host = 'localhost', $port = 5984, $username = null, $password = null, $ip = null , $ssl = false, $path = null, $timeout = 10, array $headers = array())
{
$this->options['host'] = (string) $host;
$this->options['port'] = (int) $port;
Expand All @@ -56,6 +59,7 @@ public function __construct($host = 'localhost', $port = 5984, $username = null,
$this->options['password'] = $password;
$this->options['path'] = $path;
$this->options['timeout'] = (float) $timeout;
$this->options['headers'] = $headers;

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

case 'http-log':
case 'headers':
case 'password':
case 'username':
$this->options[$option] = $value;
Expand Down
6 changes: 4 additions & 2 deletions lib/Doctrine/CouchDB/HTTP/MultipartParserAndSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public function __construct(
$sourceOptions['ip'],
$sourceOptions['ssl'],
$sourceOptions['path'],
$sourceOptions['timeout']
$sourceOptions['timeout'],
$sourceOptions['headers']
);

$targetOptions = $target->getOptions();
Expand All @@ -62,7 +63,8 @@ public function __construct(
$targetOptions['ip'],
$targetOptions['ssl'],
$targetOptions['path'],
$targetOptions['timeout']
$targetOptions['timeout'],
$sourceOptions['headers']
);
}

Expand Down
3 changes: 3 additions & 0 deletions lib/Doctrine/CouchDB/HTTP/SocketClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ protected function buildRequest(
// available in the locale net.
$request .= "Connection: " . ($this->options['keep-alive'] ? 'Keep-Alive' : 'Close') . "\r\n";

if ($this->options['headers']) {
$headers = array_merge($this->options['headers'], $headers);
}
if (!isset($headers['Content-Type'])) {
$headers['Content-Type'] = 'application/json';
}
Expand Down
3 changes: 3 additions & 0 deletions lib/Doctrine/CouchDB/HTTP/StreamClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ protected function checkConnection($method, $path, $data, $headers)
if ($this->options['username']) {
$basicAuth .= "{$this->options['username']}:{$this->options['password']}@";
}
if ($this->options['headers']) {
$headers = array_merge($this->options['headers'], $headers);
}
if (!isset($headers['Content-Type'])) {
$headers['Content-Type'] = 'application/json';
}
Expand Down
14 changes: 14 additions & 0 deletions tests/Doctrine/Tests/CouchDB/CouchDBClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function testCreateClientFromUrl()
'timeout' => 10,
'keep-alive' => true,
'path' => null,
'headers' => array(),
),
$client->getHttpClient()->getOptions()
);
Expand All @@ -60,11 +61,24 @@ public function testCreateClientFromUrlWithPath()
'timeout' => 10,
'keep-alive' => true,
'path' => 'baz/qux',
'headers' => array(),
),
$client->getHttpClient()->getOptions()
);
}

public function testCreateClientWithDefaultHeaders()
{
$client = CouchDBClient::create(array('dbname' => 'test', 'headers' => array('X-Test' => 'test')));
$http_client = $client->getHttpClient();
$connection_options = $http_client->getOptions();
$this->assertSame(array('X-Test' => 'test'), $connection_options['headers']);

$http_client->setOption('headers', array('X-Test-New' => 'new'));
$connection_options = $http_client->getOptions();
$this->assertSame(array('X-Test-New' => 'new'), $connection_options['headers']);
}

public function testCreateClientWithLogging()
{
$client = CouchDBClient::create(array('dbname' => 'test', 'logging' => true));
Expand Down
7 changes: 6 additions & 1 deletion tests/Doctrine/Tests/CouchDB/CouchDBFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ abstract class CouchDBFunctionalTestCase extends \PHPUnit_Framework_TestCase
{
private $httpClient = null;

/**
protected function tearDown() {
parent::tearDown();
$this->createCouchDBClient()->deleteDatabase($this->getTestDatabase());
}

/**
* @return \Doctrine\CouchDB\HTTP\Client
*/
public function getHttpClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class CouchDBClientTest extends \Doctrine\Tests\CouchDB\CouchDBFunctionalTestCas
public function setUp()
{
$this->couchClient = $this->createCouchDBClient();
$this->couchClient->createDatabase($this->getTestDatabase());
}

public function testGetUuids()
Expand Down Expand Up @@ -73,7 +74,6 @@ public function testDropMultipleTimesSkips()
*/
public function testCreateDuplicateDatabaseThrowsException()
{
$this->couchClient->createDatabase($this->getTestDatabase());
$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.');
$this->couchClient->createDatabase($this->getTestDatabase());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ public function setUp()

}

public function tearDown()
{
parent::tearDown();
$this->createCouchDBClient()->deleteDatabase($this->getTestDatabase() . '_multipart_copy');
}

public function testRequestThrowsHTTPExceptionOnEmptyStatus()
{
$this->setExpectedException(
Expand Down

0 comments on commit eeec709

Please sign in to comment.