1- from django .test import TestCase
1+ from django .test import TestCase , override_settings
22
33from django .contrib .sites .models import Site
44from django .contrib .redirects .models import Redirect
55
6+ from pydotorg .middleware import GlobalSurrogateKey
7+
68
79class MiddlewareTests (TestCase ):
810
@@ -25,3 +27,40 @@ def test_redirects(self):
2527 response = self .client .get (url )
2628 self .assertEqual (response .status_code , 301 )
2729 self .assertEqual (response ['Location' ], redirect .new_path )
30+
31+
32+ class GlobalSurrogateKeyTests (TestCase ):
33+
34+ def test_get_section_key (self ):
35+ """Test section key extraction from URL paths."""
36+ middleware = GlobalSurrogateKey (lambda r : None )
37+
38+ self .assertEqual (middleware ._get_section_key ('/downloads/' ), 'downloads' )
39+ self .assertEqual (middleware ._get_section_key ('/downloads/release/python-3141/' ), 'downloads' )
40+ self .assertEqual (middleware ._get_section_key ('/events/' ), 'events' )
41+ self .assertEqual (middleware ._get_section_key ('/events/python-events/123/' ), 'events' )
42+ self .assertEqual (middleware ._get_section_key ('/sponsors/' ), 'sponsors' )
43+
44+ # returns None
45+ self .assertIsNone (middleware ._get_section_key ('/' ))
46+
47+ self .assertEqual (middleware ._get_section_key ('/downloads' ), 'downloads' )
48+ self .assertEqual (middleware ._get_section_key ('downloads/' ), 'downloads' )
49+
50+ @override_settings (GLOBAL_SURROGATE_KEY = 'pydotorg-app' )
51+ def test_surrogate_key_header_includes_section (self ):
52+ """Test that Surrogate-Key header includes both global and section keys."""
53+ response = self .client .get ('/downloads/' )
54+ self .assertTrue (response .has_header ('Surrogate-Key' ))
55+ surrogate_key = response ['Surrogate-Key' ]
56+
57+ self .assertIn ('pydotorg-app' , surrogate_key )
58+ self .assertIn ('downloads' , surrogate_key )
59+
60+ @override_settings (GLOBAL_SURROGATE_KEY = 'pydotorg-app' )
61+ def test_surrogate_key_header_homepage (self ):
62+ """Test that homepage only has global surrogate key."""
63+ response = self .client .get ('/' )
64+ self .assertTrue (response .has_header ('Surrogate-Key' ))
65+ surrogate_key = response ['Surrogate-Key' ]
66+ self .assertIn ('pydotorg-app' , surrogate_key )
0 commit comments