From d1177bc2c593290e72cd0c59406a7e4a49561a98 Mon Sep 17 00:00:00 2001 From: Sheng Date: Wed, 25 Apr 2018 19:01:54 +0800 Subject: [PATCH] Updated handler.py and test_hanlder.py --- tests/test_handler.py | 35 ++++++++++++----------------------- webssh/handler.py | 2 ++ 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/tests/test_handler.py b/tests/test_handler.py index a7cc9f6..b7c487b 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -1,5 +1,4 @@ import unittest -import sys from tornado.httputil import HTTPServerRequest from handler import MixinHandler @@ -7,33 +6,23 @@ from handler import MixinHandler class TestMixinHandler(unittest.TestCase): - def test_get_real_client_addr_without_nginx_config(self): + def test_get_real_client_addr(self): handler = MixinHandler() handler.request = HTTPServerRequest(uri='/') self.assertIsNone(handler.get_real_client_addr()) - def test_get_real_client_addr_with_correct_nginx_config(self): - handler = MixinHandler() - handler.request = HTTPServerRequest(uri='/') - ip = '127.0.0.1' handler.request.headers.add('X-Real-Ip', ip) - handler.request.headers.add('X-Real-Port', '12345') - self.assertEqual(handler.get_real_client_addr(), (ip, 12345)) - - @unittest.skipIf(sys.version_info < (3,), - reason='assertLogs not supported in Python 2') - def test_get_real_client_addr_with_bad_nginx_config(self): - handler = MixinHandler() - handler.request = HTTPServerRequest(uri='/') - - ip = '127.0.0.1' - handler.request.headers.add('X-Real-Ip', ip) - with self.assertLogs() as cm: - handler.get_real_client_addr() - self.assertEqual(cm.output, ['WARNING:root:Bad nginx configuration.']) + self.assertEqual(handler.get_real_client_addr(), False) handler.request.headers.add('X-Real-Port', '12345x') - with self.assertLogs() as cm: - handler.get_real_client_addr() - self.assertEqual(cm.output, ['WARNING:root:Bad nginx configuration.']) + self.assertEqual(handler.get_real_client_addr(), False) + + handler.request.headers.update({'X-Real-Port': '12345'}) + self.assertEqual(handler.get_real_client_addr(), (ip, 12345)) + + handler.request.headers.update({'X-Real-ip': None}) + self.assertEqual(handler.get_real_client_addr(), False) + + handler.request.headers.update({'X-Real-Port': '12345x'}) + self.assertEqual(handler.get_real_client_addr(), False) diff --git a/webssh/handler.py b/webssh/handler.py index f6cabcc..ec23825 100644 --- a/webssh/handler.py +++ b/webssh/handler.py @@ -35,7 +35,9 @@ class MixinHandler(object): else: if ip: # does not validate ip and port here return (ip, port) + logging.warn('Bad nginx configuration.') + return False class IndexHandler(MixinHandler, tornado.web.RequestHandler):