Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/7329' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Mar 18, 2015
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Client implements Stdlib\DispatchableInterface
* @const string Supported HTTP Authentication methods
*/
const AUTH_BASIC = 'basic';
const AUTH_DIGEST = 'digest'; // not implemented yet
const AUTH_DIGEST = 'digest';

/**
* @const string POST data encoding methods
Expand Down Expand Up @@ -1140,7 +1140,12 @@ protected function prepareHeaders($body, $uri)
}
break;
case self::AUTH_DIGEST :
throw new Exception\RuntimeException("The digest authentication is not implemented yet");
if (!$this->adapter instanceof Client\Adapter\Curl) {
throw new Exception\RuntimeException("The digest authentication is only available for curl adapters (Zend\\Http\\Client\\Adapter\\Curl)");
}

$this->adapter->setCurlOption(CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
$this->adapter->setCurlOption(CURLOPT_USERPWD, $this->auth['user'] . ':' . $this->auth['password']);
}
}

Expand Down
24 changes: 23 additions & 1 deletion test/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Zend\Http\Client\Adapter\Test;
use ZendTest\Http\TestAsset\ExtendedClient;


class ClientTest extends \PHPUnit_Framework_TestCase
{
public function testIfCookiesAreSticky()
Expand Down Expand Up @@ -376,6 +375,29 @@ public function testPrepareHeadersCreateRightHttpField()
$this->assertArrayHasKey('Content-Length', $headers);
}

public function testPrepareHeadersCurlDigestAuthentication()
{
$body = json_encode(array('foofoo'=>'barbar'));

$client = new Client();
$prepareHeadersReflection = new \ReflectionMethod($client, 'prepareHeaders');
$prepareHeadersReflection->setAccessible(true);

$request = new Request();
$request->getHeaders()->addHeaderLine('Authorization: Digest');
$request->getHeaders()->addHeaderLine('content-type', 'application/json');
$request->getHeaders()->addHeaderLine('content-length', strlen($body));
$client->setRequest($request);

$this->assertSame($client->getRequest(), $request);

$headers = $prepareHeadersReflection->invoke($client, $body, new Http('http://localhost:5984'));

$this->assertInternalType('array', $headers);
$this->assertArrayHasKey('Authorization', $headers);
$this->assertContains('Digest', $headers['Authorization']);
}

/**
* @group 6301
*/
Expand Down

0 comments on commit 4e9e50f

Please # to comment.