1
+ import asyncio
1
2
import unittest
2
3
3
4
from secure import (
@@ -44,6 +45,20 @@ class MockResponseNoHeaders:
44
45
45
46
46
47
class TestSecure (unittest .TestCase ):
48
+ def setUp (self ):
49
+ # Initialize Secure with some test headers
50
+ self .secure = Secure (
51
+ custom = [
52
+ CustomHeader ("X-Test-Header-1" , "Value1" ),
53
+ CustomHeader ("X-Test-Header-2" , "Value2" ),
54
+ ]
55
+ )
56
+ # Precompute headers dictionary
57
+ self .secure .headers = {
58
+ header .header_name : header .header_value
59
+ for header in self .secure .headers_list
60
+ }
61
+
47
62
def test_with_default_headers (self ):
48
63
"""Test that default headers are correctly applied."""
49
64
secure_headers = Secure .with_default_headers ()
@@ -210,8 +225,6 @@ def test_async_set_headers(self):
210
225
async def mock_set_headers ():
211
226
await secure_headers .set_headers_async (response )
212
227
213
- import asyncio
214
-
215
228
asyncio .run (mock_set_headers ())
216
229
217
230
# Verify that headers are set asynchronously
@@ -235,43 +248,43 @@ async def mock_set_headers():
235
248
236
249
def test_set_headers_with_set_header_method (self ):
237
250
"""Test setting headers on a response object with set_header method."""
238
- secure_headers = Secure .with_default_headers ()
239
251
response = MockResponseWithSetHeader ()
240
-
241
- # Apply the headers to the response object
242
- secure_headers .set_headers (response )
252
+ self .secure .set_headers (response )
243
253
244
254
# Verify that headers are set using set_header method
245
- self .assertIn ("Strict-Transport-Security" , response .header_storage )
246
- self .assertEqual (
247
- response .header_storage ["Strict-Transport-Security" ],
248
- "max-age=31536000" ,
249
- )
255
+ self .assertEqual (response .header_storage , self .secure .headers )
256
+ # Ensure set_header was called correct number of times
257
+ self .assertEqual (len (response .header_storage ), len (self .secure .headers ))
250
258
251
- self .assertIn ("X-Content-Type-Options" , response .header_storage )
252
- self .assertEqual (response .header_storage ["X-Content-Type-Options" ], "nosniff" )
259
+ def test_set_headers_with_headers_dict (self ):
260
+ """Test set_headers with a response object that has a headers dictionary."""
261
+ response = MockResponse ()
262
+ self .secure .set_headers (response )
253
263
254
- def test_async_set_headers_with_async_set_header_method (self ):
255
- """Test async setting headers on a response object with async set_header method."""
256
- secure_headers = Secure .with_default_headers ()
257
- response = MockResponseAsyncSetHeader ()
264
+ # Verify that headers are set
265
+ self .assertEqual (response .headers , self .secure .headers )
258
266
259
- async def mock_set_headers ():
260
- await secure_headers .set_headers_async (response )
267
+ def test_set_headers_async_with_async_set_header (self ):
268
+ """Test set_headers_async with a response object that has an asynchronous set_header method."""
269
+ response = MockResponseAsyncSetHeader ()
261
270
262
- import asyncio
271
+ async def test_async ():
272
+ await self .secure .set_headers_async (response )
263
273
264
- asyncio .run (mock_set_headers ())
274
+ asyncio .run (test_async ())
265
275
266
276
# Verify that headers are set using async set_header method
267
- self .assertIn ("Strict-Transport-Security" , response .header_storage )
268
- self .assertEqual (
269
- response .header_storage ["Strict-Transport-Security" ],
270
- "max-age=31536000" ,
271
- )
277
+ self .assertEqual (response .header_storage , self .secure .headers )
278
+ # Ensure set_header was called correct number of times
279
+ self .assertEqual (len (response .header_storage ), len (self .secure .headers ))
280
+
281
+ def test_set_headers_async_with_headers_dict (self ):
282
+ """Test set_headers_async with a response object that has a headers dictionary."""
283
+ response = MockResponse ()
284
+ asyncio .run (self .secure .set_headers_async (response ))
272
285
273
- self . assertIn ( "X-Content-Type-Options" , response . header_storage )
274
- self .assertEqual (response .header_storage [ "X-Content-Type-Options" ], "nosniff" )
286
+ # Verify that headers are set
287
+ self .assertEqual (response .headers , self . secure . headers )
275
288
276
289
def test_set_headers_missing_interface (self ):
277
290
"""Test that an error is raised when response object lacks required methods."""
@@ -286,6 +299,12 @@ def test_set_headers_missing_interface(self):
286
299
str (context .exception ),
287
300
)
288
301
302
+ def test_set_headers_with_async_set_header_in_sync_context (self ):
303
+ """Test set_headers raises RuntimeError when encountering async set_header in sync context."""
304
+ response = MockResponseAsyncSetHeader ()
305
+ with self .assertRaises (RuntimeError ):
306
+ self .secure .set_headers (response )
307
+
289
308
def test_set_headers_overwrites_existing_headers (self ):
290
309
"""Test that existing headers are overwritten by Secure."""
291
310
secure_headers = Secure .with_default_headers ()
@@ -347,10 +366,10 @@ def test_invalid_preset(self):
347
366
348
367
def test_empty_secure_instance (self ):
349
368
"""Test that an empty Secure instance does not set any headers."""
350
- secure_headers = Secure ()
369
+ self . secure = Secure ()
351
370
response = MockResponse ()
352
371
353
- secure_headers .set_headers (response )
372
+ self . secure .set_headers (response )
354
373
self .assertEqual (len (response .headers ), 0 )
355
374
356
375
def test_multiple_custom_headers (self ):
@@ -430,16 +449,10 @@ def test_set_headers_async_with_sync_set_header(self):
430
449
async def mock_set_headers ():
431
450
await secure_headers .set_headers_async (response )
432
451
433
- import asyncio
434
-
435
452
asyncio .run (mock_set_headers ())
436
453
437
454
# Verify that headers are set using set_header method
438
- self .assertIn ("Strict-Transport-Security" , response .header_storage )
439
- self .assertEqual (
440
- response .header_storage ["Strict-Transport-Security" ],
441
- "max-age=31536000" ,
442
- )
455
+ self .assertEqual (response .header_storage , secure_headers .headers )
443
456
444
457
def test_set_headers_with_no_headers_or_set_header (self ):
445
458
"""Test that an error is raised when response lacks both headers and set_header."""
0 commit comments