@@ -181,7 +181,70 @@ def test_mapping(self):
181181 self .assertEqual (
182182 _clean_extended_attribute ("headers" , mapping , None ), expected
183183 )
184-
184+
185+ def test_wsgi_request_attribute (self ):
186+ # Test WSGIRequest type validation
187+ try :
188+ from django .core .handlers .wsgi import WSGIRequest
189+ from django .conf import settings
190+ import io
191+
192+ # Configure Django settings if not already configured
193+ if not settings .configured :
194+ settings .configure (
195+ DEBUG = True ,
196+ SECRET_KEY = 'test-secret-key' ,
197+ USE_TZ = True ,
198+ ROOT_URLCONF = [],
199+ MIDDLEWARE = [],
200+ )
201+
202+ # Create a minimal WSGI environ dict
203+ environ = {
204+ 'REQUEST_METHOD' : 'GET' ,
205+ 'PATH_INFO' : '/test' ,
206+ 'QUERY_STRING' : '' ,
207+ 'CONTENT_TYPE' : '' ,
208+ 'CONTENT_LENGTH' : '' ,
209+ 'HTTP_HOST' : 'testserver' ,
210+ 'wsgi.version' : (1 , 0 ),
211+ 'wsgi.url_scheme' : 'http' ,
212+ 'wsgi.input' : io .StringIO (),
213+ 'wsgi.errors' : io .StringIO (),
214+ 'wsgi.multithread' : False ,
215+ 'wsgi.multiprocess' : False ,
216+ 'wsgi.run_once' : False ,
217+ 'SERVER_NAME' : 'testserver' ,
218+ 'SERVER_PORT' : '80' ,
219+ }
220+
221+ # Create a WSGIRequest object
222+ wsgi_request = WSGIRequest (environ )
223+
224+ # Test that WSGIRequest gets cleaned to a dictionary format
225+ expected_cleaned = {
226+ 'method' : 'GET' ,
227+ 'path' : '/test' ,
228+ 'path_info' : '/test' ,
229+ 'content_type' : ''
230+ }
231+
232+ # Test WSGIRequest cleaning directly
233+ from opentelemetry .attributes import _clean_extended_attribute
234+ cleaned_value = _clean_extended_attribute ("request" , wsgi_request , None )
235+ self .assertEqual (cleaned_value , expected_cleaned )
236+
237+ # Test WSGIRequest in sequences - should be cleaned to dict
238+ cleaned_sequence = _clean_extended_attribute ("requests" , [wsgi_request ], None )
239+ self .assertEqual (cleaned_sequence , (expected_cleaned ,))
240+
241+ # Test WSGIRequest in mappings - should be cleaned to dict
242+ cleaned_mapping = _clean_extended_attribute ("data" , {"request" : wsgi_request }, None )
243+ self .assertEqual (cleaned_mapping , {"request" : expected_cleaned })
244+
245+ except ImportError :
246+ # Skip test if django is not available
247+ self .skipTest ("Django not available" )
185248
186249class TestBoundedAttributes (unittest .TestCase ):
187250 # pylint: disable=consider-using-dict-items
0 commit comments