# Path: fdep/servers/jsonrpc.py
# class JSONRPCServer(RPCServer):
#     """Implement a JSONRPC server."""
# 
#     def register_functions(self, func_pairs):
#         for name, func in func_pairs:
#             dispatcher[name] = self.wrap_function(name, func)
# 
#     def serve_forever(self, **kwargs):
# 
#         if kwargs.get('username') and kwargs.get('password'):
#             JSONRPCRequestHandler.set_account(kwargs['username'], kwargs['password'], kwargs.get('temp_password'))
# 
#         self.server = HTTPServer(('0.0.0.0', kwargs['port']), JSONRPCRequestHandler)
# 
#         try:
#             self.server.serve_forever()
#         except KeyboardInterrupt:
#             pass
# 
#     def shutdown(self):
#         if not getattr(self, 'server', None):
#             raise Exception("The server has never been started.")
#         self.server.shutdown()
#         self.server.server_close()
# 
# Path: fdep/servers/xmlrpc.py
# class XMLRPCServer(RPCServer):
#     """Implement a XMLRPC server."""
# 
#     def register_functions(self, func_pairs):
#         self.func_pairs = func_pairs
# 
#     def serve_forever(self, **kwargs):
# 
#         if kwargs.get('username') and kwargs.get('password'):
#             XMLRPCRequestHandler.set_account(kwargs['username'], kwargs['password'], kwargs.get('temp_password'))
# 
#         self.server = SimpleXMLRPCServer(('0.0.0.0', kwargs['port']), requestHandler=XMLRPCRequestHandler)
# 
#         for name, func in self.func_pairs:
#             self.server.register_function(
#                 self.wrap_function(name, func),
#                 name=name
#             )
#         self.server.register_multicall_functions()
# 
#         try:
#             self.server.serve_forever()
#         except KeyboardInterrupt:
#             pass
# 
#     def shutdown(self):
#         if not getattr(self, 'server', None):
#             raise Exception("The server has never been started.")
#         self.server.shutdown()
#         self.server.server_close()


import json
import time
import pytest
import requests
from fdep.servers.jsonrpc import JSONRPCServer
from fdep.servers.xmlrpc import XMLRPCServer
from six.moves import xmlrpc_client
def test_basic_jsonrpc_server(fdep_serve):
    server = setup_test_server(JSONRPCServer)
    th = fdep_serve(server, port=14244)
    th.start()
    time.sleep(1)

    payload = {
        "method": "test",
        "params": [],
        "jsonrpc": "2.0",
        "id": 0
    }
    headers = {
        'content-type': 'application/json'
    }

    res = requests.post(
        'http://127.0.0.1:14244/', data=json.dumps(payload),
        headers=headers
    ).json()

    assert res['result'] is True
    assert res['jsonrpc']
    assert res['id'] == 0

    th.stop()
    th.join()


def test_jsonrpc_server_auth(fdep_serve):

# Path: fdep/servers/jsonrpc.py
# class JSONRPCServer(RPCServer):
#     """Implement a JSONRPC server."""
# 
#     def register_functions(self, func_pairs):
#         for name, func in func_pairs:
#             dispatcher[name] = self.wrap_function(name, func)
# 
#     def serve_forever(self, **kwargs):
# 
#         if kwargs.get('username') and kwargs.get('password'):
#             JSONRPCRequestHandler.set_account(kwargs['username'], kwargs['password'], kwargs.get('temp_password'))
# 
#         self.server = HTTPServer(('0.0.0.0', kwargs['port']), JSONRPCRequestHandler)
# 
#         try:
#             self.server.serve_forever()
#         except KeyboardInterrupt:
#             pass
# 
#     def shutdown(self):
#         if not getattr(self, 'server', None):
#             raise Exception("The server has never been started.")
#         self.server.shutdown()
#         self.server.server_close()
# 
# Path: fdep/servers/xmlrpc.py
# class XMLRPCServer(RPCServer):
#     """Implement a XMLRPC server."""
# 
#     def register_functions(self, func_pairs):
#         self.func_pairs = func_pairs
# 
#     def serve_forever(self, **kwargs):
# 
#         if kwargs.get('username') and kwargs.get('password'):
#             XMLRPCRequestHandler.set_account(kwargs['username'], kwargs['password'], kwargs.get('temp_password'))
# 
#         self.server = SimpleXMLRPCServer(('0.0.0.0', kwargs['port']), requestHandler=XMLRPCRequestHandler)
# 
#         for name, func in self.func_pairs:
#             self.server.register_function(
#                 self.wrap_function(name, func),
#                 name=name
#             )
#         self.server.register_multicall_functions()
# 
#         try:
#             self.server.serve_forever()
#         except KeyboardInterrupt:
#             pass
# 
#     def shutdown(self):
#         if not getattr(self, 'server', None):
#             raise Exception("The server has never been started.")
#         self.server.shutdown()
#         self.server.server_close()


# Path: tests/test_servers.py
import json
import time
import pytest
import requests
from fdep.servers.jsonrpc import JSONRPCServer
from fdep.servers.xmlrpc import XMLRPCServer
from six.moves import xmlrpc_client

def test_basic_jsonrpc_server(fdep_serve):
    server = setup_test_server(JSONRPCServer)
    th = fdep_serve(server, port=14244)
    th.start()
    time.sleep(1)

    payload = {
        "method": "test",
        "params": [],
        "jsonrpc": "2.0",
        "id": 0
    }
    headers = {
        'content-type': 'application/json'
    }

    res = requests.post(
        'http://127.0.0.1:14244/', data=json.dumps(payload),
        headers=headers
    ).json()

    assert res['result'] is True
    assert res['jsonrpc']
    assert res['id'] == 0

    th.stop()
    th.join()


def test_jsonrpc_server_auth(fdep_serve):

    server = setup_test_server(JSONRPCServer)