Skip to content

Commit f229c5c

Browse files
committed
send auth credentials only when necessary
1 parent 770f403 commit f229c5c

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ CHANGELOG
44
0.3.0
55
-----
66

7+
* Do not send authentication headers when no credentials have been configured.
8+
79
* Fixed treating HTTP methods case insensitive. Rejecting uppercased HTTP
810
method names contradicts the HTTP specification. Lowercased method names
911
will still be supported to keep backwards compatibility though.

spec/Request/HandlerSpec.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,18 @@ function it_sets_the_experience_api_version_header_and_the_content_type_header_w
2424
$client->get('/xapi/statements')->willReturn($request);
2525
$request->addHeader('X-Experience-API-Version', '1.0.1')->shouldBeCalled();
2626
$request->addHeader('Content-Type', 'application/json')->shouldBeCalled();
27-
$request->setAuth(null, null)->shouldBeCalled();
27+
28+
$this->createRequest('get', '/xapi/statements');
29+
}
30+
31+
function it_sets_basic_auth_data_when_credentials_are_configured_when_creating_a_request(ClientInterface $client, RequestInterface $request)
32+
{
33+
$this->beConstructedWith($client, '1.0.1', 'username', 'password');
34+
35+
$client->get('/xapi/statements')->willReturn($request);
36+
$request->addHeader('X-Experience-API-Version', '1.0.1')->shouldBeCalled();
37+
$request->addHeader('Content-Type', 'application/json')->shouldBeCalled();
38+
$request->setAuth('username', 'password')->shouldBeCalled();
2839

2940
$this->createRequest('get', '/xapi/statements');
3041
}

src/Request/Handler.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ public function createRequest($method, $uri, array $urlParameters = array(), $bo
7878

7979
$request->addHeader('X-Experience-API-Version', $this->version);
8080
$request->addHeader('Content-Type', 'application/json');
81-
$request->setAuth($this->username, $this->password);
81+
82+
if (null !== $this->username && null !== $this->password) {
83+
$request->setAuth($this->username, $this->password);
84+
}
8285

8386
return $request;
8487
}

0 commit comments

Comments
 (0)