def test_handle_request(self): mock_sock = mock.Mock() atr = { "settimeout.return_value": None, "recv.return_value": False, "close.return_value": None, "send.return_value": None } mock_sock.configure_mock(**atr) data = ProxyServer.receive(mock_sock) proxy_ = ProxyServer() asyncio.run(proxy_.handle_request(mock, self.request_connect))
async def main(args, test=False): log = logging.basicConfig(filename="proxy.log", level=logging.INFO) proxy = ProxyServer() try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) if args.port: s.bind(('127.0.0.1', args.port)) print("Working on localhost:", args.port) else: s.bind(('127.0.0.1', 8080)) print("Working on localhost:8080 ...") s.listen(200) logging.info("RUN\n") proxy.sockets.add(s) except Exception as msg: logging.info(str(msg)) sys.exit() while True: try: if not test: inputs, _, _ = select.select(proxy.sockets, [], []) else: inputs = test for sock in inputs: if sock == s: client, address = sock.accept() data = proxy.receive(client) await proxy.handle_request(client, data) else: try: data = proxy.receive(sock) if data: proxy.connections[sock].send(data) except Exception as msg: logging.info(str(msg)) if test: s.close() break except Exception as msg: client.close() logging.info(str(msg))
def test_connect(self): mock_client = mock.Mock() atr = {"send.return_value": None, "close.return_value": None} mock_client.configure_mock(**atr) proxy_ = ProxyServer() proxy_.handle_connect(mock_client, self.parsed_dict)
def test_get(self): mock_client = mock.Mock() atr = {"send.return_value": None, "close.return_value": None} mock_client.configure_mock(**atr) parsed = ProxyServer.parse_request(self.request_get.decode()) ProxyServer.handle_get(mock_client, parsed)
def test_request_parsing_get(self): parsed = ProxyServer.parse_request(self.request_get.decode()) self.assertEqual("GET", parsed["Type"])
def test_parsing_url(self): parsed = ProxyServer.parse_request(self.request_connect.decode()) self.assertEqual(parsed["URL"], self.parsed_dict["URL"])