Skip to content

Commit 5a69f69

Browse files
authored
Merge pull request #656 from eynhaender/master
Extend and move endpoint tests.
2 parents 93ffd47 + d79eeab commit 5a69f69

6 files changed

Lines changed: 466 additions & 8 deletions

File tree

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ This directory contains Python-based integration tests for all libbitcoin-server
88

99
| Interface | Protocol | Test File | Status |
1010
|-----------|----------|-----------|--------|
11-
| **Native REST** | HTTP/S + JSON | `test_native.py` | 🔧 In Progress |
12-
| **bitcoind RPC** | HTTP/S + JSON-RPC 2.0 | `test_bitcoind_rpc.py` | 🔧 In Progress |
13-
| **Electrum** | TCP + JSON-RPC 2.0 | `test_electrum.py` | 🔧 In Progress |
11+
| **Native REST** | HTTP/S + JSON | `test_native.py` | ✅ Active |
12+
| **bitcoind RPC** | HTTP/S + JSON-RPC 2.0 | `test_bitcoind_rpc.py` | ✅ Active |
13+
| **Electrum** | TCP + JSON-RPC 2.0 | `test_electrum.py` | ✅ Active |
1414
| **bitcoind REST** | HTTP/S + JSON/Binary | `test_bitcoind_rest.py` | 🚧 Planned |
1515
| **Stratum v1** | TCP + JSON-RPC 1.0 | `test_stratum_v1.py` | 🚧 Planned |
1616
| **Stratum v2** | TCP + Binary | `test_stratum_v2.py` | 🚧 Planned |
@@ -231,10 +231,10 @@ pytest test_bitcoind_rpc.py -k "getblock"
231231
Tests Electrum Protocol 1.4.2 JSON-RPC over TCP.
232232

233233
**Coverage:**
234-
- ✅ Server methods (version, banner, features, ping)
235-
- ✅ Blockchain methods (headers, estimatefee, relayfee)
236-
- ✅ Scripthash methods (balance, history, mempool, listunspent, subscribe)
237-
- ✅ Transaction methods (get, id_from_pos)
234+
- ✅ Server methods (version, banner, features, ping, add_peer, donation_address, peers.subscribe)
235+
- ✅ Blockchain methods (block.header, block.headers, headers.subscribe, estimatefee, relayfee)
236+
- ✅ Scripthash methods (balance, history, mempool, listunspent, subscribe, unsubscribe)
237+
- ✅ Transaction methods (get, get_merkle, id_from_pos, broadcast)
238238
- ✅ Mempool methods (fee_histogram)
239239

240240
**Example test runs:**
@@ -360,6 +360,32 @@ ModuleNotFoundError: No module named 'utils'
360360
```
361361
**Solution:** Run pytest from the `test/endpoints/` directory or install package in development mode.
362362

363+
### Request/Response Debug Output
364+
365+
Each test module supports a dedicated environment variable that enables pretty-printed JSON logging of every request and response:
366+
367+
| Test file | Variable | Prints |
368+
|-----------|----------|--------|
369+
| `test_electrum.py` | `ELECTRUM_DEBUG=1` | `>>>` JSON-RPC payload / `<<<` response with elapsed time |
370+
| `test_native.py` | `NATIVE_DEBUG=1` | `>>> GET <url>` / `<<<` response JSON with elapsed time |
371+
| `test_bitcoind_rpc.py` | `BITCOIND_DEBUG=1` | `>>>` JSON-RPC payload / `<<<` response with elapsed time |
372+
373+
```bash
374+
# Electrum — pretty-print all requests and responses
375+
ELECTRUM_DEBUG=1 pytest test_electrum.py -s
376+
377+
# Native REST
378+
NATIVE_DEBUG=1 pytest test_native.py -s
379+
380+
# bitcoind RPC
381+
BITCOIND_DEBUG=1 pytest test_bitcoind_rpc.py -s
382+
383+
# Combine with -k to focus on a single test
384+
ELECTRUM_DEBUG=1 pytest test_electrum.py -s -k "block_headers_20000"
385+
```
386+
387+
> **Note:** Use `-s` (or `--capture=no`) together with the debug variable so pytest does not suppress stdout.
388+
363389
### Debug Mode
364390

365391
```bash
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
pytest test_bitcoind_rpc.py --bitcoind-auth --bitcoind-cookie=/path/to/.cookie
1111
"""
1212

13+
import os
14+
import time
1315
import pytest
1416
import requests
1517
import json
@@ -46,6 +48,10 @@ def send_rpc(
4648
"params": params if params is not None else [],
4749
}
4850

51+
if os.getenv("BITCOIND_DEBUG"):
52+
print(">>>", json.dumps(payload, indent=2), flush=True)
53+
54+
_t0 = time.monotonic()
4955
try:
5056
response = requests.post(
5157
config["url"],
@@ -57,12 +63,19 @@ def send_rpc(
5763
response.raise_for_status()
5864
except requests.exceptions.RequestException as e:
5965
raise RuntimeError(f"RPC connection error: {e}")
66+
_elapsed = time.monotonic() - _t0
6067

6168
try:
6269
data = response.json()
6370
except ValueError:
6471
raise RuntimeError("Invalid JSON response from RPC server")
6572

73+
if os.getenv("BITCOIND_DEBUG"):
74+
try:
75+
print(f"<<< {method} ({_elapsed * 1000:.1f} ms):", json.dumps(data, indent=2), flush=True)
76+
except Exception:
77+
pass
78+
6679
# Check for error in response
6780
if "error" in data and data["error"] is not None:
6881
error = data["error"]

0 commit comments

Comments
 (0)