Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions test/endpoints/README.md → endpoints/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ This directory contains Python-based integration tests for all libbitcoin-server

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

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

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

### Request/Response Debug Output

Each test module supports a dedicated environment variable that enables pretty-printed JSON logging of every request and response:

| Test file | Variable | Prints |
|-----------|----------|--------|
| `test_electrum.py` | `ELECTRUM_DEBUG=1` | `>>>` JSON-RPC payload / `<<<` response with elapsed time |
| `test_native.py` | `NATIVE_DEBUG=1` | `>>> GET <url>` / `<<<` response JSON with elapsed time |
| `test_bitcoind_rpc.py` | `BITCOIND_DEBUG=1` | `>>>` JSON-RPC payload / `<<<` response with elapsed time |

```bash
# Electrum — pretty-print all requests and responses
ELECTRUM_DEBUG=1 pytest test_electrum.py -s

# Native REST
NATIVE_DEBUG=1 pytest test_native.py -s

# bitcoind RPC
BITCOIND_DEBUG=1 pytest test_bitcoind_rpc.py -s

# Combine with -k to focus on a single test
ELECTRUM_DEBUG=1 pytest test_electrum.py -s -k "block_headers_20000"
```

> **Note:** Use `-s` (or `--capture=no`) together with the debug variable so pytest does not suppress stdout.

### Debug Mode

```bash
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
pytest test_bitcoind_rpc.py --bitcoind-auth --bitcoind-cookie=/path/to/.cookie
"""

import os
import time
import pytest
import requests
import json
Expand Down Expand Up @@ -46,6 +48,10 @@ def send_rpc(
"params": params if params is not None else [],
}

if os.getenv("BITCOIND_DEBUG"):
print(">>>", json.dumps(payload, indent=2), flush=True)

_t0 = time.monotonic()
try:
response = requests.post(
config["url"],
Expand All @@ -57,12 +63,19 @@ def send_rpc(
response.raise_for_status()
except requests.exceptions.RequestException as e:
raise RuntimeError(f"RPC connection error: {e}")
_elapsed = time.monotonic() - _t0

try:
data = response.json()
except ValueError:
raise RuntimeError("Invalid JSON response from RPC server")

if os.getenv("BITCOIND_DEBUG"):
try:
print(f"<<< {method} ({_elapsed * 1000:.1f} ms):", json.dumps(data, indent=2), flush=True)
except Exception:
pass

# Check for error in response
if "error" in data and data["error"] is not None:
error = data["error"]
Expand Down
Loading
Loading