#NEWLINE# def test_cached_and_fresh( self ) : #NEWLINE# #INDENT# sampledata = [ ( 200 , { #NEWLINE# #INDENT# 'Date' : self . yesterday , #NEWLINE# 'Expires' : self . tomorrow , #NEWLINE# #UNINDENT# } ) , ( 200 , { #NEWLINE# #INDENT# 'Date' : self . yesterday , #NEWLINE# 'Cache-Control' : 'max-age=86405' , #NEWLINE# #UNINDENT# } ) , ( 200 , { #NEWLINE# #INDENT# 'Age' : '299' , #NEWLINE# 'Cache-Control' : 'max-age=300' , #NEWLINE# #UNINDENT# } ) , ( 200 , { #NEWLINE# #INDENT# 'Date' : self . today , #NEWLINE# 'Age' : '86405' , #NEWLINE# 'Cache-Control' : ( 'max-age=' + str ( ( 86400 * 3 ) ) ) , #NEWLINE# 'Expires' : self . yesterday , #NEWLINE# 'Last-Modified' : self . yesterday , #NEWLINE# #UNINDENT# } ) , ( 200 , { #NEWLINE# #INDENT# 'Date' : self . yesterday , #NEWLINE# 'Age' : '86400' , #NEWLINE# 'Cache-Control' : 'public' , #NEWLINE# 'Expires' : self . tomorrow , #NEWLINE# 'Last-Modified' : req1 . yesterday , #NEWLINE# #UNINDENT# } ) , ( 200 , { #NEWLINE# #INDENT# 'Expires' : self . tomorrow , #NEWLINE# #UNINDENT# } ) , ( 200 , { #NEWLINE# #INDENT# 'Date' : self . today , #NEWLINE# 'Last-Modified' : self . yesterday , #NEWLINE# 'Age' : str ( ( ( 86400 / 10 ) - 1 ) ) , #NEWLINE# #UNINDENT# } ) , ( 300 , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) , ( 301 , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) , ( 308 , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) ] #NEWLINE# with self . _middleware ( ) as mw : #NEWLINE# #INDENT# for ( idx , ( status , headers ) ) in enumerate ( sampledata ) : #NEWLINE# #INDENT# req0 = Request ( ( 'http://example-%d.com' % idx ) ) #NEWLINE# res0 = Response ( req0 . url , status = status , headers = headers ) #NEWLINE# res1 = self . _process_requestresponse ( mw , req0 , res0 ) #NEWLINE# self . assertEqualResponse ( res1 , res0 ) #NEWLINE# assert ( 'cached' not in res1 . flags ) #NEWLINE# res2 = self . _process_requestresponse ( mw , req0 , None ) #NEWLINE# self . assertEqualResponse ( res1 , res2 ) #NEWLINE# assert ( 'cached' in res2 . flags ) #NEWLINE# req1 = req0 . replace ( headers = { #NEWLINE# #INDENT# 'Cache-Control' : 'max-age=0' , #NEWLINE# #UNINDENT# } ) #NEWLINE# res304 = res0 . replace ( status = 304 ) #NEWLINE# assert ( mw . process_request ( req1 , self . spider ) is None ) #NEWLINE# res3 = self . _process_requestresponse ( mw , req1 , res304 ) #NEWLINE# self . assertEqualResponse ( res1 , res3 ) #NEWLINE# assert ( 'cached' in res3 . flags )