Skip to content

Commit b34c08a

Browse files
committed
Update ureq
1 parent 461dda2 commit b34c08a

3 files changed

Lines changed: 129 additions & 48 deletions

File tree

Cargo.lock

Lines changed: 91 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ electrs_macros = { path = "electrs_macros", default-features = false }
8181
bitcoind = { version = "0.36", features = ["25_0"] }
8282
elementsd = { version = "0.11", features = ["22_1_1"] }
8383
electrumd = { version = "0.1.0", features = ["4_5_4"] }
84-
ureq = { version = "2.9", default-features = false, features = ["json"] }
84+
ureq = { version = "3.1", default-features = false, features = ["json"] }
8585
tempfile = "3.10"
8686
criterion = { version = "0.4", features = ["html_reports"] }
8787
bitcoin-test-data = { version = "*" }

tests/rest.rs

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use bitcoin::hex::FromHex;
33
use bitcoind::bitcoincore_rpc::RpcApi;
44
use serde_json::Value;
55
use std::collections::HashSet;
6-
use std::io::Read;
76
use std::net;
87

98
#[cfg(not(feature = "liquid"))]
@@ -15,16 +14,19 @@ pub mod common;
1514

1615
use common::Result;
1716

18-
fn get(rest_addr: net::SocketAddr, path: &str) -> std::result::Result<ureq::Response, ureq::Error> {
17+
fn get(
18+
rest_addr: net::SocketAddr,
19+
path: &str,
20+
) -> std::result::Result<ureq::http::Response<ureq::Body>, ureq::Error> {
1921
ureq::get(&format!("http://{}{}", rest_addr, path)).call()
2022
}
2123

2224
fn get_json(rest_addr: net::SocketAddr, path: &str) -> Result<Value> {
23-
Ok(get(rest_addr, path)?.into_json::<Value>()?)
25+
Ok(get(rest_addr, path)?.into_body().read_json()?)
2426
}
2527

2628
fn get_plain(rest_addr: net::SocketAddr, path: &str) -> Result<String> {
27-
Ok(get(rest_addr, path)?.into_string()?)
29+
Ok(get(rest_addr, path)?.into_body().read_to_string()?)
2830
}
2931

3032
#[test]
@@ -320,9 +322,9 @@ fn test_rest_block() -> Result<()> {
320322
}
321323

322324
// Test GET /block/:hash/raw
323-
let mut res = get(rest_addr, &format!("/block/{}/raw", blockhash))?.into_reader();
324-
let mut rest_rawblock = Vec::new();
325-
res.read_to_end(&mut rest_rawblock).unwrap();
325+
let rest_rawblock = get(rest_addr, &format!("/block/{}/raw", blockhash))?
326+
.into_body()
327+
.read_to_vec()?;
326328
let node_hexblock = // uses low-level call() to support Elements
327329
tester.call::<String>("getblock", &[blockhash.to_string().into(), 0.into()])?;
328330
assert_eq!(rest_rawblock, Vec::from_hex(&node_hexblock).unwrap());
@@ -397,13 +399,19 @@ fn test_rest_broadcast_tx() -> Result<()> {
397399
let txid = tester.send(&addr1, "9.9 BTC".parse().unwrap())?;
398400
let tx_hex = get_plain(rest_addr, &format!("/tx/{}/hex", txid))?;
399401
// Re-send the tx created by send(). It'll be accepted again since its still in the mempool.
400-
let broadcast1_resp = ureq::post(&format!("http://{}/tx", rest_addr)).send_string(&tx_hex)?;
402+
let broadcast1_resp = ureq::post(&format!("http://{}/tx", rest_addr)).send(&tx_hex)?;
401403
assert_eq!(broadcast1_resp.status(), 200);
402-
assert_eq!(broadcast1_resp.into_string()?, txid.to_string());
404+
assert_eq!(
405+
broadcast1_resp.into_body().read_to_string()?,
406+
txid.to_string()
407+
);
403408
// Mine the tx then submit it again. Should now fail.
404409
tester.mine()?;
405-
let broadcast2_res = ureq::post(&format!("http://{}/tx", rest_addr)).send_string(&tx_hex);
406-
let broadcast2_resp = broadcast2_res.unwrap_err().into_response().unwrap();
410+
let broadcast2_resp = ureq::post(&format!("http://{}/tx", rest_addr))
411+
.config()
412+
.http_status_as_error(false)
413+
.build()
414+
.send(&tx_hex)?;
407415
assert_eq!(broadcast2_resp.status(), 400);
408416

409417
rest_handle.stop();
@@ -416,24 +424,27 @@ fn test_rest_package_validation() -> Result<()> {
416424

417425
// Test POST /txs/package - simple validation test
418426
// Test with invalid JSON first to verify the endpoint exists
419-
let invalid_package_result = ureq::post(&format!("http://{}/txs/package", rest_addr))
420-
.set("Content-Type", "application/json")
421-
.send_string("invalid json");
422-
let invalid_package_resp = invalid_package_result.unwrap_err().into_response().unwrap();
423-
let status = invalid_package_resp.status();
427+
let invalid_package_resp = ureq::post(&format!("http://{}/txs/package", rest_addr))
428+
.header("Content-Type", "application/json")
429+
.config()
430+
.http_status_as_error(false)
431+
.build()
432+
.send("invalid json")?;
424433
// Should be 400 for bad JSON, not 404 for missing endpoint
425434
assert_eq!(
426-
status, 400,
435+
invalid_package_resp.status(),
436+
400,
427437
"Endpoint should exist and return 400 for invalid JSON"
428438
);
429439

430440
// Now test with valid but empty package, should fail
431-
let empty_package_result = ureq::post(&format!("http://{}/txs/package", rest_addr))
432-
.set("Content-Type", "application/json")
433-
.send_string("[]");
434-
let empty_package_resp = empty_package_result.unwrap_err().into_response().unwrap();
435-
let status = empty_package_resp.status();
436-
assert_eq!(status, 400);
441+
let empty_package_resp = ureq::post(&format!("http://{}/txs/package", rest_addr))
442+
.header("Content-Type", "application/json")
443+
.config()
444+
.http_status_as_error(false)
445+
.build()
446+
.send("[]")?;
447+
assert_eq!(empty_package_resp.status(), 400);
437448

438449
rest_handle.stop();
439450
Ok(())
@@ -1145,15 +1156,11 @@ fn test_rest_submit_package() -> Result<()> {
11451156
}
11461157

11471158
// Now submit this transaction package via the package endpoint
1148-
let package_json =
1149-
serde_json::json!([tx1_signed_hex.clone(), tx2_signed_hex.clone()]).to_string();
1150-
let package_result = ureq::post(&format!("http://{}/txs/package", rest_addr))
1151-
.set("Content-Type", "application/json")
1152-
.send_string(&package_json);
1159+
let package_resp = ureq::post(&format!("http://{}/txs/package", rest_addr))
1160+
.send_json([tx1_signed_hex, tx2_signed_hex])?;
11531161

1154-
let package_resp = package_result.unwrap();
11551162
assert_eq!(package_resp.status(), 200);
1156-
let package_result = package_resp.into_json::<Value>()?;
1163+
let package_result = package_resp.into_body().read_json::<Value>()?;
11571164

11581165
// Verify the response structure
11591166
assert!(package_result["tx-results"].is_object());

0 commit comments

Comments
 (0)