|
| 1 | +from atproto_client.request import RequestBase |
| 2 | + |
| 3 | + |
| 4 | +def test_get_headers_case_insensitivity() -> None: |
| 5 | + """Test that get_headers handles case-insensitive header names correctly.""" |
| 6 | + req = RequestBase() |
| 7 | + |
| 8 | + # Add a header with mixed case |
| 9 | + req.add_additional_header('Content-Type', 'application/json') |
| 10 | + |
| 11 | + # Try to override with a different case |
| 12 | + headers = req.get_headers({'content-type': 'text/plain'}) |
| 13 | + |
| 14 | + # Check that the header was properly overridden |
| 15 | + assert 'content-type' in headers |
| 16 | + assert headers['content-type'] == 'text/plain' |
| 17 | + assert 'Content-Type' not in headers # No mixed case keys |
| 18 | + |
| 19 | + # Check that there's only one content-type header (case-insensitive) |
| 20 | + content_type_headers = [k for k in headers if k.lower() == 'content-type'] |
| 21 | + assert len(content_type_headers) == 1 |
| 22 | + |
| 23 | + |
| 24 | +def test_add_additional_header_case_insensitivity() -> None: |
| 25 | + """Test that add_additional_header handles case-insensitive header names correctly.""" |
| 26 | + req = RequestBase() |
| 27 | + |
| 28 | + # Add a header with mixed case |
| 29 | + req.add_additional_header('Content-Type', 'application/json') |
| 30 | + |
| 31 | + # Add the same header with a different case |
| 32 | + req.add_additional_header('content-type', 'text/plain') |
| 33 | + |
| 34 | + # Get the headers |
| 35 | + headers = req.get_headers() |
| 36 | + |
| 37 | + # Check that the header was properly overridden |
| 38 | + assert 'content-type' in headers |
| 39 | + assert headers['content-type'] == 'text/plain' |
| 40 | + assert 'Content-Type' not in headers # No mixed case keys |
| 41 | + |
| 42 | + # Check that there's only one content-type header (case-insensitive) |
| 43 | + content_type_headers = [k for k in headers if k.lower() == 'content-type'] |
| 44 | + assert len(content_type_headers) == 1 |
| 45 | + |
| 46 | + |
| 47 | +def test_set_additional_headers_case_insensitivity() -> None: |
| 48 | + """Test set_additional_headers.""" |
| 49 | + req = RequestBase() |
| 50 | + |
| 51 | + # Set headers with a mixed case |
| 52 | + req.set_additional_headers( |
| 53 | + {'Content-Type': 'application/json', 'AUTHORIZATION': 'Bearer token', 'accept': 'application/json'} |
| 54 | + ) |
| 55 | + |
| 56 | + # Get the headers |
| 57 | + headers = req.get_headers() |
| 58 | + |
| 59 | + # Check that all headers are present |
| 60 | + assert 'Content-Type' in headers |
| 61 | + assert 'AUTHORIZATION' in headers |
| 62 | + assert 'accept' in headers |
| 63 | + |
| 64 | + # Check values |
| 65 | + assert headers['Content-Type'] == 'application/json' |
| 66 | + assert headers['AUTHORIZATION'] == 'Bearer token' |
| 67 | + assert headers['accept'] == 'application/json' |
| 68 | + |
| 69 | + |
| 70 | +def test_headers_override_with_additional_headers() -> None: |
| 71 | + """Test that additional headers properly override existing headers.""" |
| 72 | + req = RequestBase() |
| 73 | + |
| 74 | + # Add some headers |
| 75 | + req.add_additional_header('content-type', 'application/json') |
| 76 | + req.add_additional_header('authorization', 'Bearer token1') |
| 77 | + |
| 78 | + # Override with additional headers |
| 79 | + headers = req.get_headers({'Content-Type': 'text/plain', 'AUTHORIZATION': 'Bearer token2'}) |
| 80 | + |
| 81 | + # Check that headers were properly overridden |
| 82 | + assert headers['Content-Type'] == 'text/plain' |
| 83 | + assert headers['AUTHORIZATION'] == 'Bearer token2' |
| 84 | + |
| 85 | + # Check that there are no duplicate headers with different cases |
| 86 | + assert len([k for k in headers if k.lower() == 'content-type']) == 1 |
| 87 | + assert len([k for k in headers if k.lower() == 'authorization']) == 1 |
| 88 | + |
| 89 | + |
| 90 | +def test_headers_from_sources() -> None: |
| 91 | + """Test that headers from sources are properly handled.""" |
| 92 | + req = RequestBase() |
| 93 | + |
| 94 | + # Add a header source |
| 95 | + req.add_additional_headers_source(lambda: {'Content-Type': 'application/json'}) |
| 96 | + |
| 97 | + # Add another header source with a different case |
| 98 | + req.add_additional_headers_source(lambda: {'content-type': 'text/plain'}) |
| 99 | + |
| 100 | + # Get the headers |
| 101 | + headers = req.get_headers() |
| 102 | + |
| 103 | + # Check that the last source's value is used |
| 104 | + assert headers['content-type'] == 'text/plain' |
| 105 | + |
| 106 | + # Check that the first source's value is not used |
| 107 | + assert 'Content-Type' not in headers |
| 108 | + |
| 109 | + # Check that there are no duplicate headers with different cases |
| 110 | + assert len([k for k in headers if k.lower() == 'content-type']) == 1 |
0 commit comments