def test_parse_request(self): server = Server() request = server.parse_request("""GET /test_running.php?a=1""") assert "/test_running.php" == request.filename assert {"a": "1"} == request.get
def test_running(self, socket_mock, inetaddress_mock, fromfd): code = """print $_GET['x'];""" filename = self.store(code) folder = os.path.dirname(filename) server = Server(folder) server.listen(0) # socket init in open_socket() socket_mock.assert_called_with(2, 1) inetaddress_mock.assert_called_with('', 0) socket = socket_mock.return_value inetaddress = inetaddress_mock.return_value socket.bind.assert_called_with(inetaddress) # run in wait_for_connection() class IpAddress(object): def get_host(self): return '127.0.0.1' def get_port(self): return 2133 addr = IpAddress() socket.accept.return_value = (1, addr) request = """GET /test_running.php?x=HelloWorld""" class ClientSocket(object): pass client_socket = ClientSocket() client_socket.recv = MagicMock(return_value=request) client_socket.send = MagicMock() client_socket.close = MagicMock() fromfd.return_value = client_socket server.run() # client_socket instantiation in wait_for_connection() fromfd.assert_called_with(1, 2, 1) # response sent in return_response() client_socket.send.assert_called_with("""HTTP/1.1 200 OK Content-Type: text/html;charset=utf-8 Content-Length: 10 Server: PyHP-Server Connection: close HelloWorld""") # close in connection_close() client_socket.close.assert_called_with()
def main(argv): filename = '' print_bytecode = False print_ast = False server = False server_port = 8080 i = 1 while i < len(argv): arg = argv[i] if arg.startswith('-'): if arg == '--bytecode': print_bytecode = True elif arg == '--ast': print_ast = True elif arg == '--server': if i == len(argv) - 1: print "--server requires an int" return 1 try: server_port = int(argv[i + 1]) except ValueError: print "--server requires an int" return 1 i += 1 server = True else: print "Unknown parameter %s" % arg return 1 else: filename = arg break i += 1 source = '' if server is False: if filename == '': print 'Required parameter filename missing' return 1 try: source = read_file(filename) except OSError: print 'File not found %s' % filename return 1 if print_ast: print ast(source).str() return 0 elif print_bytecode: print bytecode(filename, source).str() return 0 elif server: server = Server(os.getcwd()) try: server.listen(server_port) except Exception: print("Failed to acquire a socket for port %s " % server_port) return 1 print 'Listening on http://localhost:%d' % (server_port, ) print 'Document root is %s' % server.root print 'Press Ctrl-C to quit.' while True: server.run() return 0 else: return run(filename, source)
def main(argv): filename = '' print_bytecode = False print_ast = False server = False server_port = 8080 i = 1 while i < len(argv): arg = argv[i] if arg.startswith('-'): if arg == '--bytecode': print_bytecode = True elif arg == '--ast': print_ast = True elif arg == '--server': if i == len(argv) - 1: print "--server requires an int" return 1 try: server_port = int(argv[i + 1]) except ValueError: print "--server requires an int" return 1 i += 1 server = True else: print "Unknown parameter %s" % arg return 1 else: filename = arg break i += 1 source = '' if server is False: if filename == '': print 'Required parameter filename missing' return 1 try: source = read_file(filename) except OSError: print 'File not found %s' % filename return 1 if print_ast: print ast(source).str() return 0 elif print_bytecode: print bytecode(filename, source).str() return 0 elif server: server = Server(os.getcwd()) try: server.listen(server_port) except Exception: print("Failed to acquire a socket for port %s " % server_port) return 1 print 'Listening on http://localhost:%d' % (server_port,) print 'Document root is %s' % server.root print 'Press Ctrl-C to quit.' while True: server.run() return 0 else: return run(filename, source)