Skip to content
Closed
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
9 changes: 8 additions & 1 deletion src/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,14 @@ fn handle_request(

let txs = query
.chain()
.get_block_txs(&hash, start_index, CHAIN_TXS_PER_PAGE)?
.get_block_txs(&hash, start_index, CHAIN_TXS_PER_PAGE)
.map_err(|e| {
if e.description().contains("block not found") {
HttpError::not_found("Block not found".to_string())
} else {
HttpError::from(e)
}
})?
.into_iter()
.map(|tx| (tx, blockid))
.collect();
Expand Down
20 changes: 20 additions & 0 deletions tests/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,26 @@ fn test_rest_block() -> Result<()> {
let res = get_plain(rest_addr, &format!("/block/{}/txid/1", blockhash))?;
assert_eq!(res, txid.to_string());

// Test GET /block/:hash/txid/:index
// Should fail with 400 code when block hash is invalid
let invalid_hash_resp = ureq::get(&format!("http://{}/block/{}/txs/1", rest_addr, "invalid_hash"))
.config()
.http_status_as_error(false)
.build()
.call()?;
assert_eq!(invalid_hash_resp.status(), 400);
assert_eq!(invalid_hash_resp.into_body().read_to_string()?, "Invalid hex string");

// Test GET /block/:hash/txid/:index
// Should fail with 404 code when block isn't found
let invalid_hash_resp = ureq::get(&format!("http://{}/block/{}/txs/0", rest_addr, "0000000000000000000000000000000000000000000000000000000000000000"))
.config()
.http_status_as_error(false)
.build()
.call()?;
assert_eq!(invalid_hash_resp.status(), 404);
assert_eq!(invalid_hash_resp.into_body().read_to_string()?, "Block not found");

rest_handle.stop();
Ok(())
}
Expand Down