2020-08-11 20:11:50 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
proxy.py
|
|
|
|
~~~~~~~~
|
|
|
|
⚡⚡⚡ Fast, Lightweight, Pluggable, TLS interception capable proxy server focused on
|
|
|
|
Network monitoring, controls & Application development, testing, debugging.
|
|
|
|
|
|
|
|
:copyright: (c) 2013-present by Abhinav Singh and contributors.
|
|
|
|
:license: BSD, see LICENSE for more details.
|
|
|
|
"""
|
|
|
|
import unittest
|
|
|
|
|
2020-08-11 20:14:51 +00:00
|
|
|
from typing import List, Dict
|
|
|
|
|
2020-10-03 10:55:43 +00:00
|
|
|
from proxy.proxy import Proxy
|
2020-08-11 20:11:50 +00:00
|
|
|
from proxy.http.proxy import HttpProxyPlugin
|
|
|
|
from proxy.plugin import CacheResponsesPlugin
|
|
|
|
from proxy.plugin import FilterByUpstreamHostPlugin
|
|
|
|
|
2020-08-11 20:14:51 +00:00
|
|
|
|
2020-08-11 20:11:50 +00:00
|
|
|
class TestFlags(unittest.TestCase):
|
2020-08-11 20:14:51 +00:00
|
|
|
def assert_plugins(self, expected: Dict[str, List[type]]) -> None:
|
2020-08-11 20:11:50 +00:00
|
|
|
for k in expected:
|
|
|
|
self.assertIn(k.encode(), self.flags.plugins)
|
|
|
|
for p in expected[k]:
|
|
|
|
self.assertIn(p, self.flags.plugins[k.encode()])
|
2020-10-03 10:55:43 +00:00
|
|
|
self.assertEqual(
|
|
|
|
len([o for o in self.flags.plugins[k.encode()] if o == p]), 1)
|
2020-08-11 20:11:50 +00:00
|
|
|
|
2020-08-11 20:14:51 +00:00
|
|
|
def test_load_plugin_from_bytes(self) -> None:
|
2020-10-03 10:55:43 +00:00
|
|
|
self.flags = Proxy.initialize([], plugins=[
|
2020-08-11 20:11:50 +00:00
|
|
|
b'proxy.plugin.CacheResponsesPlugin',
|
|
|
|
])
|
|
|
|
self.assert_plugins({'HttpProxyBasePlugin': [CacheResponsesPlugin]})
|
|
|
|
|
2020-08-11 20:14:51 +00:00
|
|
|
def test_load_plugins_from_bytes(self) -> None:
|
2020-10-03 10:55:43 +00:00
|
|
|
self.flags = Proxy.initialize([], plugins=[
|
2020-08-11 20:11:50 +00:00
|
|
|
b'proxy.plugin.CacheResponsesPlugin',
|
|
|
|
b'proxy.plugin.FilterByUpstreamHostPlugin',
|
|
|
|
])
|
|
|
|
self.assert_plugins({'HttpProxyBasePlugin': [
|
|
|
|
CacheResponsesPlugin,
|
|
|
|
FilterByUpstreamHostPlugin,
|
|
|
|
]})
|
|
|
|
|
2020-08-11 20:14:51 +00:00
|
|
|
def test_load_plugin_from_args(self) -> None:
|
2020-10-03 10:55:43 +00:00
|
|
|
self.flags = Proxy.initialize([
|
2020-08-11 20:11:50 +00:00
|
|
|
'--plugins', 'proxy.plugin.CacheResponsesPlugin',
|
|
|
|
])
|
|
|
|
self.assert_plugins({'HttpProxyBasePlugin': [CacheResponsesPlugin]})
|
|
|
|
|
2020-08-11 20:14:51 +00:00
|
|
|
def test_load_plugins_from_args(self) -> None:
|
2020-10-03 10:55:43 +00:00
|
|
|
self.flags = Proxy.initialize([
|
2020-08-11 20:11:50 +00:00
|
|
|
'--plugins', 'proxy.plugin.CacheResponsesPlugin,proxy.plugin.FilterByUpstreamHostPlugin',
|
|
|
|
])
|
|
|
|
self.assert_plugins({'HttpProxyBasePlugin': [
|
|
|
|
CacheResponsesPlugin,
|
|
|
|
FilterByUpstreamHostPlugin,
|
|
|
|
]})
|
|
|
|
|
2020-08-11 20:14:51 +00:00
|
|
|
def test_load_plugin_from_class(self) -> None:
|
2020-10-03 10:55:43 +00:00
|
|
|
self.flags = Proxy.initialize([], plugins=[
|
2020-08-11 20:11:50 +00:00
|
|
|
CacheResponsesPlugin,
|
|
|
|
])
|
|
|
|
self.assert_plugins({'HttpProxyBasePlugin': [CacheResponsesPlugin]})
|
|
|
|
|
2020-08-11 20:14:51 +00:00
|
|
|
def test_load_plugins_from_class(self) -> None:
|
2020-10-03 10:55:43 +00:00
|
|
|
self.flags = Proxy.initialize([], plugins=[
|
2020-08-11 20:11:50 +00:00
|
|
|
CacheResponsesPlugin,
|
|
|
|
FilterByUpstreamHostPlugin,
|
|
|
|
])
|
|
|
|
self.assert_plugins({'HttpProxyBasePlugin': [
|
|
|
|
CacheResponsesPlugin,
|
|
|
|
FilterByUpstreamHostPlugin,
|
|
|
|
]})
|
|
|
|
|
2020-08-11 20:14:51 +00:00
|
|
|
def test_load_plugins_from_bytes_and_class(self) -> None:
|
2020-10-03 10:55:43 +00:00
|
|
|
self.flags = Proxy.initialize([], plugins=[
|
2020-08-11 20:11:50 +00:00
|
|
|
CacheResponsesPlugin,
|
|
|
|
b'proxy.plugin.FilterByUpstreamHostPlugin',
|
|
|
|
])
|
|
|
|
self.assert_plugins({'HttpProxyBasePlugin': [
|
|
|
|
CacheResponsesPlugin,
|
|
|
|
FilterByUpstreamHostPlugin,
|
|
|
|
]})
|
|
|
|
|
2020-08-11 20:14:51 +00:00
|
|
|
def test_unique_plugin_from_bytes(self) -> None:
|
2020-10-03 10:55:43 +00:00
|
|
|
self.flags = Proxy.initialize([], plugins=[
|
2020-08-11 20:11:50 +00:00
|
|
|
b'proxy.http.proxy.HttpProxyPlugin',
|
|
|
|
])
|
|
|
|
self.assert_plugins({'HttpProtocolHandlerPlugin': [
|
|
|
|
HttpProxyPlugin,
|
|
|
|
]})
|
|
|
|
|
2020-08-11 20:14:51 +00:00
|
|
|
def test_unique_plugin_from_args(self) -> None:
|
2020-10-03 10:55:43 +00:00
|
|
|
self.flags = Proxy.initialize([
|
2020-08-11 20:11:50 +00:00
|
|
|
'--plugins', 'proxy.http.proxy.HttpProxyPlugin',
|
|
|
|
])
|
|
|
|
self.assert_plugins({'HttpProtocolHandlerPlugin': [
|
|
|
|
HttpProxyPlugin,
|
|
|
|
]})
|
|
|
|
|
2020-08-11 20:14:51 +00:00
|
|
|
def test_unique_plugin_from_class(self) -> None:
|
2020-10-03 10:55:43 +00:00
|
|
|
self.flags = Proxy.initialize([], plugins=[
|
2020-08-11 20:11:50 +00:00
|
|
|
HttpProxyPlugin,
|
|
|
|
])
|
|
|
|
self.assert_plugins({'HttpProtocolHandlerPlugin': [
|
|
|
|
HttpProxyPlugin,
|
|
|
|
]})
|
|
|
|
|
2020-08-11 20:14:51 +00:00
|
|
|
|
2020-08-11 20:11:50 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|