Skip to content

Commit 57b4424

Browse files
committed
test: add tests per feedback
1 parent f87e41e commit 57b4424

2 files changed

Lines changed: 107 additions & 0 deletions

File tree

test/client/client_test.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3371,6 +3371,30 @@ def test_set_heading_if_not_set_multiple_headers_with_mixed_states(self):
33713371
# Original headers should still exist
33723372
self.assertEqual(result["headers"]["X-Another-Header"], "another-value")
33733373

3374+
def test_set_heading_if_not_set_two_defaults_two_customs_one_override(self):
3375+
"""Test setting two default headers when two custom headers exist, with one custom overriding one default"""
3376+
# Start with two custom headers
3377+
options = {
3378+
"headers": {
3379+
"X-Request-ID": "my-custom-request-id", # This should override the default
3380+
"X-Tenant-ID": "tenant-123", # This is custom-only
3381+
}
3382+
}
3383+
3384+
# Try to set two default headers
3385+
result = set_heading_if_not_set(options, "X-SDK-Version", "1.0.0")
3386+
result = set_heading_if_not_set(result, "X-Request-ID", "default-uuid")
3387+
3388+
# Verify all four headers exist with correct values
3389+
self.assertEqual(result["headers"]["X-SDK-Version"], "1.0.0") # Default was set
3390+
self.assertEqual(
3391+
result["headers"]["X-Request-ID"], "my-custom-request-id"
3392+
) # Custom overrode default
3393+
self.assertEqual(
3394+
result["headers"]["X-Tenant-ID"], "tenant-123"
3395+
) # Custom preserved
3396+
self.assertEqual(len(result["headers"]), 3) # Exactly 3 headers
3397+
33743398
def test_set_heading_if_not_set_with_empty_string_value(self):
33753399
"""Test that empty string values in custom headers are preserved and not overridden."""
33763400
options = {"headers": {"X-Custom-Header": ""}}
@@ -3402,6 +3426,36 @@ def test_set_heading_if_not_set_case_sensitivity(self):
34023426
self.assertEqual(result["headers"]["X-Custom-Header"], "uppercase")
34033427
self.assertEqual(result["headers"]["x-custom-header"], "lowercase")
34043428

3429+
@patch.object(rest.RESTClientObject, "request")
3430+
async def test_content_type_cannot_be_overridden_by_custom_headers(
3431+
self, mock_request
3432+
):
3433+
"""Test that Content-Type header cannot be overridden by custom headers."""
3434+
response_body = '{"allowed": true, "resolution": "1234"}'
3435+
mock_request.return_value = mock_response(response_body, 200)
3436+
3437+
body = ClientCheckRequest(
3438+
object="document:2021-budget",
3439+
relation="reader",
3440+
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
3441+
)
3442+
3443+
configuration = self.configuration
3444+
configuration.store_id = store_id
3445+
configuration.authorization_model_id = "01GXSA8YR785C4FYS3C0RTG7B1"
3446+
3447+
async with OpenFgaClient(configuration) as api_client:
3448+
# Try to override Content-Type with a custom value
3449+
custom_options = {"headers": {"Content-Type": "text/plain"}}
3450+
await api_client.check(body=body, options=custom_options)
3451+
3452+
call_args = mock_request.call_args
3453+
headers = call_args[1]["headers"]
3454+
3455+
# Content-Type should be application/json, NOT the custom text/plain
3456+
self.assertEqual(headers.get("Content-Type"), "application/json")
3457+
self.assertNotEqual(headers.get("Content-Type"), "text/plain")
3458+
34053459
@patch.object(rest.RESTClientObject, "request")
34063460
async def test_check_with_custom_headers_override_defaults(self, mock_request):
34073461
"""Test that custom headers in options override default headers for check API."""

test/sync/client/client_test.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3373,6 +3373,30 @@ def test_set_heading_if_not_set_multiple_headers_with_mixed_states(self):
33733373
# Original headers should still exist
33743374
self.assertEqual(result["headers"]["X-Another-Header"], "another-value")
33753375

3376+
def test_set_heading_if_not_set_two_defaults_two_customs_one_override(self):
3377+
"""Test setting two default headers when two custom headers exist, with one custom overriding one default"""
3378+
# Start with two custom headers
3379+
options = {
3380+
"headers": {
3381+
"X-Request-ID": "my-custom-request-id", # This should override the default
3382+
"X-Tenant-ID": "tenant-123", # This is custom-only
3383+
}
3384+
}
3385+
3386+
# Try to set two default headers
3387+
result = set_heading_if_not_set(options, "X-SDK-Version", "1.0.0")
3388+
result = set_heading_if_not_set(result, "X-Request-ID", "default-uuid")
3389+
3390+
# Verify all four headers exist with correct values
3391+
self.assertEqual(result["headers"]["X-SDK-Version"], "1.0.0") # Default was set
3392+
self.assertEqual(
3393+
result["headers"]["X-Request-ID"], "my-custom-request-id"
3394+
) # Custom overrode default
3395+
self.assertEqual(
3396+
result["headers"]["X-Tenant-ID"], "tenant-123"
3397+
) # Custom preserved
3398+
self.assertEqual(len(result["headers"]), 3) # Exactly 3 headers
3399+
33763400
def test_set_heading_if_not_set_with_empty_string_value(self):
33773401
"""Test that empty string values in custom headers are preserved and not overridden."""
33783402
options = {"headers": {"X-Custom-Header": ""}}
@@ -3406,6 +3430,35 @@ def test_set_heading_if_not_set_case_sensitivity(self):
34063430
self.assertEqual(result["headers"]["X-Custom-Header"], "uppercase")
34073431
self.assertEqual(result["headers"]["x-custom-header"], "lowercase")
34083432

3433+
@patch.object(rest.RESTClientObject, "request")
3434+
def test_content_type_cannot_be_overridden_by_custom_headers(self, mock_request):
3435+
"""Test that Content-Type header cannot be overridden by custom headers."""
3436+
response_body = '{"allowed": true, "resolution": "1234"}'
3437+
mock_request.return_value = mock_response(response_body, 200)
3438+
3439+
body = ClientCheckRequest(
3440+
object="document:2021-budget",
3441+
relation="reader",
3442+
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
3443+
)
3444+
3445+
configuration = self.configuration
3446+
configuration.store_id = store_id
3447+
configuration.authorization_model_id = "01GXSA8YR785C4FYS3C0RTG7B1"
3448+
3449+
with OpenFgaClient(configuration) as api_client:
3450+
# Try to override Content-Type with a custom value
3451+
custom_options = {"headers": {"Content-Type": "text/plain"}}
3452+
api_client.check(body=body, options=custom_options)
3453+
3454+
call_args = mock_request.call_args
3455+
headers = call_args[1]["headers"]
3456+
3457+
# Content-Type should be application/json, NOT the custom text/plain
3458+
self.assertEqual(headers.get("Content-Type"), "application/json")
3459+
self.assertNotEqual(headers.get("Content-Type"), "text/plain")
3460+
api_client.close()
3461+
34093462
@patch.object(rest.RESTClientObject, "request")
34103463
def test_check_with_custom_headers_override_defaults(self, mock_request):
34113464
"""Test that custom headers in options override default headers for check API."""

0 commit comments

Comments
 (0)