<s> import logging <EOL> from io import BytesIO <EOL> from django . core . handlers . wsgi import WSGIRequest <EOL> from django . core . servers . basehttp import WSGIRequestHandler <EOL> from django . test import SimpleTestCase <EOL> from django . test . client import RequestFactory <EOL> from django . test . utils import patch_logger <EOL> class Stub ( object ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . __dict__ . update ( kwargs ) <EOL> class WSGIRequestHandlerTestCase ( SimpleTestCase ) : <EOL> def test_log_message ( self ) : <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> original_handlers = logger . handlers <EOL> logger . handlers = [ logging . NullHandler ( ) ] <EOL> try : <EOL> request = WSGIRequest ( RequestFactory ( ) . get ( '<STR_LIT:/>' ) . environ ) <EOL> request . makefile = lambda * args , ** kwargs : BytesIO ( ) <EOL> handler = WSGIRequestHandler ( request , '<STR_LIT>' , None ) <EOL> level_status_codes = { <EOL> '<STR_LIT:info>' : [ <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> '<STR_LIT:error>' : [ <NUM_LIT> , <NUM_LIT> ] , <EOL> } <EOL> def _log_level_code ( level , status_code ) : <EOL> with patch_logger ( '<STR_LIT>' , level ) as messages : <EOL> handler . log_message ( '<STR_LIT>' , '<STR_LIT:A>' , str ( status_code ) ) <EOL> return messages <EOL> for level , status_codes in level_status_codes . items ( ) : <EOL> for status_code in status_codes : <EOL> messages = _log_level_code ( level , status_code ) <EOL> self . assertIn ( '<STR_LIT>' % status_code , messages [ <NUM_LIT:0> ] ) <EOL> for wrong_level in level_status_codes . keys ( ) : <EOL> if wrong_level != level : <EOL> messages = _log_level_code ( wrong_level , status_code ) <EOL> self . assertEqual ( len ( messages ) , <NUM_LIT:0> ) <EOL> finally : <EOL> logger . handlers = original_handlers <EOL> def test_https ( self ) : <EOL> request = WSGIRequest ( RequestFactory ( ) . get ( '<STR_LIT:/>' ) . environ ) <EOL> request . makefile = lambda * args , ** kwargs : BytesIO ( ) <EOL> handler = WSGIRequestHandler ( request , '<STR_LIT>' , None ) <EOL> with patch_logger ( '<STR_LIT>' , '<STR_LIT:error>' ) as messages : <EOL> handler . log_message ( "<STR_LIT>" , str ( '<STR_LIT>' ) , "<STR_LIT:4>" ) <EOL> self . assertIn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> messages [ <NUM_LIT:0> ] <EOL> ) <EOL> def test_strips_underscore_headers ( self ) : <EOL> """<STR_LIT>""" <EOL> def test_app ( environ , start_response ) : <EOL> """<STR_LIT>""" <EOL> start_response ( '<STR_LIT>' , [ ] ) <EOL> http_environ_items = sorted ( <EOL> '<STR_LIT>' % ( k , v ) for k , v in environ . items ( ) <EOL> if k . startswith ( '<STR_LIT>' ) <EOL> ) <EOL> yield ( '<STR_LIT:U+002C>' . join ( http_environ_items ) ) . encode ( '<STR_LIT:utf-8>' ) <EOL> rfile = BytesIO ( ) <EOL> rfile . write ( b"<STR_LIT>" ) <EOL> rfile . write ( b"<STR_LIT>" ) <EOL> rfile . write ( b"<STR_LIT>" ) <EOL> rfile . write ( b"<STR_LIT>" ) <EOL> rfile . seek ( <NUM_LIT:0> ) <EOL> class UnclosableBytesIO ( BytesIO ) : <EOL> def close ( self ) : <EOL> pass <EOL> wfile = UnclosableBytesIO ( ) <EOL> def makefile ( mode , * a , ** kw ) : <EOL> if mode == '<STR_LIT:rb>' : <EOL> return rfile <EOL> elif mode == '<STR_LIT:wb>' : <EOL> return wfile <EOL> request = Stub ( makefile = makefile ) <EOL> server = Stub ( base_environ = { } , get_app = lambda : test_app ) <EOL> with patch_logger ( '<STR_LIT>' , '<STR_LIT:info>' ) : <EOL> WSGIRequestHandler ( request , '<STR_LIT>' , server ) <EOL> wfile . seek ( <NUM_LIT:0> ) <EOL> body = list ( wfile . readlines ( ) ) [ - <NUM_LIT:1> ] <EOL> self . assertEqual ( body , b'<STR_LIT>' ) </s>
