<s> from busbus . util import RateLimitRequests <EOL> from datetime import datetime , timedelta <EOL> import httpbin <EOL> import pytest <EOL> import requests <EOL> import time <EOL> from wsgi_intercept import requests_intercept , add_wsgi_intercept <EOL> @ pytest . fixture ( scope = '<STR_LIT>' ) <EOL> def url_prefix ( request ) : <EOL> host = '<STR_LIT>' <EOL> port = <NUM_LIT> <EOL> requests_intercept . install ( ) <EOL> add_wsgi_intercept ( host , port , lambda : httpbin . app ) <EOL> @ request . addfinalizer <EOL> def fin ( ) : <EOL> requests_intercept . uninstall ( ) <EOL> return '<STR_LIT>' . format ( host , port ) <EOL> def test_request ( url_prefix ) : <EOL> resp = requests . get ( url_prefix + '<STR_LIT>' ) <EOL> assert len ( resp . json ( ) [ '<STR_LIT:args>' ] ) == <NUM_LIT:0> <EOL> def test_repeat_get_no_rate_limit ( url_prefix ) : <EOL> session = RateLimitRequests ( ) <EOL> resp1 = session . get ( url_prefix + '<STR_LIT>' ) <EOL> session . get ( url_prefix + '<STR_LIT>' ) <EOL> resp2 = session . get ( url_prefix + '<STR_LIT>' ) <EOL> assert resp1 . json ( ) [ '<STR_LIT>' ] != resp2 . json ( ) [ '<STR_LIT>' ] <EOL> def test_get_interval ( url_prefix ) : <EOL> session = RateLimitRequests ( interval = <NUM_LIT> ) <EOL> resp1 = session . get ( url_prefix + '<STR_LIT>' ) <EOL> t = datetime . now ( ) <EOL> resp2 = session . get ( url_prefix + '<STR_LIT>' ) <EOL> assert datetime . now ( ) - t > timedelta ( seconds = <NUM_LIT:0.1> ) <EOL> def test_repeat_get_url_interval ( url_prefix ) : <EOL> session = RateLimitRequests ( url_interval = <NUM_LIT:1> ) <EOL> t = datetime . now ( ) <EOL> resp1 = session . get ( url_prefix + '<STR_LIT>' ) <EOL> session . get ( url_prefix + '<STR_LIT>' ) <EOL> resp2 = session . get ( url_prefix + '<STR_LIT>' ) <EOL> assert datetime . now ( ) - t < timedelta ( seconds = <NUM_LIT:1> ) <EOL> assert resp1 == resp2 <EOL> assert resp1 . json ( ) [ '<STR_LIT>' ] == resp2 . json ( ) [ '<STR_LIT>' ] <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> resp3 = session . get ( url_prefix + '<STR_LIT>' ) <EOL> assert resp1 != resp3 <EOL> assert resp1 . json ( ) [ '<STR_LIT>' ] != resp3 . json ( ) [ '<STR_LIT>' ] <EOL> def test_post ( url_prefix ) : <EOL> session = RateLimitRequests ( ) <EOL> assert not session . post ( url_prefix + '<STR_LIT>' ) . json ( ) [ '<STR_LIT>' ] </s>
