@@ -19,10 +19,14 @@ use std::net::{IpAddr, Ipv4Addr, SocketAddr};
19
19
use stacks_common:: types:: chainstate:: { BurnchainHeaderHash , ConsensusHash } ;
20
20
use stacks_common:: types:: net:: PeerHost ;
21
21
22
- use crate :: net:: api:: getsortition:: { GetSortitionHandler , QuerySpecifier } ;
22
+ use crate :: net:: api:: getsortition:: { GetSortitionHandler , QuerySpecifier , SortitionInfo } ;
23
+ use crate :: net:: api:: tests:: test_rpc;
23
24
use crate :: net:: connection:: ConnectionOptions ;
24
- use crate :: net:: http:: { Error as HttpError , HttpRequestPreamble , HttpVersion } ;
25
- use crate :: net:: httpcore:: { RPCRequestHandler , StacksHttp , StacksHttpPreamble } ;
25
+ use crate :: net:: http:: {
26
+ Error as HttpError , HttpRequestContents , HttpRequestPreamble , HttpResponse ,
27
+ HttpResponsePayload , HttpVersion ,
28
+ } ;
29
+ use crate :: net:: httpcore:: { RPCRequestHandler , StacksHttp , StacksHttpPreamble , StacksHttpRequest } ;
26
30
use crate :: net:: Error as NetError ;
27
31
28
32
fn make_preamble ( query : & str ) -> HttpRequestPreamble {
@@ -99,3 +103,65 @@ fn test_parse_request() {
99
103
}
100
104
}
101
105
}
106
+
107
+ #[ test]
108
+ fn response ( ) {
109
+ let addr = SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: LOCALHOST ) , 33333 ) ;
110
+
111
+ let request = StacksHttpRequest :: new_for_peer (
112
+ addr. into ( ) ,
113
+ "GET" . into ( ) ,
114
+ "/v3/sortitions" . into ( ) ,
115
+ HttpRequestContents :: new ( ) ,
116
+ )
117
+ . expect ( "FATAL: failed to construct request from infallible data" ) ;
118
+ let mut responses = test_rpc ( function_name ! ( ) , vec ! [ request] ) ;
119
+ let HttpResponsePayload :: JSON ( response) =
120
+ responses. pop ( ) . unwrap ( ) . get_http_payload_ok ( ) . unwrap ( )
121
+ else {
122
+ panic ! ( "Expected JSON response" ) ;
123
+ } ;
124
+
125
+ info ! ( "Response:\n {:#?}\n " , response) ;
126
+
127
+ let info_array = response. as_array ( ) . expect ( "Response should be array" ) ;
128
+ assert_eq ! (
129
+ info_array. len( ) ,
130
+ 1 ,
131
+ "/v3/sortitions should return a single entry"
132
+ ) ;
133
+
134
+ let addr = SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: LOCALHOST ) , 33333 ) ;
135
+ let request = StacksHttpRequest :: new_for_peer (
136
+ addr. into ( ) ,
137
+ "GET" . into ( ) ,
138
+ "/v3/sortitions/latest_and_last" . into ( ) ,
139
+ HttpRequestContents :: new ( ) ,
140
+ )
141
+ . expect ( "FATAL: failed to construct request from infallible data" ) ;
142
+ let mut responses = test_rpc ( function_name ! ( ) , vec ! [ request] ) ;
143
+ let HttpResponsePayload :: JSON ( response) =
144
+ responses. pop ( ) . unwrap ( ) . get_http_payload_ok ( ) . unwrap ( )
145
+ else {
146
+ panic ! ( "Expected JSON response" ) ;
147
+ } ;
148
+
149
+ info ! ( "Response:\n {:#?}\n " , response) ;
150
+
151
+ let info_array = response. as_array ( ) . expect ( "Response should be array" ) ;
152
+ assert_eq ! (
153
+ info_array. len( ) ,
154
+ 2 ,
155
+ "/v3/sortitions/latest_and_last should return 2 entries"
156
+ ) ;
157
+ let first_entry: SortitionInfo = serde_json:: from_value ( info_array[ 0 ] . clone ( ) )
158
+ . expect ( "Response array elements should parse to SortitionInfo" ) ;
159
+ let second_entry: SortitionInfo = serde_json:: from_value ( info_array[ 1 ] . clone ( ) )
160
+ . expect ( "Response array elements should parse to SortitionInfo" ) ;
161
+ assert ! ( first_entry. was_sortition) ;
162
+ assert ! ( second_entry. was_sortition) ;
163
+ assert_eq ! (
164
+ first_entry. last_sortition_ch. as_ref( ) . unwrap( ) ,
165
+ & second_entry. consensus_hash,
166
+ ) ;
167
+ }
0 commit comments