From 42ac3daf87d5c4cf2961c147dca8ab2fd1a6d9e7 Mon Sep 17 00:00:00 2001 From: Sheng Date: Tue, 24 Apr 2018 10:23:45 +0800 Subject: [PATCH] Updated test_handler.py --- tests/test_handler.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/tests/test_handler.py b/tests/test_handler.py index 7a960d5..d63af32 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -1,4 +1,5 @@ import unittest +import sys from handler import MixinHandler @@ -17,11 +18,26 @@ class RequestMock(object): class TestMixinHandler(unittest.TestCase): - def test_get_real_client_addr(self): + def test_get_real_client_addr_without_nginx_config(self): handler = MixinHandler() handler.request = RequestMock() self.assertIsNone(handler.get_real_client_addr()) + def test_get_real_client_addr_with_correct_nginx_config(self): + handler = MixinHandler() + handler.request = RequestMock() + + ip = '127.0.0.1' + handler.request.set_ip(ip) + handler.request.set_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 = RequestMock() + ip = '127.0.0.1' handler.request.set_ip(ip) with self.assertLogs() as cm: @@ -32,6 +48,3 @@ class TestMixinHandler(unittest.TestCase): 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))