<s> from nova . tests import * <EOL> import webob . dec <EOL> from nova import test <EOL> from nova import context <EOL> from nova . api . openstack . limits import RateLimitingMiddleware <EOL> from nova . api . openstack . common import limited <EOL> from nova . tests . api . openstack import fakes <EOL> from webob import Request <EOL> @ webob . dec . wsgify <EOL> def simple_wsgi ( req ) : <EOL> return "<STR_LIT>" <EOL> class RateLimitingMiddlewareTest ( test . TestCase ) : <EOL> def test_get_action_name ( self ) : <EOL> middleware = RateLimitingMiddleware ( simple_wsgi ) <EOL> def verify ( method , url , action_name ) : <EOL> req = Request . blank ( url ) <EOL> req . method = method <EOL> action = middleware . get_action_name ( req ) <EOL> self . assertEqual ( action , action_name ) <EOL> verify ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> verify ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> verify ( '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT:POST>' ) <EOL> verify ( '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> verify ( '<STR_LIT:GET>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> verify ( '<STR_LIT:GET>' , '<STR_LIT>' , None ) <EOL> verify ( '<STR_LIT:GET>' , '<STR_LIT>' , None ) <EOL> verify ( '<STR_LIT>' , '<STR_LIT>' , None ) <EOL> def exhaust ( self , middleware , method , url , username , times ) : <EOL> req = Request . blank ( url , dict ( REQUEST_METHOD = method ) , <EOL> headers = { '<STR_LIT>' : username } ) <EOL> req . environ [ '<STR_LIT>' ] = context . RequestContext ( username , <EOL> username ) <EOL> for i in range ( times ) : <EOL> resp = req . get_response ( middleware ) <EOL> self . assertEqual ( resp . status_int , <NUM_LIT:200> ) <EOL> resp = req . get_response ( middleware ) <EOL> self . assertEqual ( resp . status_int , <NUM_LIT> ) <EOL> self . assertTrue ( '<STR_LIT>' in resp . headers ) <EOL> def test_single_action ( self ) : <EOL> middleware = RateLimitingMiddleware ( simple_wsgi ) <EOL> self . exhaust ( middleware , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:100> ) <EOL> self . exhaust ( middleware , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:100> ) <EOL> def test_POST_servers_action_implies_POST_action ( self ) : <EOL> middleware = RateLimitingMiddleware ( simple_wsgi ) <EOL> self . exhaust ( middleware , '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:10> ) <EOL> self . exhaust ( middleware , '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:10> ) <EOL> self . assertTrue ( set ( middleware . limiter . _levels ) == set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> def test_POST_servers_action_correctly_ratelimited ( self ) : <EOL> middleware = RateLimitingMiddleware ( simple_wsgi ) <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> self . exhaust ( middleware , '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:10> ) <EOL> del middleware . limiter . _levels [ '<STR_LIT>' ] <EOL> self . exhaust ( middleware , '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> def test_proxy_ctor_works ( self ) : <EOL> middleware = RateLimitingMiddleware ( simple_wsgi ) <EOL> self . assertEqual ( middleware . limiter . __class__ . __name__ , "<STR_LIT>" ) <EOL> middleware = RateLimitingMiddleware ( simple_wsgi , service_host = '<STR_LIT>' ) <EOL> self . assertEqual ( middleware . limiter . __class__ . __name__ , "<STR_LIT>" ) </s>
