1
1
use crate :: http:: { HttpClient , HttpEndpoint , JsonResponse } ;
2
2
3
- use base64;
4
3
use serde_json;
5
4
6
5
use std:: convert:: TryFrom ;
@@ -16,10 +15,13 @@ pub struct RpcClient {
16
15
}
17
16
18
17
impl RpcClient {
19
- pub fn new ( user_auth : & str , endpoint : HttpEndpoint ) -> std:: io:: Result < Self > {
18
+ /// Creates a new RPC client connected to the given endpoint with the provided credentials. The
19
+ /// credentials should be a base64 encoding of a user name and password joined by a colon, as is
20
+ /// required for HTTP basic access authentication.
21
+ pub fn new ( credentials : & str , endpoint : HttpEndpoint ) -> std:: io:: Result < Self > {
20
22
let client = HttpClient :: connect ( & endpoint) ?;
21
23
Ok ( Self {
22
- basic_auth : "Basic " . to_string ( ) + & base64 :: encode ( user_auth ) ,
24
+ basic_auth : "Basic " . to_string ( ) + credentials ,
23
25
endpoint,
24
26
client,
25
27
id : AtomicUsize :: new ( 0 ) ,
@@ -64,6 +66,9 @@ mod tests {
64
66
use super :: * ;
65
67
use crate :: http:: client_tests:: { HttpServer , MessageBody } ;
66
68
69
+ /// Credentials encoded in base64.
70
+ const CREDENTIALS : & ' static str = "dXNlcjpwYXNzd29yZA==" ;
71
+
67
72
/// Converts a JSON value into `u64`.
68
73
impl TryInto < u64 > for JsonResponse {
69
74
type Error = std:: io:: Error ;
@@ -79,7 +84,7 @@ mod tests {
79
84
#[ tokio:: test]
80
85
async fn call_method_returning_unknown_response ( ) {
81
86
let server = HttpServer :: responding_with_not_found ( ) ;
82
- let mut client = RpcClient :: new ( "credentials" , server. endpoint ( ) ) . unwrap ( ) ;
87
+ let mut client = RpcClient :: new ( CREDENTIALS , server. endpoint ( ) ) . unwrap ( ) ;
83
88
84
89
match client. call_method :: < u64 > ( "getblockcount" , & [ ] ) . await {
85
90
Err ( e) => assert_eq ! ( e. kind( ) , std:: io:: ErrorKind :: NotFound ) ,
@@ -91,7 +96,7 @@ mod tests {
91
96
async fn call_method_returning_malfomred_response ( ) {
92
97
let response = serde_json:: json!( "foo" ) ;
93
98
let server = HttpServer :: responding_with_ok ( MessageBody :: Content ( response) ) ;
94
- let mut client = RpcClient :: new ( "credentials" , server. endpoint ( ) ) . unwrap ( ) ;
99
+ let mut client = RpcClient :: new ( CREDENTIALS , server. endpoint ( ) ) . unwrap ( ) ;
95
100
96
101
match client. call_method :: < u64 > ( "getblockcount" , & [ ] ) . await {
97
102
Err ( e) => {
@@ -108,7 +113,7 @@ mod tests {
108
113
"error" : { "code" : -8 , "message" : "invalid parameter" } ,
109
114
} ) ;
110
115
let server = HttpServer :: responding_with_ok ( MessageBody :: Content ( response) ) ;
111
- let mut client = RpcClient :: new ( "credentials" , server. endpoint ( ) ) . unwrap ( ) ;
116
+ let mut client = RpcClient :: new ( CREDENTIALS , server. endpoint ( ) ) . unwrap ( ) ;
112
117
113
118
let invalid_block_hash = serde_json:: json!( "foo" ) ;
114
119
match client. call_method :: < u64 > ( "getblock" , & [ invalid_block_hash] ) . await {
@@ -124,7 +129,7 @@ mod tests {
124
129
async fn call_method_returning_missing_result ( ) {
125
130
let response = serde_json:: json!( { "result" : null } ) ;
126
131
let server = HttpServer :: responding_with_ok ( MessageBody :: Content ( response) ) ;
127
- let mut client = RpcClient :: new ( "credentials" , server. endpoint ( ) ) . unwrap ( ) ;
132
+ let mut client = RpcClient :: new ( CREDENTIALS , server. endpoint ( ) ) . unwrap ( ) ;
128
133
129
134
match client. call_method :: < u64 > ( "getblockcount" , & [ ] ) . await {
130
135
Err ( e) => {
@@ -139,7 +144,7 @@ mod tests {
139
144
async fn call_method_returning_valid_result ( ) {
140
145
let response = serde_json:: json!( { "result" : 654470 } ) ;
141
146
let server = HttpServer :: responding_with_ok ( MessageBody :: Content ( response) ) ;
142
- let mut client = RpcClient :: new ( "credentials" , server. endpoint ( ) ) . unwrap ( ) ;
147
+ let mut client = RpcClient :: new ( CREDENTIALS , server. endpoint ( ) ) . unwrap ( ) ;
143
148
144
149
match client. call_method :: < u64 > ( "getblockcount" , & [ ] ) . await {
145
150
Err ( e) => panic ! ( "Unexpected error: {:?}" , e) ,
0 commit comments