@@ -11,32 +11,41 @@ pub struct TestResponse {
1111 pub headers : Vec < Header < ' static > > ,
1212}
1313
14- pub async fn test_route (
14+ pub async fn test_request (
15+ method : & str ,
1516 path : & ' static str ,
17+ json : Option < Value > ,
1618 expected_status : Status ,
19+ client : Option < & Client > ,
1720) -> TestResponse {
18- let rocket = web:: rocket ( ) ;
19- let client = Client :: tracked ( rocket)
20- . await
21- . expect ( "Failed to launch web server" ) ;
21+ let client = match client {
22+ Some ( c) => c. to_owned ( ) ,
23+ None => {
24+ let rocket = web:: rocket ( ) ;
25+ & Client :: tracked ( rocket)
26+ . await
27+ . expect ( "Failed to launch web server" )
28+ }
29+ } ;
2230
23- let response = client. get ( path) . dispatch ( ) . await ;
24- create_test_response ( response, expected_status) . await
25- }
31+ let request = match method. to_lowercase ( ) . as_str ( ) {
32+ "get" => client. get ( path) ,
33+ "post" => client. post ( path) ,
34+ "put" => client. put ( path) ,
35+ "delete" => client. delete ( path) ,
36+ "patch" => client. patch ( path) ,
37+ _ => panic ! ( "Unsupported HTTP method: {}" , method) ,
38+ } ;
2639
27- pub async fn test_post_json (
28- client : & Client ,
29- path : & ' static str ,
30- json : Value ,
31- expected_status : Status ,
32- ) -> TestResponse {
33- let response = client
34- . post ( path)
35- . header ( ContentType :: JSON )
36- . body ( json. to_string ( ) )
37- . dispatch ( )
38- . await ;
40+ let request = if let Some ( json_value) = json {
41+ request
42+ . header ( ContentType :: JSON )
43+ . body ( json_value. to_string ( ) )
44+ } else {
45+ request
46+ } ;
3947
48+ let response = request. dispatch ( ) . await ;
4049 create_test_response ( response, expected_status) . await
4150}
4251
@@ -63,15 +72,15 @@ async fn create_test_response(
6372
6473#[ rocket:: async_test]
6574async fn test_swagger_ui_route ( ) {
66- test_route ( " /swagger-ui/", Status :: SeeOther ) . await ;
75+ test_request ( "get" , " /swagger-ui/", None , Status :: SeeOther , None ) . await ;
6776}
6877
6978#[ rocket:: async_test]
7079async fn test_rapidoc_route ( ) {
71- test_route ( " /rapidoc/", Status :: SeeOther ) . await ;
80+ test_request ( "get" , " /rapidoc/", None , Status :: SeeOther , None ) . await ;
7281}
7382
7483#[ rocket:: async_test]
7584async fn test_non_existent_route ( ) {
76- test_route ( " /non-existent", Status :: NotFound ) . await ;
85+ test_request ( "get" , " /non-existent", None , Status :: NotFound , None ) . await ;
7786}
0 commit comments