+
+ #[test]
+ fn into_txid_from_json_response_with_unexpected_type() {
+ let response = JsonResponse(serde_json::json!({ "result": "foo" }));
+ match TryInto::<Txid>::try_into(response) {
+ Err(e) => {
+ assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
+ assert_eq!(e.get_ref().unwrap().to_string(), "expected JSON string");
+ }
+ Ok(_) => panic!("Expected error"),
+ }
+ }
+
+ #[test]
+ fn into_txid_from_json_response_with_invalid_hex_data() {
+ let response = JsonResponse(serde_json::json!("foobar"));
+ match TryInto::<Txid>::try_into(response) {
+ Err(e) => {
+ assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
+ assert_eq!(e.get_ref().unwrap().to_string(), "invalid hex data");
+ }
+ Ok(_) => panic!("Expected error"),
+ }
+ }
+
+ #[test]
+ fn into_txid_from_json_response_with_invalid_txid_data() {
+ let response = JsonResponse(serde_json::json!("abcd"));
+ match TryInto::<Txid>::try_into(response) {
+ Err(e) => {
+ assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
+ assert_eq!(e.get_ref().unwrap().to_string(), "invalid txid");
+ }
+ Ok(_) => panic!("Expected error"),
+ }
+ }
+
+ #[test]
+ fn into_txid_from_json_response_with_valid_txid_data() {
+ let target_txid = Txid::from_slice(&[1; 32]).unwrap();
+ let response = JsonResponse(serde_json::json!(encode::serialize_hex(&target_txid)));
+ match TryInto::<Txid>::try_into(response) {
+ Err(e) => panic!("Unexpected error: {:?}", e),
+ Ok(txid) => assert_eq!(txid, target_txid),
+ }
+ }