Skip to content

Commit 6712958

Browse files
committed
Add Request::setBearerAuth method
1 parent cc7ff03 commit 6712958

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/Request.php

+15
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,21 @@ public function setBasicAuth(string $username, string $password) : static
483483
);
484484
}
485485

486+
/**
487+
* Set Authorization header with Bearer type.
488+
*
489+
* @param string $token
490+
*
491+
* @return static
492+
*/
493+
public function setBearerAuth(string $token) : static
494+
{
495+
return $this->setHeader(
496+
RequestHeader::AUTHORIZATION,
497+
'Bearer ' . $token
498+
);
499+
}
500+
486501
/**
487502
* Set the User-Agent header.
488503
*

tests/RequestTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ public function testBasicAuth() : void
148148
);
149149
}
150150

151+
public function testBearerAuth() : void
152+
{
153+
self::assertEmpty($this->request->getHeader('authorization'));
154+
$this->request->setBearerAuth('foobar');
155+
self::assertSame(
156+
'Bearer foobar',
157+
$this->request->getHeader('authorization')
158+
);
159+
}
160+
151161
public function testUserAgent() : void
152162
{
153163
self::assertEmpty($this->request->getHeader('user-agent'));

0 commit comments

Comments
 (0)