<s> from __future__ import absolute_import , division <EOL> from twisted . trial import unittest <EOL> import sys <EOL> from mock import Mock , patch <EOL> from twisted . python . components import registerAdapter <EOL> from klein import Klein <EOL> from klein . app import KleinRequest <EOL> from klein . interfaces import IKleinRequest <EOL> from klein . test . util import EqualityTestsMixin <EOL> class DummyRequest ( object ) : <EOL> def __init__ ( self , n ) : <EOL> self . n = n <EOL> def __eq__ ( self , other ) : <EOL> return other . n == self . n <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( n = self . n ) <EOL> registerAdapter ( KleinRequest , DummyRequest , IKleinRequest ) <EOL> class KleinEqualityTestCase ( unittest . TestCase , EqualityTestsMixin ) : <EOL> """<STR_LIT>""" <EOL> class _One ( object ) : <EOL> app = Klein ( ) <EOL> def __eq__ ( self , other ) : <EOL> return True <EOL> def __ne__ ( self , other ) : <EOL> return False <EOL> def __hash__ ( self ) : <EOL> return id ( self ) <EOL> _another = Klein ( ) <EOL> def anInstance ( self ) : <EOL> return self . _One ( ) . app <EOL> def anotherInstance ( self ) : <EOL> return self . _another <EOL> class KleinTestCase ( unittest . TestCase ) : <EOL> def test_route ( self ) : <EOL> """<STR_LIT>""" <EOL> app = Klein ( ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> def foo ( request ) : <EOL> return "<STR_LIT:foo>" <EOL> c = app . url_map . bind ( "<STR_LIT:foo>" ) <EOL> self . assertEqual ( c . match ( "<STR_LIT>" ) , ( "<STR_LIT:foo>" , { } ) ) <EOL> self . assertEqual ( len ( app . endpoints ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( app . execute_endpoint ( "<STR_LIT:foo>" , DummyRequest ( <NUM_LIT:1> ) ) , "<STR_LIT:foo>" ) <EOL> def test_submountedRoute ( self ) : <EOL> """<STR_LIT>""" <EOL> app = Klein ( ) <EOL> with app . subroute ( "<STR_LIT>" ) as app : <EOL> @ app . route ( "<STR_LIT>" ) <EOL> def foo_endpoint ( request ) : <EOL> return b"<STR_LIT:foo>" <EOL> c = app . url_map . bind ( "<STR_LIT>" ) <EOL> self . assertEqual ( <EOL> c . match ( "<STR_LIT>" ) , ( "<STR_LIT>" , { } ) ) <EOL> self . assertEqual ( <EOL> len ( app . endpoints ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( <EOL> app . execute_endpoint ( "<STR_LIT>" , DummyRequest ( <NUM_LIT:1> ) ) , b"<STR_LIT:foo>" ) <EOL> def test_stackedRoute ( self ) : <EOL> """<STR_LIT>""" <EOL> app = Klein ( ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> @ app . route ( "<STR_LIT>" , endpoint = "<STR_LIT:bar>" ) <EOL> def foobar ( request ) : <EOL> return "<STR_LIT>" <EOL> self . assertEqual ( len ( app . endpoints ) , <NUM_LIT:2> ) <EOL> c = app . url_map . bind ( "<STR_LIT:foo>" ) <EOL> self . assertEqual ( c . match ( "<STR_LIT>" ) , ( "<STR_LIT>" , { } ) ) <EOL> self . assertEqual ( app . execute_endpoint ( "<STR_LIT>" , DummyRequest ( <NUM_LIT:1> ) ) , "<STR_LIT>" ) <EOL> self . assertEqual ( c . match ( "<STR_LIT>" ) , ( "<STR_LIT:bar>" , { } ) ) <EOL> self . assertEqual ( app . execute_endpoint ( "<STR_LIT:bar>" , DummyRequest ( <NUM_LIT:2> ) ) , "<STR_LIT>" ) <EOL> def test_branchRoute ( self ) : <EOL> """<STR_LIT>""" <EOL> app = Klein ( ) <EOL> @ app . route ( "<STR_LIT>" , branch = True ) <EOL> def foo ( request ) : <EOL> return "<STR_LIT:foo>" <EOL> c = app . url_map . bind ( "<STR_LIT:foo>" ) <EOL> self . assertEqual ( c . match ( "<STR_LIT>" ) , ( "<STR_LIT:foo>" , { } ) ) <EOL> self . assertEqual ( <EOL> c . match ( "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , { '<STR_LIT>' : '<STR_LIT:bar>' } ) ) <EOL> self . assertEquals ( app . endpoints [ "<STR_LIT:foo>" ] . __name__ , "<STR_LIT:foo>" ) <EOL> self . assertEquals ( <EOL> app . endpoints [ "<STR_LIT>" ] . __name__ , <EOL> "<STR_LIT:foo>" ) <EOL> def test_classicalRoute ( self ) : <EOL> """<STR_LIT>""" <EOL> bar_calls = [ ] <EOL> class Foo ( object ) : <EOL> app = Klein ( ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> def bar ( self , request ) : <EOL> bar_calls . append ( ( self , request ) ) <EOL> return "<STR_LIT:bar>" <EOL> foo = Foo ( ) <EOL> c = foo . app . url_map . bind ( "<STR_LIT:bar>" ) <EOL> self . assertEqual ( c . match ( "<STR_LIT>" ) , ( "<STR_LIT:bar>" , { } ) ) <EOL> self . assertEquals ( foo . app . execute_endpoint ( "<STR_LIT:bar>" , DummyRequest ( <NUM_LIT:1> ) ) , "<STR_LIT:bar>" ) <EOL> self . assertEqual ( bar_calls , [ ( foo , DummyRequest ( <NUM_LIT:1> ) ) ] ) <EOL> def test_classicalRouteWithTwoInstances ( self ) : <EOL> """<STR_LIT>""" <EOL> class Foo ( object ) : <EOL> app = Klein ( ) <EOL> def __init__ ( self ) : <EOL> self . bar_calls = [ ] <EOL> @ app . route ( "<STR_LIT>" ) <EOL> def bar ( self , request ) : <EOL> self . bar_calls . append ( ( self , request ) ) <EOL> return "<STR_LIT:bar>" <EOL> foo_1 = Foo ( ) <EOL> foo_1_app = foo_1 . app <EOL> foo_2 = Foo ( ) <EOL> foo_2_app = foo_2 . app <EOL> dr1 = DummyRequest ( <NUM_LIT:1> ) <EOL> dr2 = DummyRequest ( <NUM_LIT:2> ) <EOL> foo_1_app . execute_endpoint ( '<STR_LIT:bar>' , dr1 ) <EOL> foo_2_app . execute_endpoint ( '<STR_LIT:bar>' , dr2 ) <EOL> self . assertEqual ( foo_1 . bar_calls , [ ( foo_1 , dr1 ) ] ) <EOL> self . assertEqual ( foo_2 . bar_calls , [ ( foo_2 , dr2 ) ] ) <EOL> def test_classicalRouteWithBranch ( self ) : <EOL> """<STR_LIT>""" <EOL> class Foo ( object ) : <EOL> app = Klein ( ) <EOL> def __init__ ( self ) : <EOL> self . bar_calls = [ ] <EOL> @ app . route ( "<STR_LIT>" , branch = True ) <EOL> def bar ( self , request ) : <EOL> self . bar_calls . append ( ( self , request ) ) <EOL> return "<STR_LIT:bar>" <EOL> foo_1 = Foo ( ) <EOL> foo_1_app = foo_1 . app <EOL> foo_2 = Foo ( ) <EOL> foo_2_app = foo_2 . app <EOL> dr1 = DummyRequest ( <NUM_LIT:1> ) <EOL> dr2 = DummyRequest ( <NUM_LIT:2> ) <EOL> foo_1_app . execute_endpoint ( '<STR_LIT>' , dr1 ) <EOL> foo_2_app . execute_endpoint ( '<STR_LIT>' , dr2 ) <EOL> self . assertEqual ( foo_1 . bar_calls , [ ( foo_1 , dr1 ) ] ) <EOL> self . assertEqual ( foo_2 . bar_calls , [ ( foo_2 , dr2 ) ] ) <EOL> def test_branchDoesntRequireTrailingSlash ( self ) : <EOL> """<STR_LIT>""" <EOL> app = Klein ( ) <EOL> @ app . route ( "<STR_LIT>" , branch = True ) <EOL> def foo ( request ) : <EOL> return "<STR_LIT:foo>" <EOL> c = app . url_map . bind ( "<STR_LIT:foo>" ) <EOL> self . assertEqual ( c . match ( "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT:bar>" } ) ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_run ( self , reactor , mock_log , mock_site , mock_kr ) : <EOL> """<STR_LIT>""" <EOL> app = Klein ( ) <EOL> app . run ( "<STR_LIT:localhost>" , <NUM_LIT> ) <EOL> reactor . listenTCP . assert_called_with ( <EOL> <NUM_LIT> , mock_site . return_value , interface = "<STR_LIT:localhost>" ) <EOL> reactor . run . assert_called_with ( ) <EOL> mock_site . assert_called_with ( mock_kr . return_value ) <EOL> mock_kr . assert_called_with ( app ) <EOL> mock_log . startLogging . assert_called_with ( sys . stdout ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_runWithLogFile ( self , reactor , mock_log , mock_site , mock_kr ) : <EOL> """<STR_LIT>""" <EOL> app = Klein ( ) <EOL> logFile = Mock ( ) <EOL> app . run ( "<STR_LIT:localhost>" , <NUM_LIT> , logFile = logFile ) <EOL> reactor . listenTCP . assert_called_with ( <EOL> <NUM_LIT> , mock_site . return_value , interface = "<STR_LIT:localhost>" ) <EOL> reactor . run . assert_called_with ( ) <EOL> mock_site . assert_called_with ( mock_kr . return_value ) <EOL> mock_kr . assert_called_with ( app ) <EOL> mock_log . startLogging . assert_called_with ( logFile ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_resource ( self , mock_kr ) : <EOL> """<STR_LIT>""" <EOL> app = Klein ( ) <EOL> resource = app . resource ( ) <EOL> mock_kr . assert_called_with ( app ) <EOL> self . assertEqual ( mock_kr . return_value , resource ) </s>
