def test_del_input_header(self): headers = HTTPHeaders() headers.add("Header-Name", "header value1") request = HTTPServerRequest(method='GET', uri='/', headers=headers) exchange = HTTPExchange(request) actions = Actions(del_input_header="Header-Name") actions.execute_input_actions(exchange) keys = list(exchange.request.headers.keys()) self.assertEquals(len(keys), 0) actions = Actions(del_input_header="Header-Name2") actions.execute_input_actions(exchange)
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)
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'))
def test_set_output_body(self): request = HTTPServerRequest(method='GET', uri='/') exchange = HTTPExchange(request) actions = Actions(set_output_body=b"foobar") actions.execute_output_actions(exchange) self.assertEqual(exchange.response.body, b"foobar")
def test_set_status_code(self): request = HTTPServerRequest(method='GET', uri='/') exchange = HTTPExchange(request) actions = Actions(set_status_code=201) actions.execute_input_actions(exchange) self.assertEqual(exchange.response.status_code, 201)
def test_set_redis_port(self): request = HTTPServerRequest(method='GET', uri='/') exchange = HTTPExchange(request) actions = Actions(set_redis_port=1234) actions.execute_input_actions(exchange) self.assertEqual(exchange.redis_port, 1234)
def test_set_remote_ip(self): request = HTTPServerRequest(method='GET', uri='/') exchange = HTTPExchange(request) actions = Actions(set_remote_ip="1.2.3.4") actions.execute_input_actions(exchange) self.assertEqual(exchange.request.remote_ip, "1.2.3.4")
def test_set_host(self): request = HTTPServerRequest(method='GET', uri='/') exchange = HTTPExchange(request) actions = Actions(set_host="foobar:8080") actions.execute_input_actions(exchange) self.assertEqual(exchange.request.host, "foobar:8080")
def test_set_method(self): request = HTTPServerRequest(method='GET', uri='/') exchange = HTTPExchange(request) actions = Actions(set_method="POST") actions.execute_input_actions(exchange) self.assertEqual(exchange.request.method, "POST")
def test_set_path(self): request = HTTPServerRequest(method='GET', uri='/') exchange = HTTPExchange(request) actions = Actions(set_path="/foo") actions.execute_input_actions(exchange) self.assertEqual(exchange.request.path, "/foo")