mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 21:35:11 +01:00
pytest: Implement method mocking for ProxiedBitcoinRpc
This commit is contained in:
parent
e132dffa0b
commit
88186020e0
@ -29,11 +29,20 @@ class ProxiedBitcoinD(BitcoinD):
|
||||
self.app = Flask("BitcoindProxy")
|
||||
self.app.add_url_rule("/", "API entrypoint", self.proxy, methods=['POST'])
|
||||
self.proxyport = proxyport
|
||||
self.mocks = {}
|
||||
|
||||
def proxy(self):
|
||||
r = json.loads(request.data.decode('ASCII'))
|
||||
conf_file = os.path.join(self.bitcoin_dir, 'bitcoin.conf')
|
||||
brpc = BitcoinProxy(btc_conf_file=conf_file)
|
||||
method = r['method']
|
||||
|
||||
# If we have set a mock for this method reply with that instead of
|
||||
# forwarding the request.
|
||||
if method in self.mocks and type(method) == dict:
|
||||
return self.mocks[method]
|
||||
elif method in self.mocks and callable(self.mocks[method]):
|
||||
return self.mocks[method](r)
|
||||
|
||||
try:
|
||||
reply = {
|
||||
@ -72,6 +81,19 @@ class ProxiedBitcoinD(BitcoinD):
|
||||
self.server.stop()
|
||||
self.proxy_thread.join()
|
||||
|
||||
def mock_rpc(self, method, response=None):
|
||||
"""Mock the response to a future RPC call of @method
|
||||
|
||||
The response can either be a dict with the full JSON-RPC response, or a
|
||||
function that returns such a response. If the response is None the mock
|
||||
is removed and future calls will be passed through to bitcoind again.
|
||||
|
||||
"""
|
||||
if response is not None:
|
||||
self.mocks[method] = response
|
||||
elif method in self.mocks:
|
||||
del self.mocks[method]
|
||||
|
||||
|
||||
# The main entrypoint is mainly used to test the proxy. It is not used during
|
||||
# lightningd testing.
|
||||
|
Loading…
Reference in New Issue
Block a user