@@ -19,27 +19,117 @@ where
1919 type Rejection = Response ;
2020
2121 async fn from_request_parts ( parts : & mut Parts , _state : & S ) -> Result < Self , Self :: Rejection > {
22- let raw_query = parts. uri . query ( ) ;
23-
24- if raw_query. is_none ( ) {
25- return Err ( responses:: error:: Error :: from ( ParseScrapeQueryError :: MissingParams {
26- location : Location :: caller ( ) ,
27- } )
28- . into_response ( ) ) ;
22+ match extract_scrape_from ( parts. uri . query ( ) ) {
23+ Ok ( scrape_request) => Ok ( ExtractRequest ( scrape_request) ) ,
24+ Err ( error) => Err ( error. into_response ( ) ) ,
2925 }
26+ }
27+ }
3028
31- let query = raw_query. unwrap ( ) . parse :: < Query > ( ) ;
29+ fn extract_scrape_from ( maybe_raw_query : Option < & str > ) -> Result < Scrape , responses:: error:: Error > {
30+ if maybe_raw_query. is_none ( ) {
31+ return Err ( responses:: error:: Error :: from ( ParseScrapeQueryError :: MissingParams {
32+ location : Location :: caller ( ) ,
33+ } ) ) ;
34+ }
3235
33- if let Err ( error) = query {
34- return Err ( responses:: error:: Error :: from ( error) . into_response ( ) ) ;
35- }
36+ let query = maybe_raw_query. unwrap ( ) . parse :: < Query > ( ) ;
37+
38+ if let Err ( error) = query {
39+ return Err ( responses:: error:: Error :: from ( error) ) ;
40+ }
41+
42+ let scrape_request = Scrape :: try_from ( query. unwrap ( ) ) ;
43+
44+ if let Err ( error) = scrape_request {
45+ return Err ( responses:: error:: Error :: from ( error) ) ;
46+ }
47+
48+ Ok ( scrape_request. unwrap ( ) )
49+ }
3650
37- let scrape_request = Scrape :: try_from ( query. unwrap ( ) ) ;
51+ #[ cfg( test) ]
52+ mod tests {
53+ use std:: str:: FromStr ;
3854
39- if let Err ( error) = scrape_request {
40- return Err ( responses:: error:: Error :: from ( error) . into_response ( ) ) ;
55+ use super :: extract_scrape_from;
56+ use crate :: http:: axum_implementation:: requests:: scrape:: Scrape ;
57+ use crate :: http:: axum_implementation:: responses:: error:: Error ;
58+ use crate :: protocol:: info_hash:: InfoHash ;
59+
60+ struct TestInfoHash {
61+ pub bencoded : String ,
62+ pub value : InfoHash ,
63+ }
64+
65+ fn test_info_hash ( ) -> TestInfoHash {
66+ TestInfoHash {
67+ bencoded : "%3B%24U%04%CF%5F%11%BB%DB%E1%20%1C%EAjk%F4Z%EE%1B%C0" . to_owned ( ) ,
68+ value : InfoHash :: from_str ( "3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0" ) . unwrap ( ) ,
4169 }
70+ }
71+
72+ fn assert_error_response ( error : & Error , error_message : & str ) {
73+ assert ! (
74+ error. failure_reason. contains( error_message) ,
75+ "Error response does not contain message: '{error_message}'. Error: {error:?}"
76+ ) ;
77+ }
78+
79+ #[ test]
80+ fn it_should_extract_the_scrape_request_from_the_url_query_params ( ) {
81+ let info_hash = test_info_hash ( ) ;
82+
83+ let raw_query = format ! ( "info_hash={}" , info_hash. bencoded) ;
84+
85+ let scrape = extract_scrape_from ( Some ( & raw_query) ) . unwrap ( ) ;
86+
87+ assert_eq ! (
88+ scrape,
89+ Scrape {
90+ info_hashes: vec![ info_hash. value] ,
91+ }
92+ ) ;
93+ }
94+
95+ #[ test]
96+ fn it_should_extract_the_scrape_request_from_the_url_query_params_with_more_than_one_info_hash ( ) {
97+ let info_hash = test_info_hash ( ) ;
98+
99+ let raw_query = format ! ( "info_hash={}&info_hash={}" , info_hash. bencoded, info_hash. bencoded) ;
100+
101+ let scrape = extract_scrape_from ( Some ( & raw_query) ) . unwrap ( ) ;
102+
103+ assert_eq ! (
104+ scrape,
105+ Scrape {
106+ info_hashes: vec![ info_hash. value, info_hash. value] ,
107+ }
108+ ) ;
109+ }
110+
111+ #[ test]
112+ fn it_should_reject_a_request_without_query_params ( ) {
113+ let response = extract_scrape_from ( None ) . unwrap_err ( ) ;
114+
115+ assert_error_response (
116+ & response,
117+ "Cannot parse query params for scrape request: missing query params for scrape request" ,
118+ ) ;
119+ }
120+
121+ #[ test]
122+ fn it_should_reject_a_request_with_a_query_that_cannot_be_parsed ( ) {
123+ let invalid_query = "param1=value1=value2" ;
124+ let response = extract_scrape_from ( Some ( invalid_query) ) . unwrap_err ( ) ;
125+
126+ assert_error_response ( & response, "Cannot parse query params" ) ;
127+ }
128+
129+ #[ test]
130+ fn it_should_reject_a_request_with_a_query_that_cannot_be_parsed_into_a_scrape_request ( ) {
131+ let response = extract_scrape_from ( Some ( "param1=value1" ) ) . unwrap_err ( ) ;
42132
43- Ok ( ExtractRequest ( scrape_request . unwrap ( ) ) )
133+ assert_error_response ( & response , "Cannot parse query params for scrape request" ) ;
44134 }
45135}
0 commit comments