Skip to content

Commit

Permalink
feat: Pinned header version (#17)
Browse files Browse the repository at this point in the history
* feat: Pinned header version

* test: Add tests for appending Clerk-API-Version

* chore: Remove extra spaces
  • Loading branch information
tmilewski authored Feb 27, 2025
1 parent 0a44547 commit 9587f37
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .speakeasy/workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ speakeasyVersion: latest
sources:
clerk-openapi:
inputs:
- location: https://raw.githubusercontent.com/clerk/openapi-specs/refs/heads/main/bapi/2021-02-05.yml
- location: https://raw.githubusercontent.com/clerk/openapi-specs/refs/heads/main/bapi/2024-10-01.yml
registry:
location: registry.speakeasyapi.dev/clerk/clerk/clerk-openapi
targets:
Expand Down
46 changes: 46 additions & 0 deletions Tests/Hooks/ClerkBeforeRequestHooksTest.php
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);
}
}
61 changes: 61 additions & 0 deletions Tests/Hooks/HooksTest.php
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'));
}
}
18 changes: 18 additions & 0 deletions src/Hooks/ClerkBeforeRequestHooks.php
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'
);
}
}
2 changes: 2 additions & 0 deletions src/Hooks/HookRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class HookRegistration
*/
public static function initHooks(Hooks $hooks): void
{
$hooks->registerBeforeRequestHook(new ClerkBeforeRequestHooks());

// $myHook = new MyHook();

// $hooks->registerSDKInitHook($myHook);
Expand Down

0 comments on commit 9587f37

Please sign in to comment.