Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a replaceable RestClient #1175

Open
wants to merge 7 commits into
base: savage-wasm-sdk-common
Choose a base branch
from

Conversation

dangeross
Copy link
Collaborator

This PR:

  • Introduces a RestClient trait (with ReqwestRestClient and MockRestClient implementations)
  • Allows external REST services (e.g. mempool.space, LNURL services) to be mocked for testing
  • Removes the mockito dependency, simplifies WASM testing and removes URL injecting for test purposes

Copy link
Contributor

@danielgranhao danielgranhao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! I gave it a first pass and looks good. Will give it a second look later.

Copy link
Contributor

@JssDWt JssDWt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good!
I like the way the responses can be set in the mock client. I very much like that we're not calling mempool.space in the tests anymore.

Some nits.

}

pub async fn parse_with_rest_client(
rest_client: Arc<dyn RestClient>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs for the reqwest client say

You do not have to wrap the Client in an [Rc] or Arc to reuse it, because it already uses an Arc internally.

So nit: I think functions can use a borrowed rest client rather than an Arc. That saves clones on passing it to the next function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this what you had in mind? 0f4633d

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. I had in mind

pub async fn validate_lnurl_withdraw(rest_client: &dyn RestClient)

... 

let mock_rest_client = MockRestClient::new();
validate_lnurl_withdraw(&mock_rest_client, withdraw_req, invoice);

But coding it up it would require generics, to support cloning the rest client. It would get quite some feet in the earth. So let's stick with the arcs.

@@ -469,6 +472,9 @@ fn ln_address_decode(ln_address: &str) -> Result<(String, String, String)> {
// It is safe to downcase the domains since they are case-insensitive.
// https://www.rfc-editor.org/rfc/rfc3986#section-3.2.2
let domain = split[1].to_lowercase();
if !domain.contains('.') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What caused you to add this here in this PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MockRestClient was picking up a request to get LNURL info for 03864ef025fde8fb587d989186ce6a4a186895ee44a926bfc370e2c366597a3zzz@sdfsffs (in the node_id parsing trst). I think TLDs need a dot right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically it could be localhost, or other domains in the local network. So this failed before because the network call failed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this failed before because the network call failed?

Yes. Ok, it maybe makes sense to remove this then and add a mock response for this case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants