|
21 | 21 |
|
22 | 22 | from openfga_sdk import rest
|
23 | 23 | from openfga_sdk.client import ClientConfiguration
|
24 |
| -from openfga_sdk.client.client import OpenFgaClient |
| 24 | +from openfga_sdk.client.client import OpenFgaClient, set_heading_if_not_set |
25 | 25 | from openfga_sdk.client.models.assertion import ClientAssertion
|
26 | 26 | from openfga_sdk.client.models.batch_check_item import ClientBatchCheckItem
|
27 | 27 | from openfga_sdk.client.models.batch_check_request import ClientBatchCheckRequest
|
@@ -3273,3 +3273,100 @@ def test_configuration_authorization_model_id_invalid(self):
|
3273 | 3273 | authorization_model_id="abcd",
|
3274 | 3274 | )
|
3275 | 3275 | self.assertRaises(FgaValidationException, configuration.is_valid)
|
| 3276 | + |
| 3277 | + def test_set_heading_if_not_set_when_none_provided(self): |
| 3278 | + """Should set header when no options provided""" |
| 3279 | + result = set_heading_if_not_set(None, "X-Test-Header", "default-value") |
| 3280 | + |
| 3281 | + self.assertIsNotNone(result) |
| 3282 | + self.assertIn("headers", result) |
| 3283 | + self.assertEqual(result["headers"]["X-Test-Header"], "default-value") |
| 3284 | + |
| 3285 | + def test_set_heading_if_not_set_when_empty_options_provided(self): |
| 3286 | + """Should set header when empty options dict provided""" |
| 3287 | + result = set_heading_if_not_set({}, "X-Test-Header", "default-value") |
| 3288 | + |
| 3289 | + self.assertIn("headers", result) |
| 3290 | + self.assertEqual(result["headers"]["X-Test-Header"], "default-value") |
| 3291 | + |
| 3292 | + def test_set_heading_if_not_set_when_no_headers_in_options(self): |
| 3293 | + """Should set header when options dict has no headers key""" |
| 3294 | + options = {"page_size": 10} |
| 3295 | + result = set_heading_if_not_set(options, "X-Test-Header", "default-value") |
| 3296 | + |
| 3297 | + self.assertIn("headers", result) |
| 3298 | + self.assertEqual(result["headers"]["X-Test-Header"], "default-value") |
| 3299 | + self.assertEqual(result["page_size"], 10) |
| 3300 | + |
| 3301 | + def test_set_heading_if_not_set_when_headers_empty(self): |
| 3302 | + """Should set header when headers dict is empty""" |
| 3303 | + options = {"headers": {}} |
| 3304 | + result = set_heading_if_not_set(options, "X-Test-Header", "default-value") |
| 3305 | + |
| 3306 | + self.assertEqual(result["headers"]["X-Test-Header"], "default-value") |
| 3307 | + |
| 3308 | + def test_set_heading_if_not_set_does_not_override_existing_custom_header(self): |
| 3309 | + """Should NOT override when custom header already exists - this is the critical test for the bug fix""" |
| 3310 | + options = {"headers": {"X-Test-Header": "custom-value"}} |
| 3311 | + result = set_heading_if_not_set(options, "X-Test-Header", "default-value") |
| 3312 | + |
| 3313 | + # Custom header should be preserved, NOT overridden by default |
| 3314 | + self.assertEqual(result["headers"]["X-Test-Header"], "custom-value") |
| 3315 | + |
| 3316 | + def test_set_heading_if_not_set_preserves_other_headers_when_setting_new_header( |
| 3317 | + self, |
| 3318 | + ): |
| 3319 | + """Should preserve existing headers when setting a new one""" |
| 3320 | + options = {"headers": {"X-Existing-Header": "existing-value"}} |
| 3321 | + result = set_heading_if_not_set(options, "X-New-Header", "new-value") |
| 3322 | + |
| 3323 | + self.assertEqual(result["headers"]["X-Existing-Header"], "existing-value") |
| 3324 | + self.assertEqual(result["headers"]["X-New-Header"], "new-value") |
| 3325 | + |
| 3326 | + def test_set_heading_if_not_set_handles_integer_header_values(self): |
| 3327 | + """Should not override existing integer header values""" |
| 3328 | + options = {"headers": {"X-Retry-Count": 5}} |
| 3329 | + result = set_heading_if_not_set(options, "X-Retry-Count", 1) |
| 3330 | + |
| 3331 | + # Existing integer value should be preserved |
| 3332 | + self.assertEqual(result["headers"]["X-Retry-Count"], 5) |
| 3333 | + |
| 3334 | + def test_set_heading_if_not_set_handles_non_dict_headers_value(self): |
| 3335 | + """Should convert non-dict headers value to dict""" |
| 3336 | + options = {"headers": "invalid"} |
| 3337 | + result = set_heading_if_not_set(options, "X-Test-Header", "default-value") |
| 3338 | + |
| 3339 | + self.assertIsInstance(result["headers"], dict) |
| 3340 | + self.assertEqual(result["headers"]["X-Test-Header"], "default-value") |
| 3341 | + |
| 3342 | + def test_set_heading_if_not_set_does_not_mutate_when_header_exists(self): |
| 3343 | + """Should return same dict when header already exists""" |
| 3344 | + options = {"headers": {"X-Test-Header": "custom-value"}} |
| 3345 | + original_value = options["headers"]["X-Test-Header"] |
| 3346 | + |
| 3347 | + result = set_heading_if_not_set(options, "X-Test-Header", "default-value") |
| 3348 | + |
| 3349 | + # Should return the same modified dict |
| 3350 | + self.assertIs(result, options) |
| 3351 | + # Value should not have changed |
| 3352 | + self.assertEqual(result["headers"]["X-Test-Header"], original_value) |
| 3353 | + |
| 3354 | + def test_set_heading_if_not_set_multiple_headers_with_mixed_states(self): |
| 3355 | + """Should handle multiple headers, some existing and some new""" |
| 3356 | + options = { |
| 3357 | + "headers": { |
| 3358 | + "X-Custom-Header": "custom-value", |
| 3359 | + "X-Another-Header": "another-value", |
| 3360 | + } |
| 3361 | + } |
| 3362 | + |
| 3363 | + # Try to set a custom header (should not override) |
| 3364 | + result = set_heading_if_not_set(options, "X-Custom-Header", "default-value") |
| 3365 | + self.assertEqual(result["headers"]["X-Custom-Header"], "custom-value") |
| 3366 | + |
| 3367 | + # Try to set a new header (should be added) |
| 3368 | + result = set_heading_if_not_set(result, "X-New-Header", "new-value") |
| 3369 | + self.assertEqual(result["headers"]["X-New-Header"], "new-value") |
| 3370 | + |
| 3371 | + # Original headers should still exist |
| 3372 | + self.assertEqual(result["headers"]["X-Another-Header"], "another-value") |
0 commit comments