Skip to content

Commit 7a24fbb

Browse files
authored
Merge pull request #99 from clerk/tm/pinned-version
feat: Pinned header version
2 parents dfc960b + b62dd7f commit 7a24fbb

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import Union
2+
3+
import httpx
4+
5+
from .types import (
6+
BeforeRequestContext,
7+
BeforeRequestHook,
8+
)
9+
10+
class ClerkBeforeRequestHook(BeforeRequestHook):
11+
def before_request(
12+
self, hook_ctx: BeforeRequestContext, request: httpx.Request
13+
) -> Union[httpx.Request, Exception]:
14+
request.headers["Clerk-API-Version"] = "2024-10-01"
15+
16+
return request

src/clerk_backend_api/_hooks/registration.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from .clerk_before_request_hook import ClerkBeforeRequestHook
12
from .types import Hooks
23

34

@@ -11,3 +12,4 @@ def init_hooks(hooks: Hooks):
1112
"""Add hooks by calling hooks.register{sdk_init/before_request/after_success/after_error}Hook
1213
with an instance of a hook that implements that specific Hook interface
1314
Hooks are registered per SDK instance, and are valid for the lifetime of the SDK instance"""
15+
hooks.register_before_request_hook(ClerkBeforeRequestHook())
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import pytest
2+
import httpx
3+
4+
from clerk_backend_api._hooks.clerk_before_request_hook import ClerkBeforeRequestHook
5+
from clerk_backend_api._hooks.types import (
6+
BeforeRequestContext,
7+
HookContext
8+
)
9+
10+
@pytest.fixture
11+
def hook():
12+
"""ClerkBeforeRequestHook instance"""
13+
return ClerkBeforeRequestHook()
14+
15+
@pytest.fixture
16+
def hook_context():
17+
"""HookContext instance"""
18+
return HookContext(
19+
base_url="https://api.clerk.dev",
20+
operation_id="test_operation",
21+
oauth2_scopes=None,
22+
security_source=None
23+
)
24+
25+
@pytest.fixture
26+
def before_request_context(hook_context):
27+
"""BeforeRequestContext instance"""
28+
return BeforeRequestContext(hook_context)
29+
30+
def test_before_request_adds_api_version_header(hook, before_request_context):
31+
"""Test that before_request adds the Clerk-API-Version header"""
32+
# Create a request
33+
request = httpx.Request("GET", "https://api.clerk.dev/v1/users")
34+
35+
# Call the before_request method
36+
modified_request = hook.before_request(before_request_context, request)
37+
38+
# Assert that the request is returned (not an Exception)
39+
assert isinstance(modified_request, httpx.Request)
40+
41+
# Assert that the Clerk-API-Version header is added with the correct value
42+
assert "Clerk-API-Version" in modified_request.headers
43+
assert modified_request.headers["Clerk-API-Version"] == "2024-10-01"
44+
45+
# Assert that the original request is modified, not a new one created
46+
assert modified_request is request
47+
48+
49+
def test_before_request_preserves_existing_headers(hook, before_request_context):
50+
"""Test that before_request preserves existing headers"""
51+
# Create a request with existing headers
52+
request = httpx.Request(
53+
"GET",
54+
"https://api.clerk.dev/v1/users",
55+
headers={"Authorization": "Bearer sdk_test_foo", "Content-Type": "application/json"}
56+
)
57+
58+
# Call the before_request method
59+
modified_request = hook.before_request(before_request_context, request)
60+
61+
# Assert that existing headers are preserved
62+
assert modified_request.headers["Authorization"] == "Bearer sdk_test_foo"
63+
assert modified_request.headers["Content-Type"] == "application/json"
64+
65+
# Assert that the Clerk-API-Version header is added
66+
assert modified_request.headers["Clerk-API-Version"] == "2024-10-01"
67+
68+
69+
def test_before_request_overwrites_existing_api_version_header(hook, before_request_context):
70+
"""Test that before_request overwrites an existing Clerk-API-Version header"""
71+
# Create a request with an existing Clerk-API-Version header
72+
request = httpx.Request(
73+
"GET",
74+
"https://api.clerk.dev/v1/users",
75+
headers={"Clerk-API-Version": "2021-02-05"}
76+
)
77+
78+
# Call the before_request method
79+
modified_request = hook.before_request(before_request_context, request)
80+
81+
# Assert that the Clerk-API-Version header is overwritten
82+
assert modified_request.headers["Clerk-API-Version"] == "2024-10-01"

0 commit comments

Comments
 (0)