def test_stop_option(self): add_rule(Criteria(path='/foo'), Actions(set_input_header=('Header-Name', 'FOO')), stop=True) add_rule(Criteria(path='/foo'), Actions(set_input_header=('Header-Name', 'BAR'))) request = HTTPServerRequest(method='GET', uri='/foo') Rules.execute_input_actions(HTTPExchange(request)) self.assertEqual(request.headers['Header-Name'], 'FOO')
def test_write_something_to_queue(self): add_rule(Criteria(path='/quux'), Actions(set_redis_queue='test-queue')) yield self.redis.connect() yield self.redis.call('DEL', 'test-queue') yield self.http_client.fetch(self.get_url('/quux'), raise_error=False) result = yield self.redis.call('BRPOP', 'test-queue', 1) data = json.loads(result[1].decode()) self.assertEqual(data['path'], '/quux') self.assertEqual(data['extra']['response_key'], "thr:queue:response:%s" % self.response_key)
def test_coroutine_action(self): Rules.reset() @gen.coroutine def coroutine_action(exchange): yield gen.sleep(0.1) exchange.set_status_code(202) add_rule(Criteria(path='/quux'), Actions(custom_input=coroutine_action, set_redis_queue='test-queue'), stop=1) response = yield self.http_client.fetch(self.get_url('/quux')) self.assertEqual(response.code, 202)
def test_matching_coroutine_rule(self): @tornado.gen.coroutine def coroutine_rule(request): yield tornado.gen.maybe_future(None) raise tornado.gen.Return(False) add_rule(Criteria(path=coroutine_rule), Actions(set_redis_queue='no-match'), stop=1) add_rule(Criteria(path='/quux'), Actions(set_redis_queue='test-queue')) yield self.http_client.fetch(self.get_url('/quux'), raise_error=False) yield self.redis.connect() result = yield self.redis.call('BRPOP', 'test-queue', 1) data = json.loads(result[1].decode()) self.assertEqual(data['path'], '/quux')
def test_set_redis_queue_based_on_callable(self): def callback_false(request): return False def callback_true(request): return True add_rule(Criteria(custom=callback_false), Actions(set_redis_queue='not-this-one'), stop=1) add_rule(Criteria(custom=callback_true), Actions(set_redis_queue='yes-this-one')) request = HTTPServerRequest(method='GET', uri='/foo') exchange = HTTPExchange(request) Rules.execute_input_actions(exchange) self.assertEqual(exchange.redis_queue, 'yes-this-one')
from tornado import gen from thr.http2redis.rules import add_rule, Criteria, Actions @gen.coroutine def coroutine_set_status_code(exchange): yield gen.sleep(0.1) exchange.set_status_code(201) add_rule(Criteria(path='/foo'), Actions(custom_input=coroutine_set_status_code)) add_rule(Criteria(path='/bar'), Actions(set_status_code=202))
def add_basic_rules(self): add_rule(Criteria(path="/baz"), Actions(set_redis_queue="null")) add_rule(Criteria(path='/foo'), Actions(set_status_code=201)) add_rule(Criteria(path='/bar'), Actions(set_status_code=202))
def test_set_redis_queue_based_on_path(self): add_rule(Criteria(path='/foo'), Actions(set_redis_queue='test-queue')) request = HTTPServerRequest(method='GET', uri='/foo') exchange = HTTPExchange(request) Rules.execute_input_actions(exchange) self.assertEqual(exchange.redis_queue, 'test-queue')
def test_add_rule(self): add_rule(Criteria(), Actions()) self.assertEqual(Rules.count(), 1)
from thr.http2redis.rules import add_rule, Criteria, Actions # Match any incoming request and push it to thr:queue:hello add_rule(Criteria(), Actions(set_redis_queue='thr:queue:hello'))