From acc3b47ec6d29e2e65525deaabff234a695e5bb4 Mon Sep 17 00:00:00 2001 From: Sheng Date: Tue, 24 Apr 2018 09:59:51 +0800 Subject: [PATCH] Added test_handler.py --- tests/test_handler.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tests/test_handler.py diff --git a/tests/test_handler.py b/tests/test_handler.py new file mode 100644 index 0000000..7a960d5 --- /dev/null +++ b/tests/test_handler.py @@ -0,0 +1,37 @@ +import unittest + +from handler import MixinHandler + + +class RequestMock(object): + + def __init__(self): + self.headers = {} + + def set_ip(self, ip): + self.headers['X-Real-Ip'] = ip + + def set_port(self, port): + self.headers['X-Real-Port'] = port + + +class TestMixinHandler(unittest.TestCase): + + def test_get_real_client_addr(self): + handler = MixinHandler() + handler.request = RequestMock() + self.assertIsNone(handler.get_real_client_addr()) + + ip = '127.0.0.1' + handler.request.set_ip(ip) + with self.assertLogs() as cm: + handler.get_real_client_addr() + self.assertEqual(cm.output, ['WARNING:root:Bad nginx configuration.']) + + handler.request.set_port('12345x') + with self.assertLogs() as cm: + handler.get_real_client_addr() + self.assertEqual(cm.output, ['WARNING:root:Bad nginx configuration.']) + + handler.request.set_port('12345') + self.assertEqual(handler.get_real_client_addr(), (ip, 12345))