# Path: reflectrpc/testing.py
# class ServerRunner(object):
#     """
#     Runs a server program in a subprocess and allows to stop it again
#     """
#     def __init__(self, path, port):
#         self.directory = os.path.dirname(path)
#         self.server_program = os.path.basename(path)
#         self.host = 'localhost'
#         self.port = port
#         self.pid = None
#         self.timeout = 5
# 
#     def run(self):
#         # we don't fork before we know that the TCP port/UNIX socket is free
#         if isinstance(self.port, int):
#             wait_for_free_port(self.host, self.port, self.timeout)
#         else:
#             wait_for_unix_socket_gone(self.port, self.timeout)
# 
#         pid = os.fork()
# 
#         if not pid:
#             # child
#             os.chdir(self.directory)
# 
#             if self.server_program.endswith('.py'):
#                 python = sys.executable
#                 os.execl(python, python, self.server_program)
#             else:
#                 os.execl(self.server_program, self.server_program)
#         else:
#             # parent
#             self.pid = pid
# 
#             if isinstance(self.port, int):
#                 wait_for_tcp_port_in_use(self.host, self.port, self.timeout)
#             else:
#                 wait_for_unix_socket_in_use(self.port, self.timeout)
# 
#     def stop(self):
#         os.kill(self.pid, signal.SIGINT)
#         os.waitpid(self.pid, 0)
# 
#         if isinstance(self.port, int):
#             wait_for_free_port(self.host, self.port, self.timeout)
#         else:
#             wait_for_unix_socket_gone(self.port, self.timeout)


from builtins import bytes, dict, list, int, float, str
from reflectrpc.testing import ServerRunner
import os
import sys
import unittest
import pexpect


sys.path.append('..')


# Generic tests for ReflectRPC command-line utils
class CmdlineTests(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super(CmdlineTests, self).__init__(*args, **kwargs)
        self.cmdline_programs = ['rpcsh', 'rpcdoc', 'rpcgencode']

    def test_cmdline_expect_connection_fails(self):
        for cmd in self.cmdline_programs:
            # connect although no server is running
            try:
                python = sys.executable
                outfile = ''
                if cmd != 'rpcsh':
                    outfile = 'outfile.html'

                child = pexpect.spawn('%s ../%s localhost 5500 %s' % (python, cmd, outfile))

                child.expect('NetworkError: \[Errno \d+\] Connection refused\r\n')
                child.expect('\r\n')
                child.expect('Failed to connect to localhost on TCP port 5500\r\n')
            finally:
                child.close(True)

            # connect to a TLS server without enabling TLS
            try:

# Path: reflectrpc/testing.py
# class ServerRunner(object):
#     """
#     Runs a server program in a subprocess and allows to stop it again
#     """
#     def __init__(self, path, port):
#         self.directory = os.path.dirname(path)
#         self.server_program = os.path.basename(path)
#         self.host = 'localhost'
#         self.port = port
#         self.pid = None
#         self.timeout = 5
# 
#     def run(self):
#         # we don't fork before we know that the TCP port/UNIX socket is free
#         if isinstance(self.port, int):
#             wait_for_free_port(self.host, self.port, self.timeout)
#         else:
#             wait_for_unix_socket_gone(self.port, self.timeout)
# 
#         pid = os.fork()
# 
#         if not pid:
#             # child
#             os.chdir(self.directory)
# 
#             if self.server_program.endswith('.py'):
#                 python = sys.executable
#                 os.execl(python, python, self.server_program)
#             else:
#                 os.execl(self.server_program, self.server_program)
#         else:
#             # parent
#             self.pid = pid
# 
#             if isinstance(self.port, int):
#                 wait_for_tcp_port_in_use(self.host, self.port, self.timeout)
#             else:
#                 wait_for_unix_socket_in_use(self.port, self.timeout)
# 
#     def stop(self):
#         os.kill(self.pid, signal.SIGINT)
#         os.waitpid(self.pid, 0)
# 
#         if isinstance(self.port, int):
#             wait_for_free_port(self.host, self.port, self.timeout)
#         else:
#             wait_for_unix_socket_gone(self.port, self.timeout)


# Path: tests/cmdline-tests.py
from builtins import bytes, dict, list, int, float, str
from reflectrpc.testing import ServerRunner
import os
import sys
import unittest
import pexpect



sys.path.append('..')


# Generic tests for ReflectRPC command-line utils
class CmdlineTests(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super(CmdlineTests, self).__init__(*args, **kwargs)
        self.cmdline_programs = ['rpcsh', 'rpcdoc', 'rpcgencode']

    def test_cmdline_expect_connection_fails(self):
        for cmd in self.cmdline_programs:
            # connect although no server is running
            try:
                python = sys.executable
                outfile = ''
                if cmd != 'rpcsh':
                    outfile = 'outfile.html'

                child = pexpect.spawn('%s ../%s localhost 5500 %s' % (python, cmd, outfile))

                child.expect('NetworkError: \[Errno \d+\] Connection refused\r\n')
                child.expect('\r\n')
                child.expect('Failed to connect to localhost on TCP port 5500\r\n')
            finally:
                child.close(True)

            # connect to a TLS server without enabling TLS
            try:

                server = ServerRunner('../examples/servertls.py', 5500)