From 095096060ea06ece4938af877a985cfcca389bc9 Mon Sep 17 00:00:00 2001 From: Nathan Boiron Date: Wed, 18 Nov 2020 16:19:14 +0100 Subject: [PATCH] Add a method to configure a Bearer token --- src/Http/Payload/RequestPayload.php | 19 +++++++++++++++++-- .../Unit/Payload/Http/RequestPayloadTest.php | 10 ++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Http/Payload/RequestPayload.php b/src/Http/Payload/RequestPayload.php index 862a28a..7cacdc8 100644 --- a/src/Http/Payload/RequestPayload.php +++ b/src/Http/Payload/RequestPayload.php @@ -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; @@ -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) { @@ -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']); + } } diff --git a/tests/Unit/Payload/Http/RequestPayloadTest.php b/tests/Unit/Payload/Http/RequestPayloadTest.php index 5944205..932079f 100644 --- a/tests/Unit/Payload/Http/RequestPayloadTest.php +++ b/tests/Unit/Payload/Http/RequestPayloadTest.php @@ -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', + ]); +});