From 59121d519e1c6cca5df11056e242dd94dde0a95b Mon Sep 17 00:00:00 2001 From: jbesraa Date: Fri, 1 Dec 2023 19:23:45 +0200 Subject: [PATCH] test `WellKnownError` --- payjoin/Cargo.toml | 2 ++ payjoin/tests/integration.rs | 41 +++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/payjoin/Cargo.toml b/payjoin/Cargo.toml index 067da7c0..73ba5016 100644 --- a/payjoin/Cargo.toml +++ b/payjoin/Cargo.toml @@ -31,6 +31,8 @@ url = "2.2.2" [dev-dependencies] env_logger = "0.9.0" bitcoind = { version = "0.31.1", features = ["0_21_2"] } +httpmock = "0.7.0-rc.1" +isahc = "1.7.2" [package.metadata.docs.rs] features = ["send", "receive", "base64"] diff --git a/payjoin/tests/integration.rs b/payjoin/tests/integration.rs index dd4cc545..a40b216b 100644 --- a/payjoin/tests/integration.rs +++ b/payjoin/tests/integration.rs @@ -8,11 +8,14 @@ mod integration { use bitcoind::bitcoincore_rpc; use bitcoind::bitcoincore_rpc::core_rpc_json::{AddressType, WalletProcessPsbtResult}; use bitcoind::bitcoincore_rpc::RpcApi; + use httpmock::prelude::*; use log::{debug, log_enabled, Level}; use payjoin::bitcoin::base64; use payjoin::receive::Headers; - use payjoin::send::{Request, RequestBuilder}; + use payjoin::send::error::WellKnownError; + use payjoin::send::{Request, RequestBuilder, ResponseError}; use payjoin::{bitcoin, Error, Uri}; + use serde_json::json; #[test] fn integration_test() { @@ -91,6 +94,42 @@ mod integration { // ********************** // Inside the Receiver: // this data would transit from one party to another over the network in production + let receiver_server = MockServer::start(); + { + receiver_server.mock(|when, then| { + // let err = Error::BadRequest + let error_response = Error::Server("".to_string().into()); + // dbg!(&error_response); + when.method(httpmock::Method::POST).path("/payjoin"); + then.status(200) + .header("content-type", "text/plain") + .body(error_response.to_string()); + }); + let mut response = isahc::post(receiver_server.url("/payjoin"), "").unwrap(); + let client_response = ctx.clone().process_response(response.body_mut()); + dbg!(&client_response); + } + { + receiver_server.mock(|when, then| { + let error_response = + json!({ "errorCode": WellKnownError::NotEnoughMoney.to_string(), "message": + WellKnownError::NotEnoughMoney.message() + }); + when.method(httpmock::Method::POST).path("/payjoin/1"); + then.status(200) + .header("content-type", "text/plain") + .body(error_response.to_string()); + }); + let mut response = isahc::post(receiver_server.url("/payjoin/1"), "").unwrap(); + match ctx.clone().process_response(response.body_mut()) { + Ok(_) => panic!("should have failed"), + Err(e) => assert_eq!( + &e.to_string(), + &ResponseError::WellKnown(WellKnownError::NotEnoughMoney, "".to_string()) + .to_string() + ), + } + } let response = handle_pj_request(req, headers, receiver); // this response would be returned as http response to the sender