Skip to content

Commit

Permalink
test WellKnownError
Browse files Browse the repository at this point in the history
  • Loading branch information
jbesraa committed Dec 1, 2023
1 parent 50033f9 commit 59121d5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions payjoin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
41 changes: 40 additions & 1 deletion payjoin/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 59121d5

Please sign in to comment.