Skip to content

Commit

Permalink
Add a method to configure a Bearer token
Browse files Browse the repository at this point in the history
  • Loading branch information
Mopolo committed Nov 18, 2020
1 parent 8d22536 commit 0950960
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/Http/Payload/RequestPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ public function withHeader(string $name, string $value): self

public function withAuthBasic(string $username, ?string $password = null): self
{
unset($this->options['auth_bearer']);
unset($this->options['auth_ntlm']);
$this->clearAuths();

$auth = $username;

Expand All @@ -118,6 +117,15 @@ public function withAuthBasic(string $username, ?string $password = null): self
return $this;
}

public function withAuthBearer(string $token): self
{
$this->clearAuths();

$this->options['auth_bearer'] = $token;

return $this;
}

public function method(): string
{
if (null === $this->method || strlen($this->method) === 0) {
Expand All @@ -144,4 +152,11 @@ public function options(): array
{
return $this->options;
}

private function clearAuths(): void
{
unset($this->options['auth_basic']);
unset($this->options['auth_bearer']);
unset($this->options['auth_ntlm']);
}
}
10 changes: 10 additions & 0 deletions tests/Unit/Payload/Http/RequestPayloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,13 @@
'auth_basic' => 'fiz:baz',
]);
});

it('saves "bearer auth" option', function () {
$payload = new RequestPayload('foo', 'bar');

$payload->withAuthBearer('foo');

expect($payload->options())->toBe([
'auth_bearer' => 'foo',
]);
});

0 comments on commit 0950960

Please sign in to comment.