mirror of https://github.com/huashengdun/webssh
Sheng
7 years ago
1 changed files with 8 additions and 19 deletions
@ -1,50 +1,39 @@ |
|||||||
import unittest |
import unittest |
||||||
import sys |
import sys |
||||||
|
|
||||||
|
from tornado.httputil import HTTPServerRequest |
||||||
from handler import MixinHandler |
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): |
class TestMixinHandler(unittest.TestCase): |
||||||
|
|
||||||
def test_get_real_client_addr_without_nginx_config(self): |
def test_get_real_client_addr_without_nginx_config(self): |
||||||
handler = MixinHandler() |
handler = MixinHandler() |
||||||
handler.request = RequestMock() |
handler.request = HTTPServerRequest(uri='/') |
||||||
self.assertIsNone(handler.get_real_client_addr()) |
self.assertIsNone(handler.get_real_client_addr()) |
||||||
|
|
||||||
def test_get_real_client_addr_with_correct_nginx_config(self): |
def test_get_real_client_addr_with_correct_nginx_config(self): |
||||||
handler = MixinHandler() |
handler = MixinHandler() |
||||||
handler.request = RequestMock() |
handler.request = HTTPServerRequest(uri='/') |
||||||
|
|
||||||
ip = '127.0.0.1' |
ip = '127.0.0.1' |
||||||
handler.request.set_ip(ip) |
handler.request.headers.add('X-Real-Ip', ip) |
||||||
handler.request.set_port('12345') |
handler.request.headers.add('X-Real-Port', '12345') |
||||||
self.assertEqual(handler.get_real_client_addr(), (ip, 12345)) |
self.assertEqual(handler.get_real_client_addr(), (ip, 12345)) |
||||||
|
|
||||||
@unittest.skipIf(sys.version_info < (3,), |
@unittest.skipIf(sys.version_info < (3,), |
||||||
reason='assertLogs not supported in Python 2') |
reason='assertLogs not supported in Python 2') |
||||||
def test_get_real_client_addr_with_bad_nginx_config(self): |
def test_get_real_client_addr_with_bad_nginx_config(self): |
||||||
handler = MixinHandler() |
handler = MixinHandler() |
||||||
handler.request = RequestMock() |
handler.request = HTTPServerRequest(uri='/') |
||||||
|
|
||||||
ip = '127.0.0.1' |
ip = '127.0.0.1' |
||||||
handler.request.set_ip(ip) |
handler.request.headers.add('X-Real-Ip', ip) |
||||||
with self.assertLogs() as cm: |
with self.assertLogs() as cm: |
||||||
handler.get_real_client_addr() |
handler.get_real_client_addr() |
||||||
self.assertEqual(cm.output, ['WARNING:root:Bad nginx configuration.']) |
self.assertEqual(cm.output, ['WARNING:root:Bad nginx configuration.']) |
||||||
|
|
||||||
handler.request.set_port('12345x') |
handler.request.headers.add('X-Real-Port', '12345x') |
||||||
with self.assertLogs() as cm: |
with self.assertLogs() as cm: |
||||||
handler.get_real_client_addr() |
handler.get_real_client_addr() |
||||||
self.assertEqual(cm.output, ['WARNING:root:Bad nginx configuration.']) |
self.assertEqual(cm.output, ['WARNING:root:Bad nginx configuration.']) |
||||||
|
Loading…
Reference in new issue