-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Pinned header version * test: Add tests for appending Clerk-API-Version * chore: Remove extra spaces
- Loading branch information
Showing
5 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Clerk\Backend\Tests\Hooks; | ||
|
||
use Clerk\Backend\Hooks\BeforeRequestContext; | ||
use Clerk\Backend\Hooks\ClerkBeforeRequestHooks; | ||
use Clerk\Backend\Hooks\HookContext; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
|
||
final class ClerkBeforeRequestHooksTest extends TestCase | ||
{ | ||
public function test_adds_api_version_header(): void | ||
{ | ||
// Create a mock for the RequestInterface | ||
$request = $this->createMock(RequestInterface::class); | ||
|
||
// Set up the mock to expect withHeader to be called with the correct parameters | ||
$request->expects($this->once()) | ||
->method('withHeader') | ||
->with( | ||
$this->equalTo('Clerk-API-Version'), | ||
$this->equalTo('2024-10-01') | ||
) | ||
->willReturnSelf(); | ||
|
||
// Create a mock for the HookContext | ||
$hookContext = new HookContext('test_operation', null, null); | ||
|
||
// Create the BeforeRequestContext with the HookContext | ||
$context = new BeforeRequestContext($hookContext); | ||
|
||
// Create the hook instance | ||
$hook = new ClerkBeforeRequestHooks(); | ||
|
||
// Call the beforeRequest method | ||
$result = $hook->beforeRequest($context, $request); | ||
|
||
// Assert that the result is the same as the request (since we configured the mock to return itself) | ||
$this->assertSame($request, $result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Clerk\Backend\Tests; | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
use Clerk\Backend; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Handler\MockHandler; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Middleware; | ||
use GuzzleHttp\Psr7\Response; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class HooksTest extends TestCase | ||
{ | ||
private $apiVersion = '2024-10-01'; | ||
|
||
public function testGetJwksWithApiVersionHeader(): void | ||
{ | ||
// Create a container to capture the request | ||
$container = []; | ||
$history = Middleware::history($container); | ||
|
||
// Create a mock response | ||
$mockResponse = new Response( | ||
200, | ||
['Content-Type' => 'application/json'], | ||
json_encode([ | ||
'keys' => [] | ||
]) | ||
); | ||
|
||
$mock = new MockHandler([$mockResponse]); | ||
$handlerStack = HandlerStack::create($mock); | ||
$handlerStack->push($history); | ||
|
||
// Create a client with the mock handler | ||
$client = new Client(['handler' => $handlerStack]); | ||
|
||
// Create SDK with the mock | ||
$sdk = Backend\ClerkBackend::builder() | ||
->setSecurity('sk_test_foo') | ||
->setClient($client) | ||
->build(); | ||
|
||
$sdk->jwks->get(); | ||
|
||
// Assert we made exactly one request | ||
$this->assertCount(1, $container); | ||
|
||
// Get the request from the container | ||
$request = $container[0]['request']; | ||
|
||
// Assert the Clerk-API-Version header was set correctly | ||
$this->assertTrue($request->hasHeader('Clerk-API-Version')); | ||
$this->assertEquals($this->apiVersion, $request->getHeaderLine('Clerk-API-Version')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Clerk\Backend\Hooks; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
|
||
class ClerkBeforeRequestHooks implements BeforeRequestHook | ||
{ | ||
public function beforeRequest(BeforeRequestContext $context, RequestInterface $request): RequestInterface | ||
{ | ||
return $request->withHeader( | ||
'Clerk-API-Version', | ||
'2024-10-01' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters