@@ -15,6 +15,10 @@ use futures::{future, Stream, StreamExt};
1515use http:: StatusCode ;
1616use hyper:: Body ;
1717#[ cfg( test) ]
18+ use once_cell:: sync:: Lazy ;
19+ #[ cfg( test) ]
20+ use regex:: Regex ;
21+ #[ cfg( test) ]
1822use serde:: Serialize ;
1923#[ cfg( test) ]
2024use serde_json:: Value ;
@@ -90,6 +94,12 @@ const SIGNATURES_FILTER: [EventFilter; 2] =
9094 [ EventFilter :: ApiVersion , EventFilter :: FinalitySignature ] ;
9195/// The filter associated with `/events/sidecar` path.
9296const SIDECAR_FILTER : [ EventFilter ; 1 ] = [ EventFilter :: SidecarVersion ] ;
97+
98+ #[ cfg( test) ]
99+ static ENDS_WITH_ID_REGEX : Lazy < Regex > = Lazy :: new ( || Regex :: new ( r"\nid:\d*$" ) . unwrap ( ) ) ;
100+ #[ cfg( test) ]
101+ static STARTS_WITH_REGEX : Lazy < Regex > = Lazy :: new ( || Regex :: new ( r"^data:" ) . unwrap ( ) ) ;
102+
93103/// The "id" field of the events sent on the event stream to clients.
94104pub type Id = u32 ;
95105pub type IsLegacyFilter = bool ;
@@ -483,10 +493,7 @@ fn parse_url_props(
483493 Some ( filter) => filter,
484494 None => return Err ( create_404 ( enable_legacy_filters) ) ,
485495 } ;
486- let start_from = match parse_query ( query) {
487- Ok ( maybe_id) => maybe_id,
488- Err ( error_response) => return Err ( error_response) ,
489- } ;
496+ let start_from = parse_query ( query) ?;
490497 Ok ( ( event_filter, stream_filter, start_from, is_legacy_filter) )
491498}
492499
@@ -687,7 +694,6 @@ mod tests {
687694 use super :: * ;
688695 use casper_types:: { testing:: TestRng , TransactionHash } ;
689696 use rand:: Rng ;
690- use regex:: Regex ;
691697 use std:: iter;
692698 #[ cfg( feature = "additional-metrics" ) ]
693699 use tokio:: sync:: mpsc:: channel;
@@ -1016,17 +1022,15 @@ mod tests {
10161022 let expected_data = deduplicated_event. data . clone ( ) . unwrap ( ) ;
10171023 let mut received_event_str = received_event. to_string ( ) . trim ( ) . to_string ( ) ;
10181024
1019- let ends_with_id = Regex :: new ( r"\nid:\d*$" ) . unwrap ( ) ;
1020- let starts_with_data = Regex :: new ( r"^data:" ) . unwrap ( ) ;
10211025 if let Some ( id) = deduplicated_event. id {
10221026 assert ! ( received_event_str. ends_with( format!( "\n id:{}" , id) . as_str( ) ) ) ;
10231027 } else {
1024- assert ! ( !ends_with_id . is_match( received_event_str. as_str( ) ) ) ;
1028+ assert ! ( !ENDS_WITH_ID_REGEX . is_match( received_event_str. as_str( ) ) ) ;
10251029 } ;
1026- received_event_str = ends_with_id
1030+ received_event_str = ENDS_WITH_ID_REGEX
10271031 . replace_all ( received_event_str. as_str ( ) , "" )
10281032 . into_owned ( ) ;
1029- received_event_str = starts_with_data
1033+ received_event_str = STARTS_WITH_REGEX
10301034 . replace_all ( received_event_str. as_str ( ) , "" )
10311035 . into_owned ( ) ;
10321036 if is_legacy_endpoint {
0 commit comments