@@ -236,22 +236,23 @@ def set_headers(self, response: ResponseProtocol) -> None:
236236 RuntimeError: If an asynchronous 'set_header' method is used in a synchronous context.
237237 AttributeError: If the response object does not support setting headers.
238238 """
239- for header_name , header_value in self . headers . items ( ):
240- if isinstance ( response , SetHeaderProtocol ):
241- # If response has set_header method, use it
242- set_header = response . set_header
243- if inspect . iscoroutinefunction ( set_header ):
244- raise RuntimeError (
245- "Encountered asynchronous 'set_header' in synchronous context."
246- )
239+ if isinstance ( response , SetHeaderProtocol ):
240+ # Use the set_header method if available
241+ set_header = response . set_header
242+ if inspect . iscoroutinefunction ( set_header ):
243+ raise RuntimeError (
244+ "Encountered asynchronous 'set_header' in synchronous context."
245+ )
246+ for header_name , header_value in self . headers . items ():
247247 set_header (header_name , header_value )
248- elif isinstance (response , HeadersProtocol ): # type: ignore
249- # If response has headers dictionary, use it
248+ elif isinstance (response , HeadersProtocol ): # type: ignore
249+ # Use the headers dictionary if available
250+ for header_name , header_value in self .headers .items ():
250251 response .headers [header_name ] = header_value
251- else :
252- raise AttributeError (
253- f"Response object of type '{ type (response ).__name__ } ' does not support setting headers."
254- )
252+ else :
253+ raise AttributeError (
254+ f"Response object of type '{ type (response ).__name__ } ' does not support setting headers."
255+ )
255256
256257 async def set_headers_async (self , response : ResponseProtocol ) -> None :
257258 """
@@ -266,18 +267,20 @@ async def set_headers_async(self, response: ResponseProtocol) -> None:
266267 Raises:
267268 AttributeError: If the response object does not support setting headers.
268269 """
269- for header_name , header_value in self . headers . items ( ):
270- if isinstance ( response , SetHeaderProtocol ):
271- # If response has set_header method, use it
272- set_header = response . set_header
273- if inspect . iscoroutinefunction ( set_header ):
270+ if isinstance ( response , SetHeaderProtocol ):
271+ # Use the set_header method if available
272+ set_header = response . set_header
273+ if inspect . iscoroutinefunction ( set_header ):
274+ for header_name , header_value in self . headers . items ( ):
274275 await set_header (header_name , header_value )
275- else :
276+ else :
277+ for header_name , header_value in self .headers .items ():
276278 set_header (header_name , header_value )
277- elif isinstance (response , HeadersProtocol ): # type: ignore
278- # If response has headers dictionary, use it
279+ elif isinstance (response , HeadersProtocol ): # type: ignore
280+ # Use the headers dictionary if available
281+ for header_name , header_value in self .headers .items ():
279282 response .headers [header_name ] = header_value
280- else :
281- raise AttributeError (
282- f"Response object of type '{ type (response ).__name__ } ' does not support setting headers."
283- )
283+ else :
284+ raise AttributeError (
285+ f"Response object of type '{ type (response ).__name__ } ' does not support setting headers."
286+ )
0 commit comments